/* * ---------------------------------------------------------------------------------- * * Copyright (c) 2012–2015 Software AG, Darmstadt, Germany and/or * Software AG USA Inc., Reston, VA, USA, and/or its subsidiaries * and/or its affiliates and/or their licensors. Use, reproduction, * transfer, publication or disclosure is prohibited except as specifically * provided for in your License Agreement with Software AG. * *   In the event that you should download or otherwise use this software *   you hereby acknowledge and agree to the terms at *   http://www.my-channels.com/company/terms.html#legalnotices * */ #include "Exception.h" #include "nSampleApp.h" #include "nConstants.h" #include "nEventService.h" #include "nServiceFactory.h" #include "nSessionAttributes.h" #include "nServerService.h" #include "nConsumeEvent.h" #include "nEventProperties.h" #include "Poco/Thread.h" #include #include using namespace com::pcbsys::nirvana::client; using namespace com::pcbsys::nirvana::client::p2p; namespace com { namespace pcbsys { namespace nirvana { namespace apps { namespace nP2PEcho { class nP2PEcho { /** Class that creates a P2P server service that simply echos whatever is typed into the client. See Usage() for more info. */ private: static nP2PEcho *m_pSelf; static void processArgs(int argc, char** argv) { switch (argc) { case 1: Usage(); exit (1); break; } } public: static int Main(int argc, char** argv) { //Process command line arguments processArgs(argc, argv); // Get the realm details std::string RNAME = argv[1]; if (RNAME.length() == 0) { Usage(); return 1; } // Trim the array std::string *pRpropertiesTrimmed = new std::string[1]; pRpropertiesTrimmed[0] = RNAME; m_pSelf = new nP2PEcho(pRpropertiesTrimmed, argc > 2); return 0; } nP2PEcho(std::string *pRprops, bool isServer) { if (pRprops == NULL) return; nServiceFactory *pFactory = NULL; try { nSessionAttributes *pNsa = NULL; try { pNsa = new nSessionAttributes(*pRprops); } catch (Exception ex) { printf("Error creating Session Attributes. Please check your RNAME\n"); return; } // // Create an instance of a Service Factory // printf("Creating factory\n"); pFactory = new nServiceFactory(pNsa); if (isServer) { printf("Running as server\n"); nServerService *pServer = pFactory->createEventService("echo", "Echo Server. sends all messages back to the sender"); while (true) { nEventService *pServ = (nEventService*)pServer->accept(); handler *pHd = new handler(pServ); Poco::Thread *pNewThread = new Poco::Thread("P2P"); pNewThread->start(*pHd); } } else { // // Find the service by name // printf("Getting list of services\n"); nServiceInfo *pInfo = NULL; while (pInfo == NULL) { pInfo = pFactory->findService("echo"); if (pInfo == NULL) { Poco::Thread::sleep(300); } } // // now connect to this service // nEventService *pServ = (nEventService*)pFactory->connectToService(pInfo); printf("Connected to service\n"); // // Now just read from the input stream and write to the service // dumpStream *pDs = new dumpStream(pServ); Poco::Thread *pNewThread = new Poco::Thread("P2P"); pNewThread->start(*pDs); while (true) { unsigned int length = 0; std::string line; std::getline(std::cin,line); unsigned char *pLine = nConstants::encode(line, length); nEventProperties *pProps = new nEventProperties(); pServ->write(new nConsumeEvent(pProps, pLine, length)); } } } catch (Exception ex) { try { pFactory->close(); } catch (Exception ex1) { } printf (ex.displayText().c_str()); } } public: class handler : public Poco::Runnable { private: nEventService *m_pService; public: handler(nEventService *pServ) : m_pService(pServ) { } void run() { try { while (true) { nConsumeEvent *pEvt = m_pService->read(); m_pService->write(pEvt); } } catch (Exception ex) { } } }; class dumpStream : public Poco::Runnable { private: nEventService *m_pIs; public: dumpStream(nEventService *pEis) : m_pIs(pEis) { } void run() { try { while (true) { nConsumeEvent *pEvt = m_pIs->read(); std::string strData; nConstants::decode (pEvt->getEventData(), pEvt->getEventDataLength(), strData); printf ("%s\n", strData.c_str()); } } catch (Exception ex) { } } }; // End of dumpStream Inner Class /** * Prints the usage string for this class */ private: static void Usage() { printf("Usage ...\n\n"); printf("nP2PEcho [server]\n\n"); printf(" \n\n"); printf(" - the rname of the server to connect to\n"); printf("\n[Optional Arguments] \n\n"); printf("[server] - write 'server' to run echo server, or leave out to run echo client\n"); } }; } } } } } using namespace com::pcbsys::nirvana::apps::nP2PEcho; nP2PEcho* nP2PEcho::m_pSelf = NULL; int main (int argc, char** argv) { return nP2PEcho::Main (argc, argv); }