/* * * 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 "nChannelAttributes.h" #include "nChannel.h" #include "nSession.h" #include "nSessionFactory.h" #include "nChannelNotFoundException.h" #include "nSessionNotConnectedException.h" #include "nRequestTimedOutException.h" #include "nUnexpectedResponseException.h" #include "nUnknownRemoteRealmException.h" #include "nChannelAlreadyExistsException.h" #include "nSecurityException.h" using namespace com::pcbsys::nirvana::apps; using namespace com::pcbsys::nirvana::client; class makechan : nSampleApp { /** * Creates a nirvana channel */ private: static makechan *m_pSelf; /** * This method demonstrates the Nirvana API calls necessary to create * a channel. * It is called after all command line arguments have been received and * validated * * @param realmDetails a String[] containing the possible RNAME values * @param nRealmDetail length of the rname array * @param achannelName the channel name to create * @param age the time to live for each event published to each channel * @param cap the maximum number fo events to hold within the event in a First in First out policy * @param type the type of channel to create (see Usage()) * @param cluster whether the channel is cluster wide * @eid the starting eid to begin the channel from */ void doit(std::string *pRealmDetails, int nRealmDetail, std::string& achannelName, int age, int cap, std::string& type, bool cluster, long eid) { m_pSelf->constructSession(pRealmDetails, nRealmDetail); //Creates the specified channel try { //Create a channel attributes object nChannelAttributes *pNca = createChannelAttributes(achannelName, age, cap, type, cluster); //Create the channel nChannel *pChannel = m_pSession->createChannel(pNca, eid); if (pChannel) delete pChannel; if (pNca) delete pNca; } //Handle errors catch (nChannelNotFoundException cnfe) { printf("The channel specified could not be found.\n"); printf("Please ensure that the channel exists in the REALM you connect to.\n"); exit(1); } catch (nSecurityException se) { printf("Unsufficient permissions for the requested operation.\n"); printf("Please check the ACL settings on the server.\n"); exit(1); } catch (nSessionNotConnectedException snce) { printf("The session object used is not physically connected to the Nirvana realm.\n"); printf("Please ensure the realm is up and check your RNAME value.\n"); exit(1); } catch (nUnexpectedResponseException ure) { printf("The Nirvana REALM has returned an unexpected response.\n"); printf("Please ensure the Nirvana REALM and client API used are compatible.\n"); exit(1); } catch (nUnknownRemoteRealmException urre) { printf("The channel specified resided in a remote realm which could not be found.\n"); printf("Please ensure the channel name specified is correct.\n"); exit(1); } catch (nRequestTimedOutException rtoe) { printf("The requested operation has timed out waiting for a response from the REALM.\n"); printf("If this is a very busy REALM ask your administrator to increase the client timeout values.\n"); exit(1); } catch (nChannelAlreadyExistsException caee) { printf("The channel specified already exists on the REALM.\n"); exit(1); } catch (nBaseClientException nbce) { printf("An error occured while creating the Channel Attributes object.\n"); exit(1); } //Close the session we opened try { nSessionFactory::close(m_pSession); } catch (Exception ex) { } //Close any other sessions within this program so that we can exit nSessionFactory::shutdown(); } private: nChannelAttributes* createChannelAttributes(std::string& achannelName, int age, int cap, std::string& type, bool cluster) { //Create a channel attributes object nChannelAttributes *pNca = new nChannelAttributes(); //Set the channel name pNca->setName(achannelName); //Set the channel type parameter (reliable or guaranteed) if (!type.empty()) { if (!type.compare("R")) { pNca->setType(nChannelAttributes::RELIABLE_TYPE); } else if (!type.compare("P")) { pNca->setType(nChannelAttributes::PERSISTENT_TYPE); } else if (!type.compare("M")) { pNca->setType(nChannelAttributes::MIXED_TYPE); } else if (!type.compare("S")) { pNca->setType(nChannelAttributes::SIMPLE_TYPE); } else if (!type.compare("T")) { pNca->setType(nChannelAttributes::TRANSIENT_TYPE); } else if (!type.compare("O")) { pNca->setType(nChannelAttributes::OFF_HEAP_TYPE); } else if (!type.compare("G")) { pNca->setType(nChannelAttributes::PAGE_TYPE); } } else { pNca->setType(nChannelAttributes::SIMPLE_TYPE); } //Set the channel capacity parameter pNca->setMaxEvents(cap); //Set the channel Time to Live parameter pNca->setTTL(age); // set cluster wide flag pNca->setClusterWide(cluster); return pNca; } /** * Prints the usage message for this class */ private: static void Usage() { printf("Usage ...\n\n"); printf("makechan [time to live] [capacity] [type] [cluster wide] [start eid] \n\n"); printf(" \n\n"); printf(" - the rname of the server to connect to\n"); printf(" - Channel name parameter for the channel to be created\n"); printf("\n[Optional Arguments] \n\n"); printf("[time to live] - The Time To Live parameter for the new channel (default: 0)\n"); printf("[capacity] - The Capacity parameter for the new channel (default: 0)\n"); printf("[type] - The type parameter for the new channel (default: S)\n"); printf(" R - For a reliable (stored in memory) channel with persistent eids\n"); printf(" P - For a persistent (stored on disk) channel\n"); printf(" S - For a simple (stored in memory) channel with non-persistent eids\n"); printf(" T - For a transient (no server based storage)\n"); printf(" O - For a off-heap (stored off-heap) channel with persistent eids\n"); printf(" G - For a paged (stored off-heap and on disk) channel\n"); printf(" M - For a Mixed (allows both memory and persistent events) channel\n\n"); printf("[cluster wide] - Whether the channel is cluster wide. Will only work if the realm is part of a cluster\n"); printf("[start eid] - The initial start event id for the new channel (default: 0)\n"); } protected: virtual void processArgs(int argc, char** argv) { // // Need a min of 2, rname, channel name, capacity, ttl, type if (argc < 3) { Usage(); exit(2); } std::string RNAME = argv[1]; std::string channelName = argv[2]; int age = 0; int cap = 0; std::string type = "S"; bool cluster = false; long eid = 0; //Check the channel name specified if (argc > 3) { age = atoi(argv[3]); } if (argc > 4) { cap = atoi(argv[4]); } if (argc > 5) { type = argv[5]; } if (argc > 6) { cluster = !strcmp(argv[6], "true"); } if (argc > 7) { eid = atoi(argv[7]); } // // Run the sample app // int nRproperty = 0; std::string *pRproperties = parseRealmProperties(RNAME, nRproperty); m_pSelf->doit(pRproperties, nRproperty, channelName, age, cap, type, cluster, eid); } public: static int Main(int argc, char** argv) { //Create an instance for this class m_pSelf = new makechan(); m_pSelf->processArgs(argc, argv); return 0; } }; makechan* makechan::m_pSelf = NULL; int main (int argc, char** argv) { return makechan::Main (argc, argv); }