Download Message Passing Tools for Software Integration
Transcript
/* This handler is called whenever any message is received by a client
* The approach is to convert the message buffer to an object, and rename
the object to "incoming". Then the "COMMAND" slot is tested to see what
kind of message was received. The appropriate routine is then called for
:he incoming message. Each routine will then look at the "incoming" message
and act appropriately based on the contents of the message.
void handler(message, size, sfd)
char *message;
int size, sfd;
char command[20];
object_from_ascbuf(message, "incoming"); /* convert to an object */
object_slot_get("incoming",
"COMMAND", command);
if (strcmp(command, "PRESUBMIT") == 0)
presubmit(sfd);
else if (strcmp(command, "SUBMIT") == 0)
submit(sfd);
else if (strcmp(command, "TAKE") == 0)
take(sfd);
else if (strcmp(command, "GIVE") == 0)
give(sfd);
else if (strcmp(command, "DISPLAY") == 0)
display(sfd);
else if (strcmp(command, "POINT") == 0)
point(sfd);
/* This routine is called whenever a new client connects to the listening
* socket descriptor */
void new_client(sfd)
int sfd;
char picob[l0], *buf; int i, size, newsfd;
unsigned char busy;
newsfd = bs_accept(sfd);
/* accept connection */
FD_SET(newsfd, &clients);
/* add socket descriptor to the set of clients */
/* Tell all clients that the server is busy */
busy = 1;
object_slot_set("servstat",
"STATUS", &busy, OB_BYTE, 1);
buf = ascbuf_fromobject("servstat", &size);
for (i = 0; i < 256; i++)
if (FD_ISSET(i, &clients))
write(i, buf, strlen(buf));
/* Send the control message, picture messages, display message,
point message to the new client */
buf = ascbuf_from_object("control", &size);
write(newsfd, buf, strlen(buf));
for (i = 0; i < numpictures; i++) {
sprintf(picob, "pic%d", i);
buf = ascbuf_from_object(picob, &size);
write(newsfd, buf, strlen(buf));
buf = ascbuf_from_object ("display",
&size);
write(newsfd, buf, strlen(buf));
Table 29: Viewgraph Server Source Code
Page 103