/* * * Copyright (c) 1999 - 2011 my-Channels Ltd * Copyright (c) 2012 - 2017 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. * */ #include "nSampleApp.h" #include "connectionwatch.h" #include "Watchers.h" #include "nSessionAttributes.h" #include "nRealmNode.h" #include "nLeafNode.h" #include "nConnectionDetails.h" #include #include 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"); std::cin.ignore(); pNode->close(); } catch (Exception ex) { } } 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(); } } /// /// * 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"); exit(1); } 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::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()); } } catch (Exception ex) { } } int main (int argc, char** argv) { connectionwatch (argc, argv); return 0; }