testsuite/src/c++/clientPOA.cpp

Go to the documentation of this file.
00001 #define  SERVER_HEADER 1 // does #include <generated/xmlBlasterS.h> with CompatibleCorba.h, OMNIORB: use -Wbh=.h to force this extension
00002 #include <client/protocol/corba/CompatibleCorba.h>
00003 #include COSNAMING
00004 #include <generated/xmlBlaster.h>
00005 #include <fstream>
00006 #include <string>
00007 #include <iostream>
00008 
00009 using namespace std;
00010 using namespace authenticateIdl;
00011 using namespace serverIdl;
00012 
00013 
00017 class BlasterCallback_impl : virtual public POA_clientIdl::BlasterCallback  {
00018 
00019 private:
00020   ostream& print_msg(ostream &out, const serverIdl::MessageUnit &msg) {
00021     for (string::size_type i=0; i < msg.content.length(); i++) {
00022       out << msg.content[i];
00023     }
00024     return out;
00025   };
00026 
00027 public:
00028   BlasterCallback_impl()  {}
00029   ~BlasterCallback_impl() {}
00030 
00031   serverIdl::XmlTypeArr* update(const char* /*sessionId*/, const serverIdl::MessageUnitArr& messageUnitArr) {
00032     int nmax = messageUnitArr.length();
00033     serverIdl::XmlTypeArr *res = new serverIdl::XmlTypeArr(nmax);
00034     res->length(nmax);
00035     cout << endl;
00036     cout << "Callback invoked: there are " << nmax << " messages" << endl;
00037     cout << "messages: " << endl;
00038     for (int i=0; i < nmax; i++) {
00039       print_msg(cout,messageUnitArr[i]);
00040       cout << endl;
00041 
00042       CORBA::String_var str = CORBA::string_dup("<qos><state id='OK'/></qos>");
00043       (*res)[i] = str;
00044     }
00045     return res;
00046   };
00047 
00048   void updateOneway(const char* /*sessionId*/, const serverIdl::MessageUnitArr& messageUnitArr) {
00049     int nmax = messageUnitArr.length();
00050     cout << endl;
00051     cout << "Oneway callback invoked: there are " << nmax << " messages" << endl;
00052     cout << "messages: " << endl;
00053     for (int i=0; i < nmax; i++) {
00054       print_msg(cout,messageUnitArr[i]);
00055       cout << endl;
00056     }
00057   };
00058 
00059   char *ping(const char * /*qos*/) {
00060    return CORBA::string_dup("");
00061   };
00062 };
00063 
00064 
00065 
00066 int main(int argc, char* argv[]) {
00067 
00068   AuthServer_var authServer_obj;
00069   CORBA::ORB_var orb;
00070 
00071   try {
00072     // Create the ORB
00073     orb = CORBA::ORB_init(argc, argv);
00074 
00075     // Get the naming service
00076     CORBA::Object_var obj0;
00077     try {
00078       obj0 = orb->resolve_initial_references("NameService");
00079     } catch(const CORBA::ORB::InvalidName&) {
00080       cerr << argv[0] << ": can't resolve `NameService'" << endl << "Start naming service and try" << endl << "   ./clientPOA -ORBNamingIOR `cat ${DocumentRoot}/NS_Ref`" << endl << "(read README file)" << endl;
00081       return 1;
00082     }
00083 
00084     if(CORBA::is_nil(obj0.in())) {
00085       cerr << argv[0] << ": `NameService' is a nil object reference"
00086            << endl;
00087       return 1;
00088     }
00089 
00090     CosNaming::NamingContext_var nc =
00091       CosNaming::NamingContext::_narrow(obj0.in());
00092 
00093     if(CORBA::is_nil(nc.in())) {
00094       cerr << argv[0] << ": `NameService' is not a NamingContext";
00095       cerr << "object reference" << endl;
00096       return 1;
00097     }
00098 
00099     // now the Naming Context is known: get the objects by name
00100 
00101     CORBA::Object_var aObj;
00102     try {
00103       // Resolve names with the Naming Service
00104       CosNaming::Name aName;
00105       aName.length(1);
00106       aName[0].id   = CORBA::string_dup("xmlBlaster-Authenticate");
00107       aName[0].kind = CORBA::string_dup("MOM");
00108       aObj          = nc->resolve(aName);
00109       cout << "Resolved the authentication" << endl;
00110 
00111     } catch(const CosNaming::NamingContext::NotFound& ex) {
00112       cerr << argv[0] << ": Got a `NotFound' exception (";
00113       switch(ex.why)
00114         {
00115         case CosNaming::NamingContext::missing_node: cerr << "missing node";
00116           break;
00117         case CosNaming::NamingContext::not_context: cerr << "not context";
00118           break;
00119         case CosNaming::NamingContext::not_object: cerr << "not object";
00120           break;
00121         }
00122       cerr << ")" << endl;
00123       return 1;
00124     } catch(const CosNaming::NamingContext::CannotProceed&) {
00125       cerr << argv[0] << ": Got a `CannotProceed' exception" << endl;
00126       return 1;
00127     } catch(const CosNaming::NamingContext::InvalidName&) {
00128       cerr << argv[0] << ": Got an `InvalidName' exception" << endl;
00129       return 1;
00130     } catch(const CosNaming::NamingContext::AlreadyBound&) {
00131       cerr << argv[0] << ": Got an `AlreadyBound' exception" << endl;
00132       return 1;
00133     } catch(const CosNaming::NamingContext::NotEmpty&) {
00134       cerr << argv[0] << ": Got a `NotEmpty' exception" << endl;
00135       return 1;
00136     }
00137 
00138     // narrow IOR-String to object reference
00139     authServer_obj= AuthServer::_narrow(aObj);
00140 
00141     // get the rootPOA
00142     CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
00143     PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
00144     PortableServer::POAManager_var poa_mgr = poa->the_POAManager();
00145 
00146     // create an instance of the Callback Servant
00147     BlasterCallback_impl *impl = new BlasterCallback_impl();
00148 
00149     // Implicitly activate the Servant & get a reference to it
00150     clientIdl::BlasterCallback_ptr callback = impl->_this();
00151 
00152     // activate the poa
00153     poa_mgr->activate();
00154 
00155     string xmlQos("<qos><callback type='IOR'>");
00156     xmlQos += orb->object_to_string(callback);
00157     xmlQos += "</callback></qos>";
00158 
00159     serverIdl::Server_ptr
00160       xmlBlaster = authServer_obj->login("Fritz", "simple", xmlQos.c_str());
00161 
00162     cout << "Successful login!" << endl;
00163 
00164     //-------------- publish() a message -------------
00165     string xmlKey("<?xml version='1.0' encoding='ISO-8859-1' ?>\n"
00166                   "<key oid='' contentMime='text/xml'>\n </key>");
00167 
00168     MessageUnit message;
00169     message.xmlKey    = xmlKey.c_str();
00170 
00171     // is there a better way to fill the message.content ??
00172     char content[100] = "ti che ta tacat i tac tacum i tac!";
00173     message.content   = ContentType(sizeof(content),sizeof(content),
00174                                     (CORBA::Octet*)content);
00175     message.qos = "<qos></qos>";
00176 
00177     string publishOid = xmlBlaster->publish(message);
00178 
00179     cout << "Successfully published message with new oid=";
00180     cout << publishOid << endl;
00181 
00182     //-------------- subscribe() to the previous message OID -------
00183     cout << "Subscribing using the exact oid ..." << endl;
00184     xmlKey = "<?xml version='1.0' encoding='ISO-8859-1' ?>\n"
00185       "<key oid='" + publishOid + "'>\n </key>";
00186     string qualityOfService = "";
00187 
00188     try {
00189       xmlBlaster->subscribe(xmlKey.c_str(), qualityOfService.c_str());
00190     } catch(XmlBlasterException e) {
00191       cerr << "XmlBlasterException: " << e.errorCodeStr << ": " << e.message << endl;
00192     }
00193 
00194     cout << "Subscribed to '" << publishOid << "' ..." << endl;
00195 
00196 
00197     //-------------- wait for something to happen -------------------
00198     orb->run ();
00199 
00200   } catch(serverIdl::XmlBlasterException e) {
00201     cerr << "Caught Server Exception: " << e.errorCodeStr << ": " << e.message << endl;
00202   } catch(const CORBA::Exception &ex) {
00203     cerr << "CORBA: " << ex << endl;
00204   } catch (...) {
00205     cerr << "some other error" << endl;
00206   }
00207 
00208   return 0;
00209 }
00210 
00211 
00212 
00213