SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
ProtocolMessageFactory.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include "common/NonCopyable.h"
7 #include "IProtocolMessage.h"
8 #include "AIStateMessage.h"
11 #include "AIPauseMessage.h"
12 #include "AISelectMessage.h"
13 #include "AINamesMessage.h"
14 #include "AIChangeMessage.h"
15 #include "AIStepMessage.h"
16 #include "AIUpdateNodeMessage.h"
17 #include "AIAddNodeMessage.h"
18 #include "AIDeleteNodeMessage.h"
19 
20 namespace ai {
21 
23 private:
24  uint8_t *_aiState;
25  uint8_t *_aiSelect;
26  uint8_t *_aiPause;
27  uint8_t *_aiNames;
28  uint8_t *_aiChange;
29  uint8_t *_aiReset;
30  uint8_t *_aiStep;
31  uint8_t *_aiPing;
32  uint8_t *_aiCharacterDetails;
33  uint8_t *_aiCharacterStatic;
34  uint8_t *_aiUpdateNode;
35  uint8_t *_aiAddNode;
36  uint8_t *_aiDeleteNode;
37 
39  _aiState(new uint8_t[sizeof(AIStateMessage)]),
40  _aiSelect(new uint8_t[sizeof(AISelectMessage)]),
41  _aiPause(new uint8_t[sizeof(AIPauseMessage)]),
42  _aiNames(new uint8_t[sizeof(AINamesMessage)]),
43  _aiChange(new uint8_t[sizeof(AIChangeMessage)]),
44  _aiReset(new uint8_t[sizeof(AIResetMessage)]),
45  _aiStep(new uint8_t[sizeof(AIStepMessage)]),
46  _aiPing(new uint8_t[sizeof(AIPingMessage)]),
47  _aiCharacterDetails(new uint8_t[sizeof(AICharacterDetailsMessage)]),
48  _aiCharacterStatic(new uint8_t[sizeof(AICharacterStaticMessage)]),
49  _aiUpdateNode(new uint8_t[sizeof(AIUpdateNodeMessage)]),
50  _aiAddNode(new uint8_t[sizeof(AIAddNodeMessage)]),
51  _aiDeleteNode(new uint8_t[sizeof(AIDeleteNodeMessage)]) {
52  }
53 public:
55  delete[] _aiState;
56  delete[] _aiSelect;
57  delete[] _aiPause;
58  delete[] _aiNames;
59  delete[] _aiChange;
60  delete[] _aiReset;
61  delete[] _aiStep;
62  delete[] _aiPing;
63  delete[] _aiCharacterDetails;
64  delete[] _aiCharacterStatic;
65  delete[] _aiUpdateNode;
66  delete[] _aiAddNode;
67  delete[] _aiDeleteNode;
68  }
69 
70  static ProtocolMessageFactory& get() {
71  static ProtocolMessageFactory _instance;
72  return _instance;
73  }
74 
78  inline bool isNewMessageAvailable(const streamContainer& in) const {
79  const int32_t size = IProtocolMessage::peekInt(in);
80  if (size == -1) {
81  // not enough data yet, wait a little bit more
82  return false;
83  }
84  const int streamSize = static_cast<int>(in.size() - sizeof(int32_t));
85  if (size > streamSize) {
86  // not enough data yet, wait a little bit more
87  return false;
88  }
89  return true;
90  }
91 
96  IProtocolMessage *create(streamContainer& in) {
97  // remove the size from the stream
98  in.erase(in.begin(), std::next(in.begin(), sizeof(int32_t)));
99  // get the message type
100  const uint8_t type = in.front();
101  in.pop_front();
102  if (type == PROTO_STATE) {
103  return new (_aiState) AIStateMessage(in);
104  } else if (type == PROTO_SELECT) {
105  return new (_aiSelect) AISelectMessage(in);
106  } else if (type == PROTO_PAUSE) {
107  return new (_aiPause) AIPauseMessage(in);
108  } else if (type == PROTO_NAMES) {
109  return new (_aiNames) AINamesMessage(in);
110  } else if (type == PROTO_CHANGE) {
111  return new (_aiChange) AIChangeMessage(in);
112  } else if (type == PROTO_RESET) {
113  return new (_aiReset) AIResetMessage();
114  } else if (type == PROTO_STEP) {
115  return new (_aiStep) AIStepMessage(in);
116  } else if (type == PROTO_PING) {
117  return new (_aiPing) AIPingMessage();
118  } else if (type == PROTO_CHARACTER_DETAILS) {
119  return new (_aiCharacterDetails) AICharacterDetailsMessage(in);
120  } else if (type == PROTO_CHARACTER_STATIC) {
121  return new (_aiCharacterStatic) AICharacterStaticMessage(in);
122  } else if (type == PROTO_UPDATENODE) {
123  return new (_aiUpdateNode) AIUpdateNodeMessage(in);
124  } else if (type == PROTO_ADDNODE) {
125  return new (_aiAddNode) AIAddNodeMessage(in);
126  } else if (type == PROTO_DELETENODE) {
127  return new (_aiDeleteNode) AIDeleteNodeMessage(in);
128  }
129 
130  return nullptr;
131  }
132 };
133 
134 }
Message for the remote debugging interface.
Definition: AINamesMessage.h:15
Definition: ProtocolMessageFactory.h:22
bool isNewMessageAvailable(const streamContainer &in) const
Checks whether a new message is available in the stream.
Definition: ProtocolMessageFactory.h:78
A protocol message is used for the serialization of the ai states for remote debugging.
Definition: IProtocolMessage.h:60
IProtocolMessage * create(streamContainer &in)
Call this only if isNewMessageAvailable returned true on the same streamContainer before! ...
Definition: ProtocolMessageFactory.h:96
Message for the remote debugging interface.
Definition: AIPauseMessage.h:20
Definition: AICharacterStaticMessage.h:11
Message for the remote debugging interface.
Definition: AICharacterDetailsMessage.h:17
Message for the remote debugging interface.
Definition: AIDeleteNodeMessage.h:16
Message for the remote debugging interface.
Definition: AIChangeMessage.h:16
Message for the remote debugging interface.
Definition: AISelectMessage.h:16
Perform one step if the ai controlled entities are in paused mode.
Definition: AIStepMessage.h:15
Message for the remote debugging interface.
Definition: AIAddNodeMessage.h:16
Definition: NonCopyable.h:8
Message for the remote debugging interface.
Definition: AIUpdateNodeMessage.h:16
Message for the remote debugging interface.
Definition: AIStateMessage.h:17