Reorganize to remove DIKU specific naming
[remote/remote-mci.git] / protocols / host_server / HostMsg.cc
blobe0a4cc67e333d618f1595a131019fcd0114e2ca4
1 #include "HostMsg.h"
2 #include "macros.h"
3 namespace remote { namespace protocols { namespace host_server {
5 HostMsg::HostMsg(MsgPlugEvent& message)
6 : protocolVersion(HOST_SERVER_PROTOCOL_VERSION),
7 type(HOSTMSGTYPE_PLUGEVENT),
8 message(&message),
9 deleteMsg(false)
13 HostMsg::HostMsg(MsgHostRequest& message)
14 : protocolVersion(HOST_SERVER_PROTOCOL_VERSION),
15 type(HOSTMSGTYPE_HOSTREQUEST),
16 message(&message),
17 deleteMsg(false)
21 HostMsg::HostMsg(MsgHostConfirm& message)
22 : protocolVersion(HOST_SERVER_PROTOCOL_VERSION),
23 type(HOSTMSGTYPE_HOSTCONFIRM),
24 message(&message),
25 deleteMsg(false)
29 HostMsg::HostMsg(uint8_t*& buffer, uint32_t& buflen)
31 buffer = this->read(buffer,buflen);
32 deleteMsg = true;
35 HostMsg::~HostMsg()
37 if (deleteMsg) { delete this->message; }
40 uint32_t HostMsg::getLength()
42 return sizeof(type) + sizeof(protocolVersion) + message->getLength();
45 uint8_t* HostMsg::write(uint8_t* buffer, uint32_t& buflen)
47 buffer = writevalue(protocolVersion,buffer,buflen);
48 buffer = writevalue(type,buffer,buflen);
49 buffer = message->write(buffer,buflen);
50 return buffer;
53 void HostMsg::print(FILE* s)
55 fprintf(s,"MESSAGE HostMsg\n");
56 fprintf(s,"protocolVersion: %u\n",protocolVersion);
57 fprintf(s,"type: ");
58 switch (type)
60 case HOSTMSGTYPE_PLUGEVENT:
61 fprintf(s,"HOSTMSGTYPE_PLUGEVENT\n");
62 break;
63 case HOSTMSGTYPE_HOSTREQUEST:
64 fprintf(s,"HOSTMSGTYPE_HOSTMOTE\n");
65 break;
66 default:
67 fprintf(s,"Invalid type\n");
68 break;
70 message->print(s);
73 uint8_t* HostMsg::read(uint8_t* buffer, uint32_t& buflen)
75 buffer = readvalue(protocolVersion,buffer,buflen);
76 if (protocolVersion != HOST_SERVER_PROTOCOL_VERSION)
78 __THROW__("Host/Server protocol version conflict!");
80 buffer = readvalue(type,buffer,buflen);
81 switch (type)
83 case HOSTMSGTYPE_PLUGEVENT:
84 message = new MsgPlugEvent(buffer,buflen);
85 break;
86 case HOSTMSGTYPE_HOSTREQUEST:
87 message = new MsgHostRequest(buffer,buflen);
88 break;
89 case HOSTMSGTYPE_HOSTCONFIRM:
90 message = new MsgHostConfirm(buffer,buflen);
91 break;
92 default:
93 __THROW__("Unknown type of HostMsg!");
94 break;
96 return buffer;
99 uint32_t HostMsg::getProtocolVersion()
101 return protocolVersion;
104 uint8_t HostMsg::getType()
106 return type;
109 MsgHostRequest& HostMsg::getHostRequest()
111 if (type != HOSTMSGTYPE_HOSTREQUEST)
112 __THROW__ ("Cannot get MsgHostRequest when type is not HOSTMSGTYPE_HOSTREQUEST!");
113 return *((MsgHostRequest*)message);
116 MsgHostConfirm& HostMsg::getHostConfirm()
118 if (type != HOSTMSGTYPE_HOSTCONFIRM)
119 __THROW__ ("Cannot get MsgHostConfirm when type is not HOSTMSGTYPE_HOSTCONFIRM!");
120 return *((MsgHostConfirm*)message);
123 MsgPlugEvent& HostMsg::getPlugEvent()
125 if (type != HOSTMSGTYPE_PLUGEVENT)
126 __THROW__ ("Cannot get plugevent when type is not HOST_PLUGEVENT!");
127 return *((MsgPlugEvent*)message);