00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <socket.h>
00024 #include "message.h"
00025 #include "log.h"
00026 #include <string.h>
00027
00028
00029
00030
00031
00032 Message::Message(unsigned long int _organismId,
00033 bool _petition, MessageType _type, unsigned long int _size,
00034 const char *_msg){
00035 organismId=_organismId;
00036 petition=_petition; type=_type;
00037 size=_size;
00038 if (size!=0){
00039 msg=new char[size];
00040 if (NULL==msg)
00041 throw ost::Exception("Can't Allocate Memory for a Message");
00042 memcpy(msg,_msg,size);
00043 }
00044 else
00045 msg=NULL;
00046 }
00047
00048
00049
00050
00051
00052 Message::Message(ost::TCPStream *stream){
00053 unsigned char c;
00054
00055 organismId=0;
00056 stream->read(&c,1); organismId|=c<<24; stream->read(&c,1); organismId|=c<<16;
00057 stream->read(&c,1); organismId|=c<<8; stream->read(&c,1); organismId|=c;
00058
00059 unsigned short int t;
00060 t=0;
00061 stream->read(&c,1); t|=c<<8; stream->read(&c,1); t|=c;
00062 petition=(t&0x08000);
00063 t&=0x07FFF;
00064 type=MessageType(t);
00065
00066 size=0;
00067 stream->read(&c,1); size|=c<<24; stream->read(&c,1); size|=c<<16;
00068 stream->read(&c,1); size|=c<<8;
00069
00070 int C=stream->get();
00071 if (C<0)
00072 throw ost::Exception("Can't read data!");
00073 c=C;
00074 size|=c;
00075
00076
00077
00078 if (size==0)
00079 msg=NULL;
00080 else{
00081 msg=new char[size];
00082 if (NULL==msg) throw ost::Exception("Can't Allocate Memory for a Message");
00083 stream->read(msg, size);
00084 }
00085
00086 }
00087
00088
00089
00090
00091
00092 Message::~Message(){
00093 if (msg!=NULL) delete msg;
00094 };
00095
00096 #include <stdio.h>
00097
00098
00099
00100
00101 string Message::toString(){
00102 char a[256];
00103
00104 sprintf(a,"organismId: %ld petition: %d type: %d size: %ld", organismId, petition, type, size);
00105
00106 return string(a);
00107 }
00108
00109
00110
00111
00112 const void Message::sendTo(ost::TCPStream *stream){
00113 unsigned char c;
00114
00115
00116 c=(organismId>>24)&0x0FF; stream->write(&c,1);
00117 c=(organismId>>16)&0x0FF; stream->write(&c,1);
00118 c=(organismId>>8)&0x0FF; stream->write(&c,1);
00119 c=(organismId)&0x0FF; stream->write(&c,1);
00120
00121 c=((petition==true)<<7)|((type>>8)&0x07F);
00122 stream->write(&c,1);
00123 c=(type)&0x0FF; stream->write(&c,1);
00124
00125 c=(size>>24)&0x0FF; stream->write(&c,1);
00126 c=(size>>16)&0x0FF; stream->write(&c,1);
00127 c=(size>>8)&0x0FF; stream->write(&c,1);
00128 c=(size)&0x0FF; stream->write(&c,1);
00129
00130 if (size!=0)
00131 stream->write(msg, size);
00132 }