Skip to content

Commit

Permalink
Merge branch 'com_module_binary_protocol'
Browse files Browse the repository at this point in the history
Conflicts:
	commodule.cpp
  • Loading branch information
Minoru committed May 10, 2012
2 parents 46704eb + 429ed68 commit a7af5fe
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
35 changes: 23 additions & 12 deletions commodule.cpp
Expand Up @@ -88,7 +88,7 @@ void ComModule::handleMessage()
* then put the message into the queue */
msg->num = seq_num;
msg->port = port;
msg->type = (MessageType)msg_type;
msg->type = static_cast<MessageType>(msg_type);

// FIXME: is std::queue thread-safe? Should we use mutex here?
messageQueue->push(msg);
Expand All @@ -103,30 +103,41 @@ void ComModule::sendMessage(Message *msg)
QDataStream stream(&datagram, QIODevice::WriteOnly);

// version: 1
stream << (quint8)1;
stream << static_cast<quint8>(1);
// other header infortmation
stream << (quint32)msg->num << (quint16)msg->port << (quint8)msg->type;
stream << static_cast<quint32>(msg->num) << static_cast<quint16>(msg->port)
<< static_cast<quint8>(msg->type);

switch(msg->type) {
case MsgAcknowledge:
// "acknowledge" doesn't have any payload
break;
case MsgStart:
break;
case MsgPause:
break;
case MsgStop:
break;
case MsgBump:
stream << (quint32)((MessageBump *)msg)->coordX
<< (quint32)((MessageBump *)msg)->coordY;
{
MessageBump *m = static_cast<MessageBump *>(msg);
stream << static_cast<quint32>(m->coordX) << static_cast<quint32>(m->coordY);
};
break;
case MsgThereYouSee:
MessageThereYouSee *m = (MessageThereYouSee *)msg;
quint32 count = (quint32)m->objects.size();
{
MessageThereYouSee *m = static_cast<MessageThereYouSee *>(msg);
quint32 count = static_cast<quint32>(m->objects.size());
stream << count;
/* For each object, put its description into the stream */
for(quint32 i = 0; i < count; i++) {
MessageObject o = m->objects.front();
m->objects.pop_front();
stream << (quint32)o.coordX << (quint32)o.coordY
<< (quint32)o.diameter << (quint32)(o.degrees / 3600)
<< (quint8)o.red << (quint8)o.green << (quint8)o.blue;
MessageObject o = m->objects.takeFirst();
stream << static_cast<quint32>(o.coordX) << static_cast<quint32>(o.coordY)
<< static_cast<quint32>(o.diameter) << static_cast<quint32>(o.degrees / 3600)
<< static_cast<quint8>(o.red) << static_cast<quint8>(o.green)
<< static_cast<quint8>(o.blue);
}
};
break;
}

Expand Down
28 changes: 22 additions & 6 deletions docs/BinaryNetworkProtocol.markdown
Expand Up @@ -23,7 +23,7 @@ be either some dull message (see "`acknowledge` message") or answer to
the question that agent asked (see "`there you see` message" and "`bump`
message").

There are 9 types of messages:
There are 12 types of messages:

* `move`
* `turn`
Expand All @@ -34,9 +34,12 @@ There are 9 types of messages:
* `acknowledge`
* `bump`
* `there you see`
* `start`
* `pause`
* `stop`

Out of those, only first 6 can be sent by agent, and the last three
can be sent only by the simulator.
Out of those, only first 6 can be sent by agent, and the last six can
be sent only by the simulator.

Following are formal specification of how does header and each message
type look like.
Expand Down Expand Up @@ -68,6 +71,9 @@ Message types are mapped from names to numbers as follows:
* 6: `bump`
* 7: `there you see`
* 8: `parameter report`
* 9: `start`
* 10: `pause`
* 11: `stop`

## `move` message

Expand All @@ -86,9 +92,9 @@ Message contains:

* seconds, *4 octets*, signed integer

seconds are 1/60 of minute, and minute is in turn 1/60 of degree. That
field specify number of seconds agent should turn clockwise. To turn
counterclockwise, one should specify negative number of seconds.
second is 1/60 of minute, and minute is, in turn, 1/60 of degree. That
field specify new orientation of the agent. It is an absolute value
counted from the north direction, which is at the top of the map.

## `change size` message

Expand Down Expand Up @@ -178,6 +184,16 @@ Seconds field indidates orientation of object.

List of objects is just a stream of objects descriptions.

## `start`, `pause` and `stop` messages

Those are used to control simulation flow. Agent program should start
doing its work (i.e. moving agent around) upon receiving `start`
message. If `pause` is sent, agent should pause and wait for the
`start`. The only difference between `pause` and `stop` is that
pausing doesn't erase current agent's state, while `stop` does.

Those messages doesn't contain anything other than header.

## Example messages

### `move` message
Expand Down
4 changes: 3 additions & 1 deletion messages.h
Expand Up @@ -17,7 +17,9 @@ enum MessageType {
MsgBump,
MsgThereYouSee,
MsgParameterReport,
MsgUndefined
MsgStart,
MsgPause,
MsgStop
};

class Message
Expand Down

0 comments on commit a7af5fe

Please sign in to comment.