Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/nto/bakJuly/cs.cpp

Go to the documentation of this file.
00001 #ifndef __J2K__CS_CPP__
00002 #define __J2K__CS_CPP__
00003 
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <errno.h>
00007 #include <string.h>
00008 #include <pthread.h>
00009 #include <sys/neutrino.h>
00010 #include <sys/dispatch.h>
00011 #include <sync.h>
00012 #include <time.h>
00013 
00014 pthread_barrier_t barrier;
00015 pthread_barrier_t barrier2;
00016 
00017 #define ATTACH_POINT "myname"
00018 
00019 /* We specify the header as being at least a pulse */
00020 typedef struct _pulse msg_header_t;
00021 
00022 /* Our real data comes after the header */
00023 typedef struct _my_data {
00024   msg_header_t hdr;
00025   int data;
00026 } my_data_t;
00027 
00028 /*** Server Side of the code ***/
00029 void* server( void* dummy ) 
00030 {
00031   printf( "Server started \n" );
00032 
00033   name_attach_t *attach;
00034   my_data_t msg;
00035   int rcvid;
00036 
00037   /* Create a local name (/dev/name/local/...) */
00038   if ((attach = name_attach(NULL, ATTACH_POINT, 0)) == NULL) {
00039     exit( EXIT_FAILURE );
00040   }
00041 
00042   printf( "Server attached \n" );
00043 
00044   pthread_barrier_wait( &barrier );
00045 
00046   /* Do your MsgReceive's here now with the chid */
00047   while (1) {
00048     rcvid = MsgReceive(attach->chid, &msg, sizeof(msg), NULL);
00049 
00050     if (rcvid == -1) {/* Error condition, exit */
00051       break;
00052     }
00053 
00054     if (rcvid == 0) {/* Pulse received */
00055       switch (msg.hdr.code) 
00056       {
00057       case _PULSE_CODE_DISCONNECT:
00058         /*
00059          * A client disconnected all its connections (called
00060          * name_close() for each name_open() of our name) or
00061          * terminated
00062          */
00063         ConnectDetach(msg.hdr.scoid);
00064         break;
00065       case _PULSE_CODE_UNBLOCK:
00066         /*
00067          * REPLY blocked client wants to unblock (was hit by
00068          * a signal or timed out).  It's up to you if you
00069          * reply now or later.
00070          */
00071         break;
00072       default:
00073         /*
00074          * A pulse sent by one of your processes or a
00075          * _PULSE_CODE_COIDDEATH or _PULSE_CODE_THREADDEATH 
00076          * from the kernel?
00077          */
00078          break;
00079       }
00080       continue;
00081     }
00082 
00083     /* A QNX IO message received, reject */
00084     if (msg.hdr.type >= _IO_BASE && msg.hdr.type <= _IO_MAX) {
00085       MsgError(rcvid, ENOSYS);
00086       continue;
00087     }
00088 
00089     /* A message (presumable ours) received, handle */
00090     printf("Server receive %d \n", msg.data);
00091     MsgReply(rcvid, EOK, 0, 0);
00092 
00093   }
00094 
00095   /* Remove the name from the space */
00096   name_detach(attach, 0);
00097 
00098   return NULL;
00099 }
00100 
00101 /*** Client Side of the code ***/
00102 void* client( void* dummy ) 
00103 {
00104   printf( "Client started \n" );
00105   my_data_t msg;
00106   int fd;
00107 
00108   if ((fd = name_open(ATTACH_POINT, 0)) == -1) {
00109     exit( EXIT_FAILURE );
00110   }
00111 
00112   printf( "Client opened a connection \n" );
00113 
00114   /* We would have pre-defined data to stuff here */
00115   msg.hdr.type  = 0x00;
00116   msg.hdr.subtype = 0x00;
00117 
00118   /* Do whatever work you wanted with server connection */
00119   for (msg.data=0; msg.data < 5; msg.data++) 
00120   {
00121     printf("Client sending %d \n", msg.data);
00122 
00123     if (MsgSend(fd, &msg, sizeof(msg), NULL, 0) == -1) 
00124     {
00125       break;
00126     }
00127   }
00128 
00129   printf( "Client closed the connection \n" );
00130   /* Close the connection */
00131   name_close(fd);
00132 
00133   return NULL;
00134 }
00135 
00136 int main( int argc, char **argv ) 
00137 {
00138   pthread_barrier_init( &barrier,  NULL, 2 );
00139 
00140   pthread_create( NULL, NULL, server, NULL );
00141 
00142   pthread_barrier_wait( &barrier );
00143 
00144   client( NULL );
00145 
00146   return 0;
00147 }
00148 
00149 #endif

Generated on Sun Oct 14 18:46:39 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001