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

C:/temp/src/j2k/nto/bakJuly/cs3.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 #include <j2k/nto/Basic_PThread.hpp>
00015 #include <j2k/nto/Basic_PThread.cpp>
00016 #include <j2k/nto/Message.hpp>
00017 #include <j2k/nto/Message.cpp>
00018 #include <j2k/nto/NameSpace.hpp>
00019 #include <j2k/nto/NameSpace.cpp>
00020 
00021 pthread_barrier_t barrier;
00022 pthread_barrier_t barrier2;
00023 
00024 #define ATTACH_POINT "myname"
00025 
00026 NameSpace n;
00027 
00028 /* We specify the header as being at least a pulse */
00029 typedef struct _pulse msg_header_t;
00030 
00031 /* Our real data comes after the header */
00032 typedef struct _my_data 
00033 {
00034   msg_header_t hdr;
00035   int data;
00036 } my_data_t;
00037 
00038 class Server : public Basic_PThread
00039 {
00040 public:
00041   Server() : Basic_PThread() { }
00042   virtual ~Server() { }
00043 
00044 /*** Server Side of the code ***/
00045 virtual void run()
00046 {
00047   printf( "Server started \n" );
00048 
00049   name_attach_t *attach;
00050   my_data_t msg;
00051   int rcvid;
00052 
00053   /* Create a local name (/dev/name/local/...) */
00054   if ((attach = name_attach(NULL, ATTACH_POINT, 0)) == NULL) 
00055   {
00056     exit( EXIT_FAILURE );
00057   }
00058 
00059   printf( "Server attached \n" );
00060 
00061   pthread_barrier_wait( &barrier  );
00062 
00063   /* Do your MsgReceive's here now with the chid */
00064   while( 1 ) 
00065   {
00066     rcvid = MsgReceive(attach->chid, &msg, sizeof(msg), NULL);
00067 
00068     if (rcvid == -1) /* Error condition, exit */
00069     {
00070       break;
00071     }
00072 
00073     if (rcvid == 0) /* Pulse received */
00074     {
00075       switch (msg.hdr.code) 
00076       {
00077       case _PULSE_CODE_DISCONNECT:
00078         /*
00079          * A client disconnected all its connections (called
00080          * name_close() for each name_open() of our name) or
00081          * terminated
00082          */
00083         ConnectDetach(msg.hdr.scoid);
00084         break;
00085       case _PULSE_CODE_UNBLOCK:
00086         /*
00087          * REPLY blocked client wants to unblock (was hit by
00088          * a signal or timed out).  It's up to you if you
00089          * reply now or later.
00090          */
00091         break;
00092       default:
00093         /*
00094          * A pulse sent by one of your processes or a
00095          * _PULSE_CODE_COIDDEATH or _PULSE_CODE_THREADDEATH 
00096          * from the kernel?
00097          */
00098          break;
00099       }
00100       continue;
00101     }
00102 
00103     /* A QNX IO message received, reject */
00104     if (msg.hdr.type >= _IO_BASE && msg.hdr.type <= _IO_MAX) 
00105     {
00106       MsgError(rcvid, ENOSYS);
00107       continue;
00108     }
00109 
00110     /* A message (presumable ours) received, handle */
00111     printf("Server receive %d \n", msg.data);
00112     MsgReply(rcvid, EOK, 0, 0);
00113 
00114   }
00115 
00116   /* Remove the name from the space */
00117   name_detach(attach, 0);
00118 
00119 //  pthread_barrier_wait( &barrier2 );
00120 }
00121 
00122 };
00123 
00124 /*** Client Side of the code ***/
00125 class Client : public Basic_PThread
00126 {
00127 public:
00128   Client() : Basic_PThread() { }
00129   virtual ~Client() { }
00130 
00131 /*** Server Side of the code ***/
00132 virtual void run()
00133 {
00134   printf( "Client started \n" );
00135   my_data_t msg;
00136   int fd;
00137 
00138   if ((fd = name_open(ATTACH_POINT, 0)) == -1) {
00139     exit( EXIT_FAILURE );
00140   }
00141 
00142   printf( "Client opened a connection \n" );
00143 
00144   /* We would have pre-defined data to stuff here */
00145   msg.hdr.type  = 0x00;
00146   msg.hdr.subtype = 0x00;
00147 
00148   /* Do whatever work you wanted with server connection */
00149   for (msg.data=0; msg.data < 5; msg.data++) 
00150   {
00151     printf("Client sending %d \n", msg.data);
00152 
00153     if (MsgSend(fd, &msg, sizeof(msg), NULL, 0) == -1) 
00154     {
00155       break;
00156     }
00157   }
00158 
00159   printf( "Client closed the connection \n" );
00160   /* Close the connection */
00161   name_close(fd);
00162 
00163   pthread_barrier_wait( &barrier2 );
00164 
00165   printf( "Client Done \n" );
00166 }
00167 
00168 };
00169 
00170 int main( int argc, char **argv ) 
00171 {
00172   pthread_barrier_init( &barrier,  NULL, 2 );
00173   pthread_barrier_init( &barrier2, NULL, 2 );
00174  
00175   Server s;
00176   Client c;
00177 
00178   s.start();
00179 
00180   pthread_barrier_wait( &barrier );
00181 
00182   c.start();
00183 
00184   pthread_join( c.getPID(), NULL );
00185 
00186   pthread_barrier_wait( &barrier2 );
00187 
00188   printf( "Main Done \n" );
00189 
00190   // Shut down the READ-blocked server
00191   pthread_kill( s.getPID(), SIGKILL );
00192 
00193   return 0;
00194 }
00195 
00196 #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