From c04dcc2e7d834218ef2d4194331e383402495ae1 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 10 Apr 2024 20:07:22 +0200 Subject: Adding upstream version 2:20.4+dfsg. Signed-off-by: Daniel Baumann --- .../Source/Tests/Messages2/MessagesTest2.cpp | 242 +++++++++++++++++++++ 1 file changed, 242 insertions(+) create mode 100644 lib/libUPnP/Neptune/Source/Tests/Messages2/MessagesTest2.cpp (limited to 'lib/libUPnP/Neptune/Source/Tests/Messages2/MessagesTest2.cpp') diff --git a/lib/libUPnP/Neptune/Source/Tests/Messages2/MessagesTest2.cpp b/lib/libUPnP/Neptune/Source/Tests/Messages2/MessagesTest2.cpp new file mode 100644 index 0000000..26ad82e --- /dev/null +++ b/lib/libUPnP/Neptune/Source/Tests/Messages2/MessagesTest2.cpp @@ -0,0 +1,242 @@ +/***************************************************************** +| +| Messages Test Program 1 +| +| (c) 2001-2008 Gilles Boccon-Gibod +| Author: Gilles Boccon-Gibod (bok@bok.net) +| + ****************************************************************/ +/*---------------------------------------------------------------------- +| includes ++---------------------------------------------------------------------*/ +#include "Neptune.h" +/*---------------------------------------------------------------------- +| TestServerMessageHandler ++---------------------------------------------------------------------*/ +class TestServerMessageHandler +{ +public: + NPT_IMPLEMENT_DYNAMIC_CAST(TestServerMessageHandler) + + // destructor + virtual ~TestServerMessageHandler() {} + + // methods + virtual void OnTestCommand(NPT_MessageReceiver* /*receiver*/, int /*id*/) {} +}; +NPT_DEFINE_DYNAMIC_CAST_ANCHOR(TestServerMessageHandler) + +/*---------------------------------------------------------------------- +| TestServerMessage ++---------------------------------------------------------------------*/ +class TestServerMessage : public NPT_Message +{ +public: + static NPT_Message::Type MessageType; + NPT_Message::Type GetType() { + return MessageType; + } + virtual NPT_Result Deliver(TestServerMessageHandler* handler) = 0; + virtual NPT_Result Dispatch(NPT_MessageHandler* handler) { + TestServerMessageHandler* specific = NPT_DYNAMIC_CAST(TestServerMessageHandler, handler); + if (specific) { + return Deliver(specific); + } else { + return DefaultDeliver(handler); + } + } +}; +NPT_Message::Type TestServerMessage::MessageType = "TestServer Message"; +/*---------------------------------------------------------------------- +| TestServerTestCommandMessage ++---------------------------------------------------------------------*/ +class TestServerTestCommandMessage : public TestServerMessage +{ +public: + TestServerTestCommandMessage(NPT_MessageReceiver* receiver, int id) : + m_Receiver(receiver), m_Id(id) {} + NPT_Result Deliver(TestServerMessageHandler* handler) { + handler->OnTestCommand(m_Receiver, m_Id); + return NPT_SUCCESS; + } + +private: + NPT_MessageReceiver* m_Receiver; + int m_Id; +}; +/*---------------------------------------------------------------------- +| TestClientMessageHandler ++---------------------------------------------------------------------*/ +class TestClientMessageHandler +{ +public: + NPT_IMPLEMENT_DYNAMIC_CAST(TestClientMessageHandler) + + // destructor + virtual ~TestClientMessageHandler() {} + + // methods + virtual void OnReply(int /*id*/) {} +}; +NPT_DEFINE_DYNAMIC_CAST_ANCHOR(TestClientMessageHandler) + +/*---------------------------------------------------------------------- +| TestClientMessage ++---------------------------------------------------------------------*/ +class TestClientMessage : public NPT_Message +{ +public: + static NPT_Message::Type MessageType; + NPT_Message::Type GetType() { + return MessageType; + } + virtual NPT_Result Deliver(TestClientMessageHandler* handler) = 0; + virtual NPT_Result Dispatch(NPT_MessageHandler* handler) { + TestClientMessageHandler* specific = NPT_DYNAMIC_CAST(TestClientMessageHandler, handler); + if (specific) { + return Deliver(specific); + } else { + return DefaultDeliver(handler); + } + } +}; +NPT_Message::Type TestClientMessage::MessageType = "TestClient Message"; +/*---------------------------------------------------------------------- +| TestClientReplyMessage ++---------------------------------------------------------------------*/ +class TestClientReplyMessage : public TestClientMessage +{ +public: + TestClientReplyMessage(int id) : m_Id(id) {} + NPT_Result Deliver(TestClientMessageHandler* handler) { + handler->OnReply(m_Id); + return NPT_SUCCESS; + } +private: + int m_Id; +}; +/*---------------------------------------------------------------------- +| TestServer ++---------------------------------------------------------------------*/ +class TestServer : public NPT_Thread, + public NPT_MessageReceiver, + public NPT_MessageHandler, + public TestServerMessageHandler +{ +public: + TestServer(); + + // message posting wrappers + NPT_Result DoTestCommand(NPT_MessageReceiver* receiver, int id); + // NPT_Runnable methods (from NPT_Thread) + void Run(); + // NPT_TestServerMessageHandler methods + void OnTestCommand(NPT_MessageReceiver* receiver, int id); +private: + // members + NPT_SimpleMessageQueue* m_MessageQueue; +}; +/*---------------------------------------------------------------------- +| TestServer::TestServer ++---------------------------------------------------------------------*/ +TestServer::TestServer() +{ + // create the message queue + m_MessageQueue = new NPT_SimpleMessageQueue(); + + // attach to the message queue + SetQueue(m_MessageQueue); + SetHandler(this); + // start the thread + Start(); +} +/*---------------------------------------------------------------------- +| TestServer::Run ++---------------------------------------------------------------------*/ +void +TestServer::Run() +{ + printf("TestServer::Run - begin\n"); + while (m_MessageQueue->PumpMessage() == NPT_SUCCESS) {}; + printf("TestServer::Run - end\n"); +} +/*---------------------------------------------------------------------- +| TestServer::DoTestCommand ++---------------------------------------------------------------------*/ +NPT_Result +TestServer::DoTestCommand(NPT_MessageReceiver* receiver, int id) +{ + return this->PostMessage(new TestServerTestCommandMessage(receiver, id)); +} +/*---------------------------------------------------------------------- +| TestServer::OnTestCommand ++---------------------------------------------------------------------*/ +void +TestServer::OnTestCommand(NPT_MessageReceiver* receiver, int id) +{ + printf("TestServer::OnTestCommand %d\n", id); + receiver->PostMessage(new TestClientReplyMessage(id+10000)); +} +/*---------------------------------------------------------------------- +| TestClient ++---------------------------------------------------------------------*/ +class TestClient : public NPT_MessageReceiver, + public NPT_MessageHandler, + public TestClientMessageHandler +{ +public: + TestClient(TestServer* server, int id); + + // NPT_TestServerMessageHandler methods + void OnReply(int id); +private: + // members + int m_Id; +}; +/*---------------------------------------------------------------------- +| TestClient::TestClient ++---------------------------------------------------------------------*/ +TestClient::TestClient(TestServer* server, int id) : + m_Id(id) +{ + // set ourself as the message handler + SetHandler(this); + + // send commands to server + server->DoTestCommand(this, 1); + server->DoTestCommand(this, 2); + server->DoTestCommand(this, 3); + server->DoTestCommand(this, 4); +} +/*---------------------------------------------------------------------- +| TestClient::OnReply ++---------------------------------------------------------------------*/ +void +TestClient::OnReply(int id) +{ + printf("TestClient::OnReply (client=%d) %d\n", m_Id, id); +} +/*---------------------------------------------------------------------- +| main ++---------------------------------------------------------------------*/ +int +main(int argc, char** argv) +{ + NPT_COMPILER_UNUSED(argc); + NPT_COMPILER_UNUSED(argv); + printf("MessagesTest2:: start\n"); + TestServer* server = new TestServer(); + TestClient* client1 = new TestClient(server, 1); + TestClient* client2 = new TestClient(server, 2); + NPT_MessageQueue* queue = new NPT_SimpleMessageQueue(); + client1->SetQueue(queue); + client2->SetQueue(queue); + + server->Wait(); + + delete client1; + delete client2; + delete server; + delete queue; + printf("MessagesTest2:: end\n"); +} -- cgit v1.2.3