BIE |
/home/weinberg/src/BIE/include/cli_server.h00001 #ifndef CLI_SERVER_h 00002 #define CLI_SERVER_h 00003 00004 #include <sys/types.h> 00005 #include <sys/socket.h> 00006 #include <netinet/in.h> 00007 #include <arpa/inet.h> 00008 #include <errno.h> 00009 #include <signal.h> 00010 #include <string.h> 00011 #include <sys/wait.h> 00012 #include <sys/stat.h> 00013 00014 #include <gfunction.h> 00015 #include <cc++/thread.h> 00016 00017 #include <cstdlib> // for exit 00018 #include <iostream> 00019 #include <iomanip> 00020 #include <string> 00021 00022 00023 using namespace std; 00024 using namespace ost; 00025 00026 00027 #define CLI_INPUT "cli_input_fifo" 00028 #define CLI_OUTPUT "cli_output_fifo" 00029 #define CLI_CONSOLE "cli_console_fifo" 00030 #define MY_PORT_ID 8000 00031 00032 static int wait_for_cli; 00033 00036 class input_redirector : public Thread 00037 { 00038 private: 00039 int sock_id; 00040 char pipe_name[255]; 00041 int pipe_id; 00042 char char_buf[255]; 00043 int num_read; 00044 int i; 00045 00046 ofstream debug; 00047 00048 public: 00050 void Setup(int in_sock_id, int in_pipe_id) { 00051 sock_id = in_sock_id; 00052 pipe_id = in_pipe_id; 00053 } 00055 void Setup(int in_sock_id, char * new_pipe_name) { 00056 sock_id = in_sock_id; 00057 strcpy(pipe_name, new_pipe_name); 00058 debug.open("debug_input.err"); 00059 } 00060 00062 void run() { 00063 pipe_id = open(pipe_name, O_WRONLY); 00064 debug << "Run input::pipe_id=" << pipe_id << endl << flush; 00065 if (pipe_id < 0) { 00066 perror("problem opening pipe in input_redirector"); 00067 system("/usr/bin/killall cli"); 00068 std::exit(-1); // std exit to kill app not just thread with thread::exit 00069 } 00070 00071 debug << "Run input::pipe_name=" << pipe_name << endl << flush; 00072 debug << "Run input::sock_id=" << sock_id << endl << flush; 00073 debug << "Run input::wait_for_cli=" << wait_for_cli << endl << flush; 00074 00075 while (wait_for_cli) { 00076 debug << " sock_id=" << sock_id << endl << flush; 00077 debug << " input_redirector:: reading from pipe" << endl << flush; 00078 num_read = read(sock_id, char_buf, sizeof(char_buf)); 00079 /* */ 00080 debug << "copying from socket to pipe start num_read=" << num_read << endl << flush; 00081 for (int i = 0; i < num_read; i++) 00082 debug << char_buf[i]; 00083 debug << "copying from socket to pipe end" << endl << flush; 00084 /* */ 00085 if (write(pipe_id, char_buf, num_read) < 0) { 00086 if (true) { 00087 perror("problem writing data to pipe in input_redirect"); 00088 system("/usr/bin/killall cli"); 00089 std::exit(-1); 00090 } 00091 } 00092 } 00093 } 00095 void Final() { 00096 ::close(pipe_id); // avoid conflict with Thread::close 00097 } 00098 }; 00099 00102 class output_redirector : public Thread 00103 { 00104 public: 00106 int sock_id; 00107 00109 char pipe_name[255]; 00110 00112 int pipe_id; 00113 00115 char char_buf[255]; 00116 00118 int num_read; 00119 00121 int i; 00122 00124 ofstream debug; 00125 00126 public: 00128 void Setup(int in_pipe_id, int in_sock_id) { 00129 sock_id = in_sock_id; 00130 pipe_id = in_pipe_id; 00131 } 00132 00134 void Setup(char * new_pipe_name, int in_sock_id) { 00135 sock_id = in_sock_id; 00136 strcpy(pipe_name, new_pipe_name); 00137 debug.open("debug_output.err"); 00138 } 00139 00141 void run() { 00142 pipe_id = open(pipe_name, O_RDONLY); 00143 debug << "output::pipe_id=" << pipe_id << endl << flush; 00144 if (pipe_id < 0) { 00145 perror("problem opening pipe in output_redirector"); 00146 system("/usr/bin/killall cli"); 00147 std::exit(-1); 00148 } 00149 00150 debug << "output::pipe_name=" << pipe_name << endl << flush; 00151 debug << "output::sock_id=" << sock_id << endl << flush; 00152 debug << "output::wait_for_cli=" << wait_for_cli << endl << flush; 00153 00154 while (wait_for_cli) { 00155 debug << " output_redirector:: reading from pipe" << endl; 00156 num_read = read(pipe_id, char_buf, sizeof(char_buf)); 00157 00158 debug << "copying from pipe to socket start num_read=" << num_read << endl; 00159 for (int i = 0; i < num_read; i++) 00160 debug << char_buf[i]; 00161 debug << "copying from pipe to socket start" << endl; 00162 00163 if (send(sock_id, char_buf, num_read, 0) < 0) { 00164 if (true) { 00165 perror("problem sending data to socket in output_redirect"); 00166 system("/usr/bin/killall cli"); 00167 std::exit(-1); 00168 } 00169 } 00170 } 00171 } 00173 void Final() { 00174 ::close(pipe_id); // avoid conflict with Thread::close 00175 } 00176 }; 00177 00178 00179 #endif Send suggestions, questions, and feedback to WEINBERG at ASTRO dot UMASS dot EDU. Documentation generated at Fri Mar 26 00:35:10 2010 by
|