50 likes | 198 Views
EIToolkit stub doc. main.cpp file. int main( int argc , char* argv []) { EIProperties :: UseStandards (); // create the stub ExampleStub stub; stub.Start (); printf ( "Press ESC to quit.<br><br>"); while (_ getch () != 27); printf ( "Cleaning up ...<br>"); stub.Stop ();
E N D
main.cpp file int main(intargc, char* argv[]) { EIProperties::UseStandards(); // create the stub ExampleStub stub; stub.Start(); printf("Press ESC to quit.\n\n"); while (_getch() != 27); printf("Cleaning up ...\n"); stub.Stop(); EIProperties::StopStandards(); return 0; } The main method should not contain much more than the example shows. A stub might be created from another application and then the main method will not be called. Put most initialisation code into the constructor of the stub or its Start() method. Wait for the user to press ESC. (In Java, this is not possible – need to wait for ENTER)
Main Compiler Options • The following defines can be made globally in the Compiler options dialog in VStudio (or by using the /D compiler option) #define _NO_LISTENERS // if you don't need to listen to (UDP) packets #define _NO_SENDERS // if you don't need to send (UDP) packets #define _NO_PARTICLE_COMM // if you don't need particle communication • In Java, use the following (static) boolean variables: • EIProperties.NO_SENDERS = …; • EIProperties.NO_LISTENERS = …;
ExampleStub.cpp file Code that is executed once. The APPNAME can be retrieved using GetName() ExampleStub::ExampleStub(void) : APPNAME("ExampleStub") { } ExampleStub::~ExampleStub(void) { ExampleStub::Stop(); } void ExampleStub::Start() { Stub::Start(); // register the stub to receive messages EIProperties::GetPacketReceiver()->AddPacketObserver(this); } void ExampleStub::Stop() { Stub::Stop(); } In Java, one instead overrides method getName(). If not, the classname is used as default. public String getName() { return "FacebookStub"; } Code that is executed right before the stub is closed. Code that is executed when the stub is activated (most probably only once when started ...) If you want to receive toolkit messages, register yourself here. Code that is executed when the stub is deactivated (most probably only once when exited ...) In Java, these method calls look like this: EIProperties.getPacketReceiver().addPacketObserver(this);
void ExampleStub::PacketReceived(PacketEvent &pktEvent) { Packet *pkt = pktEvent.GetPacket(); std::string msg; pkt->ToString(&msg); std::string *recvId = pkt->GetReceiverId(); // e.g.: only accept message if directed to all (GetWildcardAll()) or specifically to this stub (GetName()) if (*recvId == GetName() || *recvId == EIProperties::GetPacketFormatter()->GetWildcardAll()) { } // TODO react to packet or msg } // Example of how to send a message as UDP void ExampleStub::SendMessageToID(const char *msg, const char *id) { std::string message = GetName() + "::" + std::string(msg) + ":" + std::string(id); Packet packet(message); EIProperties::GetPacketSender()->SendPacket(&packet); } This method is automatically called whenever a toolkit message has arrived (if you have previously registered yourself) The GetWildcardAll() method gives the placeholder used to denote ‚everything‘ – currently the „*“ star To send a message, build a packet (sender, message type, message content, receiver) and use GetPacketSender()->SendPacket to send it (one could use the GetWildcardAll() placeholder for a placeholder meaning e.g. this packet is meant for ‚all‘)