00001 #ifndef __J2K__MsgThread_CPP__
00002 #define __J2K__MsgThread_CPP__
00003
00004 #include <j2k/nto/MsgThread.hpp>
00005
00006 MsgThread::MsgThread( MsgNameSpaceUserType_t user0, MsgNameSpace* n0, const char* name = NULL )
00007 : Basic_PThread(), user( user0 )
00008 {
00009 n = n0;
00010 m = new Message();
00011
00012 if ( n == NULL )
00013 {
00014 printf( "Invalid NameSpace. The NameSpace provided is NULL. \n" );
00015 exit( 1 );
00016 }
00017
00018 if ( user == client )
00019 {
00020 printf( "Client \n" );
00021 fd = n->Nopen();
00022 }
00023 else if ( user == server )
00024 {
00025 printf( "Server \n" );
00026
00027 if ( name == NULL )
00028 {
00029 printf( "Using default NameSpace Attach Point alias name. \n " );
00030 fd = n->Nattach();
00031 }
00032 else
00033 {
00034 fd = n->Nattach( name );
00035 }
00036 }
00037 else
00038 {
00039 printf( "Unknown NameSpace User type. \n " );
00040 exit( 1 );
00041 }
00042 }
00043
00044
00045 MsgThread::~MsgThread()
00046 {
00047 if ( m != NULL )
00048 {
00049 delete m;
00050 }
00051
00052 if ( n != NULL )
00053 {
00054 if ( user == client )
00055 {
00056 n->Nclose( fd );
00057 }
00058 else if ( user == server )
00059 {
00060 n->Ndetach();
00061 }
00062 }
00063 }
00064
00065 int MsgThread::handleReceive( int rcvid )
00066 {
00067 if ( rcvid != -1 )
00068 {
00069 return -10;
00070 }
00071
00072 register int r = m->handleHeader( rcvid );
00073
00074 if ( r != 0 ) return r;
00075
00076
00077 printf( "Server receive %d \n", m->getMessageNumber() );
00078 MsgReply( rcvid, EOK, 0, 0 );
00079
00080 return 0;
00081 }
00082
00083 int MsgThread::receive()
00084 {
00085 register int rcvid = MsgReceive( n.getChannel(), m->getMessageAddr(), m->getMessageSize(), NULL );
00086 return rcvid;
00087 }
00088
00089 void MsgThread::send( int data )
00090 {
00091 m->setMessageNumber( data );
00092 register int err = MsgSend( fd, m->getMessageAddr(), m->getMessageSize(), NULL );
00093
00094 if ( err == -1 )
00095 {
00096 switch( errno )
00097 {
00098 case EBADF:
00099 printf( "The connection indicated by coid is no longer connected to a channel, \n" );
00100 printf( "or the connection indicated by coid doesn't exist. The channel may have been terminated by the server, \n" );
00101 printf( "or the network manager if it failed to respond to multiple polls. \n" );
00102 break;
00103 case EFAULT:
00104 printf( "A fault occurred when the kernel tried to access the buffers provided. \n" );
00105 printf( "This may have occurred on the receive or the reply. \n" );
00106 break;
00107 case EINTR:
00108 printf( "The call was interrupted by a signal. \n" );
00109 break;
00110 case ESRVRFAULT:
00111 printf( "A fault occurred in a server's address space when accessing the server's message buffers. \n" );
00112 break;
00113 case ETIMEDOUT:
00114 printf( "A kernel timeout unblocked the call. See TimerTimeout(). \n" );
00115 break;
00116 default:
00117 printf( "Unknown error %d: %s\n", errno, strerror( errno ) );
00118 break;
00119 }
00120 }
00121 }
00122
00123 void MsgThread::reply( int rcvid, int data )
00124 {
00125 m->setMessageNumber( data );
00126
00127 register int err = MsgReply( rcvid, EOK, m->getMessageAddr(), m->getMessageSize() );
00128 if ( err == -1 )
00129 {
00130 switch( error )
00131 {
00132 case EFAULT:
00133 printf( "A fault occurred in the sender's address space when a server tried to access the sender's return message buffers. \n" );
00134 break;
00135 case ESRCH:
00136 printf( "The thread indicated by rcvid doesn't exist, or is no longer REPLY-blocked on the channel, or the connection is detached. \n" );
00137 break;
00138 case ESRVRFAULT:
00139 printf( "A fault occurred when the kernel tried to access the buffers provided. \n" );
00140 break;
00141 default:
00142 printf( "Unknown error occured. \n" );
00143 break;
00144 }
00145 exit( 1 );
00146 }
00147 }
00148
00149 #endif