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/Namespace.hpp>
00016 #include <j2k/nto/Message.hpp>
00017
00018 #include <j2k/nto/Basic_PThread.cpp>
00019 #include <j2k/nto/Namespace.cpp>
00020 #include <j2k/nto/Message.cpp>
00021
00022 pthread_barrier_t barrier;
00023 pthread_barrier_t barrier2;
00024
00025 #define ATTACH_POINT "myname"
00026
00027 class Server : public Basic_PThread
00028 {
00029 private:
00030 int rcvid;
00031 char* name;
00032 MsgNameSpace* n;
00033 Message* m;
00034
00035 public:
00036 Server() : Basic_PThread()
00037 {
00038 name = ATTACH_POINT;
00039 n = new MsgNameSpace( name );
00040 m = new Message();
00041 }
00042
00043 virtual ~Server()
00044 {
00045 if ( n ) { delete[] n; }
00046 if ( m ) { delete[] m; }
00047 }
00048
00049
00050 virtual void run()
00051 {
00052 printf( "Server started \n" );
00053 n->Nattach( name );
00054
00055 printf( "Server attached \n" );
00056 fflush( stdout );
00057
00058 pthread_barrier_wait( &barrier );
00059
00060
00061 while (1)
00062 {
00063 rcvid = m->receive( n->getChannel() );
00064
00065 printf( "Received from %d \n", rcvid );
00066 fflush( stdout );
00067
00068 if (rcvid == -1) {
00069 break;
00070 }
00071
00072 if ( m->checkHeader( rcvid ) )
00073 {
00074 continue;
00075 }
00076
00077
00078 printf("Server receive %d \n", m->getNumber() );
00079 fflush( stdout );
00080
00081 m->reply( rcvid, 100 );
00082 }
00083
00084 n->Ndetach();
00085 }
00086
00087 };
00088
00089
00090 class Client : public Basic_PThread
00091 {
00092 char* name;
00093 MsgNameSpace* n;
00094 Message* m;
00095 Message* rm;
00096
00097 public:
00098 Client() : Basic_PThread()
00099 {
00100 name = ATTACH_POINT;
00101 n = new MsgNameSpace( name );
00102 m = new Message();
00103 rm = new Message();
00104 }
00105
00106 virtual ~Client()
00107 {
00108 if ( n ) { delete[] n; }
00109 if ( m ) { delete[] m; }
00110 if ( rm ) { delete[] rm; }
00111 }
00112
00113 virtual void run()
00114 {
00115 printf( "Client started \n" );
00116 int fd;
00117
00118 fd = n->Nopen( name );
00119
00120 printf( "Client opened a connection \n" );
00121 fflush( stdout );
00122
00123
00124 for ( register int i = 20; i < 25; i++ )
00125 {
00126 printf("Client sending %d \n", i );
00127 fflush( stdout );
00128
00129 m->setNumber( i );
00130 rm->setNumber( i );
00131
00132 if ( m->send( fd, rm ) == -1 )
00133 {
00134 break;
00135 }
00136
00137 printf("Reply: %d \n", rm->getNumber() );
00138 fflush( stdout );
00139 }
00140
00141 printf( "Client closed the connection \n" );
00142 fflush( stdout );
00143
00144
00145 n->Nclose( fd );
00146
00147 pthread_barrier_wait( &barrier2 );
00148 }
00149
00150 };
00151
00152 int main( int argc, char **argv )
00153 {
00154 pthread_barrier_init( &barrier, NULL, 2 );
00155 pthread_barrier_init( &barrier2, NULL, 2 );
00156
00157 Server s;
00158 Client c;
00159
00160 s.start();
00161 s.join();
00162
00163 pthread_barrier_wait( &barrier );
00164
00165 c.start();
00166 c.join();
00167
00168 printf( "Wait for barrier 2 \n" );
00169
00170 pthread_barrier_wait( &barrier2 );
00171
00172 printf( "done! \n" );
00173
00174 exit( 0 );
00175
00176 return 0;
00177 }
00178
00179 #endif