Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

message.cc

Go to the documentation of this file.
00001 /*
00002   svas_server -- virtual World Server of Svas
00003   Copyright (c) 2001, 2002 David Moreno Montero
00004  
00005  
00006   This program is free software; you can redistribute it and/or modify
00007   it under the terms of the GNU General Public License as published by
00008   the Free Software Foundation; either version 2 of the License, or
00009   (at your option) any later version.
00010  
00011   This program is distributed in the hope that it will be useful, but
00012   WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014   General Public License for more details.
00015  
00016   You should have received a copy of the GNU General Public License
00017   along with this program; if not, write to the Free Software
00018   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00019   02111-1307, USA.  
00020 
00021 */
00022 
00023 #include <socket.h>
00024 #include "message.h"
00025 #include "log.h"
00026 #include <string.h>
00027 
00028 /**
00029  * Create a Message. Be carfull as the msg pointer is memcopied, it's
00030  * slower but safer.
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  * Create a message from a stream, the data is copied, and deleted
00050  * when the message is deleted, so make your own copy!
00051  */
00052 Message::Message(ost::TCPStream *stream){
00053   unsigned char c;
00054   /* Now the organismId */
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   /* Now the message type and if it's a petition or a response */
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   /* Size of the message */
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;     //stream->read(&c,1);  size|=c;
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   /* The message itself */
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   //log <<"got "<<type<<endl;
00086 }
00087 
00088 /**
00089  * Destroys the message. Just delete the msg pointer if it points to
00090  * any data
00091  */
00092 Message::~Message(){
00093   if (msg!=NULL) delete msg;
00094 };
00095 
00096 #include <stdio.h>
00097 
00098 /**
00099  * Convert the message to a printable string
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  * Sends the message thought the stream
00111  */
00112 const void Message::sendTo(ost::TCPStream *stream){
00113   unsigned char c;
00114   //log <<"send "<<type<<endl;
00115   /* First the organismId */
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   /* Now the message type and if it's a petition or a response */
00121   c=((petition==true)<<7)|((type>>8)&0x07F);
00122   stream->write(&c,1);
00123   c=(type)&0x0FF; stream->write(&c,1);
00124   /* Size of the message */
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   /* The message itself */
00130   if (size!=0)
00131     stream->write(msg, size);
00132 }

Generated on Mon Jun 17 19:53:44 2002 for Svas Server by doxygen1.2.16