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 #ifndef __MESSAGE__ 00024 #define __MESSAGE__ 00025 00026 #include <string> 00027 00028 00029 enum MessageType{ 00030 messagePing=0, 00031 messageNewCylinder=1, 00032 messageGrow=2, 00033 messageSendEnergy=3, 00034 messageMove=4, 00035 messageDrop=5, 00036 messageSendMessage=6, 00037 messageReceiveMessage=7, 00038 messageAskForSense=8, 00039 messageAskforMigrationInfo=9, 00040 messageNewAgent=10, 00041 messageLog=11, 00042 messageExit=12, 00043 messageEndOfTurn=13 00044 }; 00045 00046 00047 /** The message struct, all client messages are in this format. The 00048 sizes are specified, and even on machines with bigger word 00049 sizes, there is no problem just because I read the stream byte by 00050 byte and translate it. On these machines the only problem is that 00051 they waste memory. */ 00052 class Message{ 00053 public: 00054 /** The organism that send the message. 32 bits */ 00055 unsigned long int organismId; 00056 /** If it's a petition or a response. 1 bit */ 00057 bool petition; 00058 /** The message type. 15 bits */ 00059 MessageType type; 00060 /** size of the message. 32 bits. */ 00061 unsigned long int size; 00062 /** the message itself. size of message *bytes* */ 00063 union{ 00064 /// The message coded in a char * 00065 char *msg; 00066 /// The messahe coded in a unsigned char * 00067 unsigned char *umsg; 00068 }; 00069 00070 /// Construnct a message from the stream 00071 Message(ost::TCPStream *stream); 00072 /// Construct a message with the specified parameters 00073 Message(unsigned long int _organismId, 00074 bool _petition, MessageType _type, unsigned long int _size, 00075 const char *_msg); 00076 /// Destroy a message. If the msg pointer is not null, it's deleted. 00077 ~Message(); 00078 00079 /// Sends the message thought the stream 00080 const void sendTo(ost::TCPStream *stream); 00081 00082 /// Converts the message to a string 00083 string toString(); 00084 }; 00085 00086 #endif