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
00020 typedef struct _pulse msg_header_t;
00021
00022
00023 typedef struct _my_data {
00024 msg_header_t hdr;
00025 int data;
00026 } my_data_t;
00027
00028
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
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
00047 while (1) {
00048 rcvid = MsgReceive(attach->chid, &msg, sizeof(msg), NULL);
00049
00050 if (rcvid == -1) {
00051 break;
00052 }
00053
00054 if (rcvid == 0) {
00055 switch (msg.hdr.code)
00056 {
00057 case _PULSE_CODE_DISCONNECT:
00058
00059
00060
00061
00062
00063 ConnectDetach(msg.hdr.scoid);
00064 break;
00065 case _PULSE_CODE_UNBLOCK:
00066
00067
00068
00069
00070
00071 break;
00072 default:
00073
00074
00075
00076
00077
00078 break;
00079 }
00080 continue;
00081 }
00082
00083
00084 if (msg.hdr.type >= _IO_BASE && msg.hdr.type <= _IO_MAX) {
00085 MsgError(rcvid, ENOSYS);
00086 continue;
00087 }
00088
00089
00090 printf("Server receive %d \n", msg.data);
00091 MsgReply(rcvid, EOK, 0, 0);
00092
00093 }
00094
00095
00096 name_detach(attach, 0);
00097
00098 return NULL;
00099 }
00100
00101
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
00115 msg.hdr.type = 0x00;
00116 msg.hdr.subtype = 0x00;
00117
00118
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
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