/* \n Copyright 1999-2011 (c) My-Channels \n Copyright (c) 2012-2014 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. \n\n Use, reproduction, transfer, publication or disclosure is prohibited except as specifically provided for in your License Agreement with Software AG.\n */\n #include "nSampleApp.h" #include "connectionwatch.h" #include "Watchers.h" #include "nSessionAttributes.h" #include "nRealmNode.h" #include "nLeafNode.h" #include "nServiceNode.h" #include "nConnectionDetails.h" #include #ifdef WIN32 #include #endif using namespace com::pcbsys::nirvana::client; using namespace com::pcbsys::nirvana::nAdminAPI; using namespace com::pcbsys::nirvana::nAdminAPI::apps; /// /// * This class demonstrates how to watch for connections to a realm and be notified of /// * new connections, or connections closing /// connectionwatch::connectionwatch(int argc, char** argv) { try { processArgs(argc, argv); printf("Connecting to %s\n", m_realmUrl.c_str()); // construct the session attributes for the realm nSessionAttributes *pAttr = new nSessionAttributes(m_realmUrl); // construct the real node nRealmNode *pNode = new nRealmNode(pAttr); if (!pNode->isAuthorised()) { printf ("User not authorised on this node %s\n", m_realmUrl.c_str()); return; } // add a new Realm watch instance as a watcher for connections to this root realm // call the method to add listeners to the channel and queue nodes registerNode(pNode); printf ("Press any key to quit !\n"); try { char buffer[80]; gets(buffer); } catch (Exception read) //Ignore this { } pNode->close(); } catch (Exception ex) { // Console.WriteLine(ex.StackTrace); } } void connectionwatch::processArgs(int argc, char** argv) { switch (argc) { case 0: case 1: Usage(); break; case 2: if (strcmp (argv[0], "-?") == 0) { Usage(); break; } getOptions(argc, argv); break; default: getOptions(argc, argv); break; } } /// /// * Set the program variables and flags based on command line args /// void connectionwatch::getOptions(int argc, char** argv) { m_realmUrl = argv[1]; if (m_realmUrl == "") { Usage(); exit(1); } } /// /// * Prints the usage message for this class /// void connectionwatch::Usage() { printf("Usage ...\n"); printf("nconnectionwatch \n"); printf(" - the rname of the server to connect to"); } void connectionwatch::registerNode(nRealmNode *pNode) { pNode->addObserver(this); pNode->addConnectionListener(new nRealmWatch(this)); registerNodes(pNode->getNodes()); } /// /// * Search the enumeration of child nodes, and add realm watchers to all child realms, /// * and watchers to all channel and queue nodes /// void connectionwatch::registerNodes(fSortedList nodes) { for (fSortedList::iterator iterator = nodes.begin(); iterator != nodes.end(); iterator++) { nNode *pNode = iterator->second; int type = pNode->getType (); if (type == fBase::LEAFNODE) { ((nLeafNode*)pNode)->addListener(new nChannelWatch((nLeafNode*)pNode, this)); } else if (type == fBase::CONTAINER) { registerNodes(((nContainer*)pNode)->getNodes()); } else if (type == fBase::SERVICENODE) { nServiceNode *pSNode = (nServiceNode*)pNode; pSNode->addListener(new nServiceWatch(pSNode, this)); } else if (type == fBase::REALMNODE) { // add a new realm watch instance as a watcher for connections to this realm registerNode((nRealmNode*)pNode); } } } void connectionwatch::newConnection(const std::string& target, nConnectionDetails *pCd) { MutexHelper helper (&m_newConnectionMutex); int numSubject = 0; std::string *pSubjects = pCd->getSubject(numSubject); time_t now = time (NULL); struct tm *pTm = localtime (&now); if (numSubject == 1) { printf("%02d:%02d:%02d User [%s] %s: New Connection : %s Subject: %s\n", pTm->tm_hour, pTm->tm_min, pTm->tm_sec, pSubjects[0].c_str(), target.c_str(), pCd->getId().c_str(), pSubjects[0].c_str()); } else { printf("%02d:%02d:%02d User [%s] %s: New Connection : %s\n", pTm->tm_hour, pTm->tm_min, pTm->tm_sec, pSubjects[0].c_str(), target.c_str(), pCd->getId().c_str()); for (int x = 0; x < numSubject; x++) { printf("%02d:%02d:%02d Subject[%d] %s\n", pTm->tm_hour, pTm->tm_min, pTm->tm_sec, x, pSubjects[x].c_str()); } } } void connectionwatch::delConnection(const std::string& target, nConnectionDetails *pCd) { int numSubject = 0; std::string *pSubjects = pCd->getSubject(numSubject); time_t now = time (NULL); struct tm *pTm = localtime (&now); printf("%02d:%02d:%02d User [%s] %s: Del Connection : %s\n", pTm->tm_hour, pTm->tm_min, pTm->tm_sec, pSubjects[0].c_str(), target.c_str(), pCd->getId().c_str()); } void connectionwatch::update(Observable *pObs, void *pObj) { nNode *pNode = (nNode *) pObj; if (pNode == NULL) return; int type = pNode->getType (); try { if (type == fBase::LEAFNODE) { ((nLeafNode*)pNode)->addListener(new nChannelWatch((nLeafNode*)pNode, this)); } else if (type == fBase::CONTAINER) { registerNodes(((nContainer*)pNode)->getNodes()); } else if (type == fBase::SERVICENODE) { nServiceNode *pSNode = (nServiceNode*)pNode; pSNode->addListener(new nServiceWatch(pSNode, this)); } } catch (Exception ex) { // Console.WriteLine(ex.StackTrace); } } int main (int argc, char** argv) { connectionwatch *pSimple = new connectionwatch (argc, argv); return 0; }