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
00029 typedef struct _pulse msg_header_t;
00030
00031
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
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
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
00064 while( 1 )
00065 {
00066 rcvid = MsgReceive(attach->chid, &msg, sizeof(msg), NULL);
00067
00068 if (rcvid == -1)
00069 {
00070 break;
00071 }
00072
00073 if (rcvid == 0)
00074 {
00075 switch (msg.hdr.code)
00076 {
00077 case _PULSE_CODE_DISCONNECT:
00078
00079
00080
00081
00082
00083 ConnectDetach(msg.hdr.scoid);
00084 break;
00085 case _PULSE_CODE_UNBLOCK:
00086
00087
00088
00089
00090
00091 break;
00092 default:
00093
00094
00095
00096
00097
00098 break;
00099 }
00100 continue;
00101 }
00102
00103
00104 if (msg.hdr.type >= _IO_BASE && msg.hdr.type <= _IO_MAX)
00105 {
00106 MsgError(rcvid, ENOSYS);
00107 continue;
00108 }
00109
00110
00111 printf("Server receive %d \n", msg.data);
00112 MsgReply(rcvid, EOK, 0, 0);
00113
00114 }
00115
00116
00117 name_detach(attach, 0);
00118
00119
00120 }
00121
00122 };
00123
00124
00125 class Client : public Basic_PThread
00126 {
00127 public:
00128 Client() : Basic_PThread() { }
00129 virtual ~Client() { }
00130
00131
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
00145 msg.hdr.type = 0x00;
00146 msg.hdr.subtype = 0x00;
00147
00148
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
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
00191 pthread_kill( s.getPID(), SIGKILL );
00192
00193 return 0;
00194 }
00195
00196 #endif