Changeset 7940
- Timestamp:
- Jul 19, 2006, 11:48:49 AM (20 years ago)
- Location:
- trunk/Ohana/src/opihi
- Files:
-
- 4 edited
-
include/pantasks.h (modified) (1 diff)
-
lib.shell/multicommand.c (modified) (1 diff)
-
pantasks/ListenClients.c (modified) (8 diffs)
-
pantasks/pantasks_server.c (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Ohana/src/opihi/include/pantasks.h
r7929 r7940 196 196 void InitClients (); 197 197 void AddNewClient (int client); 198 int DeleteClient (int client); 198 199 void *ListenClients (void *data); 199 200 -
trunk/Ohana/src/opihi/lib.shell/multicommand.c
r7938 r7940 70 70 } 71 71 72 // receive the command exit status 73 if (ExpectMessage (server, 2.0, &message)) { 74 switch (errno) { 75 case EPIPE: 76 gprint (GP_ERR, "server connection has died\n"); 77 exit (1); 78 default: 79 gprint (GP_ERR, "I/O error sending server message\n"); 80 exit (2); 81 } 82 } else { 83 sscanf (message.buffer, "STATUS %d", &status); 84 } 85 72 86 // receive the resulting stderr 73 87 if (ExpectMessage (server, 2.0, &message)) { -
trunk/Ohana/src/opihi/pantasks/ListenClients.c
r7938 r7940 5 5 static int Nclients; 6 6 static int *clients; 7 static IOBuffer * buffers;7 static IOBuffer **buffers; 8 8 9 9 void InitClients () { … … 12 12 NCLIENTS = 10; 13 13 ALLOCATE (clients, int, NCLIENTS); 14 ALLOCATE (buffers, IOBuffer , NCLIENTS);14 ALLOCATE (buffers, IOBuffer *, NCLIENTS); 15 15 } 16 16 … … 20 20 if (DEBUG) gprint (GP_LOG, "adding a new client (%d)\n", client); 21 21 clients[Nclients] = client; 22 InitIOBuffer(&buffers[Nclients], 256); 22 ALLOCATE (buffers[Nclients], IOBuffer, 1); 23 InitIOBuffer(buffers[Nclients], 256); 23 24 Nclients ++; 24 25 if (Nclients >= NCLIENTS - 1) { 25 26 NCLIENTS += 10; 26 27 REALLOCATE (clients, int, NCLIENTS); 27 REALLOCATE (buffers, IOBuffer , NCLIENTS);28 REALLOCATE (buffers, IOBuffer *, NCLIENTS); 28 29 } 30 } 31 32 /* add this client to the client table, create an IOBuffer for it */ 33 int DeleteClient (int client) { 34 35 int i, j; 36 37 if (DEBUG) gprint (GP_LOG, "deleting a client (%d)\n", client); 38 for (i = 0; i < Nclients; i++) { 39 if (clients[i] == client) { 40 FreeIOBuffer (buffers[i]); 41 free (buffers[i]); 42 close (clients[i]); 43 for (j = i; j < Nclients - 1; j++) { 44 clients[j] = clients[j+1]; 45 buffers[j] = buffers[j+1]; 46 } 47 Nclients --; 48 if ((Nclients > 10) && (Nclients / 2 < NCLIENTS)) { 49 NCLIENTS = Nclients + 10; 50 REALLOCATE (clients, int, NCLIENTS); 51 REALLOCATE (buffers, IOBuffer *, NCLIENTS); 52 } 53 return TRUE; 54 } 55 } 56 // did not find the client 57 return FALSE; 29 58 } 30 59 … … 56 85 Nmax = MAX (Nmax, clients[i]); 57 86 FD_SET (clients[i], &fdSet); 58 if (FD_ISSET(clients[i], &fdSet)) {59 if (DEBUG) gprint (GP_ERR, "fd %d is set\n", clients[i]);60 } else {61 if (DEBUG) gprint (GP_ERR, "fd %d is not set\n", clients[i]);62 }63 87 } 64 88 Nmax ++; … … 67 91 if (DEBUG) gprint (GP_ERR, "listening to %d clients\n", Ncurrent); 68 92 status = select (Nmax, &fdSet, NULL, NULL, &timeout); 69 70 if (DEBUG) gprint (GP_ERR, "messages from %d clients\n", status);71 72 93 if (status == -1) { 73 94 perror("select()"); … … 75 96 } 76 97 77 /* if no data, go backfor another select */98 /* if no data, update client list, wait for another select */ 78 99 if (status <= 0) continue; 79 100 80 101 /* loop over the clients with data */ 81 102 for (i = 0; i < Ncurrent; i++) { 103 /* if client has no data, skip it */ 82 104 if (!FD_ISSET(clients[i], &fdSet)) continue; 83 105 84 /* this client has data, handle it */ 85 if (DEBUG) { 86 gprint (GP_ERR, "data from client %d\n", clients[i]); 87 gprint (GP_ERR, "start: ", buffers[i].buffer); 88 gwrite (buffers[i].buffer, 1, buffers[i].Nbuffer, GP_ERR); 89 gprint (GP_ERR, "...\n"); 106 /* read until the pipe is empty: 0 is closed, -1 is empty, -2 is error */ 107 Nread = 1; 108 while (Nread > 0) { 109 Nread = ReadtoIOBuffer (buffers[i], clients[i]); 90 110 } 91 92 /* read until the pipe is empty: -1 is empty, -2 is error */ 93 Nread = 0; 94 while (Nread >= 0) { 95 Nread = ReadtoIOBuffer (&buffers[i], clients[i]); 96 if (DEBUG) gprint (GP_ERR, "read %d bytes from socket\n", Nread); 111 if ((Nread == 0) || (Nread == -2)) { 112 /* error: do something */ 113 if (DEBUG && (Nread == 0)) gprint (GP_ERR, "socket is closed\n"); 114 if (DEBUG && (Nread == -2)) gprint (GP_ERR, "error reading from socket\n"); 115 DeleteClient (clients[i]); 116 continue; 117 } 97 118 98 if (Nread == -2) { 99 /* error: do something */ 100 if (1) gprint (GP_ERR, "error reading from socket\n"); 101 } 102 } 103 if (DEBUG) gprint (GP_ERR, "read %d total bytes\n", buffers[i].Nbuffer); 119 if (DEBUG) gprint (GP_ERR, "read %d total bytes\n", buffers[i][0].Nbuffer); 104 120 105 if (DEBUG) { 106 gprint (GP_ERR, "end: ", buffers[i].buffer); 107 gwrite (buffers[i].buffer, 1, buffers[i].Nbuffer, GP_ERR); 108 gprint (GP_ERR, "...\n"); 109 } 110 111 /* see if we have a complete message waiting */ 112 line = CheckForMessage (&buffers[i]); 121 /* see if we have a complete message waiting; if not, keep waiting for messages */ 122 line = CheckForMessage (buffers[i]); 113 123 if (line == NULL) continue; 114 124 … … 116 126 /* in this thread, we set the print output destination to be an 117 127 internal buffer, which we dump at the end of the execution */ 118 /* XXX : we need to handle ; in the client-parsing of commands*/128 /* the commands sent to the server should not have ; */ 119 129 stripwhite (line); 120 130 if (*line) { 121 131 122 if (1) fprintf (stderr, "got command: %s\n", line); 123 124 /* this works: we can send a simple message */ 125 # if 0 126 SendMessage (clients[i], "first"); 127 SendMessage (clients[i], "second"); 128 # endif 129 130 /* this works: we can send a message using the IOBuffers */ 131 # if 0 132 InitIOBuffer (&testbuffer, 64); 133 PrintIOBuffer (&testbuffer, "this is a test\n"); 134 SendMessageFixed (clients[i], testbuffer.Nbuffer, testbuffer.buffer); 135 PrintIOBuffer (&testbuffer, "this is a second test\n"); 136 SendMessageFixed (clients[i], testbuffer.Nbuffer, testbuffer.buffer); 137 # endif 138 139 /* this works: we can use the printing system to write a message */ 140 # if 0 141 gprintSetBuffer (GP_LOG); 142 gprint (GP_LOG, "test is a test line\n"); 143 outbuffer = gprintGetBuffer (GP_LOG); 144 SendMessageFixed (clients[i], outbuffer[0].Nbuffer, outbuffer[0].buffer); 145 146 gprint (GP_LOG, "test is a second test line\n"); 147 outbuffer = gprintGetBuffer (GP_LOG); 148 SendMessageFixed (clients[i], outbuffer[0].Nbuffer, outbuffer[0].buffer); 149 # endif 150 151 /* this works: we can use the gprint service */ 152 # if 0 153 gprintSetBuffer (GP_LOG); 154 gprint (GP_LOG, "test is a test line\n"); 155 status = multicommand (line); 156 157 outbuffer = gprintGetBuffer (GP_LOG); 158 SendMessageFixed (clients[i], outbuffer[0].Nbuffer, outbuffer[0].buffer); 159 gprintSetFile (GP_LOG, "stdout"); 160 161 SendMessage (clients[i], "second"); 162 # endif 163 164 /* this only kind of works: it acts as if we hang */ 165 # if 1 132 /* set buffers for the output for this client */ 166 133 gprintSetBuffer (GP_LOG); 167 134 gprintSetBuffer (GP_ERR); 168 135 169 // gprint (GP_LOG, "test is a test stdout\n"); 170 // gprint (GP_ERR, "test is a test stderr\n"); 171 136 /* run the command, return the exit status */ 172 137 status = multicommand (line); 173 fprintf (stderr, "sending response\n");138 SendMessage (clients[i], "STATUS %d", status); 174 139 175 140 // return the stderr messages first 176 141 outbuffer = gprintGetBuffer (GP_ERR); 177 142 SendMessageFixed (clients[i], outbuffer[0].Nbuffer, outbuffer[0].buffer); 178 fprintf (stderr, "sent stderr\n");179 143 144 // return the stdout messages first 180 145 outbuffer = gprintGetBuffer (GP_LOG); 181 146 SendMessageFixed (clients[i], outbuffer[0].Nbuffer, outbuffer[0].buffer); 182 fprintf (stderr, "sent stdout\n");183 147 148 /* clear and reset the output buffers */ 184 149 gprintSetFile (GP_LOG, "stdout"); 185 150 gprintSetFile (GP_ERR, "stderr"); 186 # endif187 151 } 188 152 free (line); 189 190 191 /* this function should return the output buffer192 to the currently selected client */193 // DumpOutputBuffer ();194 153 } 195 196 154 /* if Nread == -2, we probably need to drop the client */ 197 155 /* check if we need to drop / remove any clients */ … … 204 162 only this thread is allowed to decrease Nclients and remove 205 163 a client from the table */ 206 207 /* - read data from fd208 - if we have a complete line, request a block in the209 scheduler loop210 - execute the command211 - send the response back to the client.212 - all commands will have to send their output to a buffer,213 to be sent back to the calling process214 */ -
trunk/Ohana/src/opihi/pantasks/pantasks_server.c
r7929 r7940 33 33 gprintInit (); // each thread needs to init the printing system 34 34 35 //signal (SIGPIPE, gotsignal);35 signal (SIGPIPE, gotsignal); 36 36 // signal (SIGTSTP, gotsignal); 37 37 // signal (SIGTTIN, gotsignal);
Note:
See TracChangeset
for help on using the changeset viewer.
