1 
  2 #define  SERVER_HEADER 1 // does #include <generated/xmlBlasterS.h> with CompatibleCorba.h, OMNIORB: use -Wbh=.h to force this extension
  3 #include <client/protocol/corba/CompatibleCorba.h>
  4 #include COSNAMING
  5 #include <generated/xmlBlaster.h>
  6 #include <fstream>
  7 #include <string>
  8 #include <iostream>
  9 
 10 using namespace std;
 11 using namespace authenticateIdl;
 12 using namespace serverIdl;
 13 
 14 
 15 /**
 16  * Callback implementation
 17  */
 18 class BlasterCallback_impl : virtual public POA_clientIdl::BlasterCallback  {
 19 
 20 private:
 21   ostream& print_msg(ostream &out, const serverIdl::MessageUnit &msg) {
 22     for (string::size_type i=0; i < msg.content.length(); i++) {
 23       out << msg.content[i];
 24     }
 25     return out;
 26   };
 27 
 28 public:
 29   BlasterCallback_impl()  {}
 30   ~BlasterCallback_impl() {}
 31 
 32   serverIdl::XmlTypeArr* update(const char* /*sessionId*/, const serverIdl::MessageUnitArr& messageUnitArr) {
 33     int nmax = messageUnitArr.length();
 34     serverIdl::XmlTypeArr *res = new serverIdl::XmlTypeArr(nmax);
 35     res->length(nmax);
 36     cout << endl;
 37     cout << "Callback invoked: there are " << nmax << " messages" << endl;
 38     cout << "messages: " << endl;
 39     for (int i=0; i < nmax; i++) {
 40       print_msg(cout,messageUnitArr[i]);
 41       cout << endl;
 42 
 43       CORBA::String_var str = CORBA::string_dup("<qos><state id='OK'/></qos>");
 44       (*res)[i] = str;
 45     }
 46     return res;
 47   };
 48 
 49   void updateOneway(const char* /*sessionId*/, const serverIdl::MessageUnitArr& messageUnitArr) {
 50     int nmax = messageUnitArr.length();
 51     cout << endl;
 52     cout << "Oneway callback invoked: there are " << nmax << " messages" << endl;
 53     cout << "messages: " << endl;
 54     for (int i=0; i < nmax; i++) {
 55       print_msg(cout,messageUnitArr[i]);
 56       cout << endl;
 57     }
 58   };
 59 
 60   char *ping(const char * /*qos*/) {
 61    return CORBA::string_dup("");
 62   };
 63 };
 64 
 65 
 66 
 67 int main(int argc, char* argv[]) {
 68 
 69   AuthServer_var authServer_obj;
 70   CORBA::ORB_var orb;
 71 
 72   try {
 73     // Create the ORB
 74     orb = CORBA::ORB_init(argc, argv);
 75 
 76     // Get the naming service
 77     CORBA::Object_var obj0;
 78     try {
 79       obj0 = orb->resolve_initial_references("NameService");
 80     } catch(const CORBA::ORB::InvalidName&) {
 81       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;
 82       return 1;
 83     }
 84 
 85     if(CORBA::is_nil(obj0.in())) {
 86       cerr << argv[0] << ": `NameService' is a nil object reference"
 87            << endl;
 88       return 1;
 89     }
 90 
 91     CosNaming::NamingContext_var nc =
 92       CosNaming::NamingContext::_narrow(obj0.in());
 93 
 94     if(CORBA::is_nil(nc.in())) {
 95       cerr << argv[0] << ": `NameService' is not a NamingContext";
 96       cerr << "object reference" << endl;
 97       return 1;
 98     }
 99 
100     // now the Naming Context is known: get the objects by name
101 
102     CORBA::Object_var aObj;
103     try {
104       // Resolve names with the Naming Service
105       CosNaming::Name aName;
106       aName.length(1);
107       aName[0].id   = CORBA::string_dup("xmlBlaster-Authenticate");
108       aName[0].kind = CORBA::string_dup("MOM");
109       aObj          = nc->resolve(aName);
110       cout << "Resolved the authentication" << endl;
111 
112     } catch(const CosNaming::NamingContext::NotFound& ex) {
113       cerr << argv[0] << ": Got a `NotFound' exception (";
114       switch(ex.why)
115         {
116         case CosNaming::NamingContext::missing_node: cerr << "missing node";
117           break;
118         case CosNaming::NamingContext::not_context: cerr << "not context";
119           break;
120         case CosNaming::NamingContext::not_object: cerr << "not object";
121           break;
122         }
123       cerr << ")" << endl;
124       return 1;
125     } catch(const CosNaming::NamingContext::CannotProceed&) {
126       cerr << argv[0] << ": Got a `CannotProceed' exception" << endl;
127       return 1;
128     } catch(const CosNaming::NamingContext::InvalidName&) {
129       cerr << argv[0] << ": Got an `InvalidName' exception" << endl;
130       return 1;
131     } catch(const CosNaming::NamingContext::AlreadyBound&) {
132       cerr << argv[0] << ": Got an `AlreadyBound' exception" << endl;
133       return 1;
134     } catch(const CosNaming::NamingContext::NotEmpty&) {
135       cerr << argv[0] << ": Got a `NotEmpty' exception" << endl;
136       return 1;
137     }
138 
139     // narrow IOR-String to object reference
140     authServer_obj= AuthServer::_narrow(aObj);
141 
142     // get the rootPOA
143     CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
144     PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
145     PortableServer::POAManager_var poa_mgr = poa->the_POAManager();
146 
147     // create an instance of the Callback Servant
148     BlasterCallback_impl *impl = new BlasterCallback_impl();
149 
150     // Implicitly activate the Servant & get a reference to it
151     clientIdl::BlasterCallback_ptr callback = impl->_this();
152 
153     // activate the poa
154     poa_mgr->activate();
155 
156     string xmlQos("<qos><callback type='IOR'>");
157     xmlQos += orb->object_to_string(callback);
158     xmlQos += "</callback></qos>";
159 
160     serverIdl::Server_ptr
161       xmlBlaster = authServer_obj->login("Fritz", "simple", xmlQos.c_str());
162 
163     cout << "Successful login!" << endl;
164 
165     //-------------- publish() a message -------------
166     string xmlKey("<?xml version='1.0' encoding='ISO-8859-1' ?>\n"
167                   "<key oid='' contentMime='text/xml'>\n </key>");
168 
169     MessageUnit message;
170     message.xmlKey    = xmlKey.c_str();
171 
172     // is there a better way to fill the message.content ??
173     char content[100] = "ti che ta tacat i tac tacum i tac!";
174     message.content   = ContentType(sizeof(content),sizeof(content),
175                                     (CORBA::Octet*)content);
176     message.qos = "<qos></qos>";
177 
178     string publishOid = xmlBlaster->publish(message);
179 
180     cout << "Successfully published message with new oid=";
181     cout << publishOid << endl;
182 
183     //-------------- subscribe() to the previous message OID -------
184     cout << "Subscribing using the exact oid ..." << endl;
185     xmlKey = "<?xml version='1.0' encoding='ISO-8859-1' ?>\n"
186       "<key oid='" + publishOid + "'>\n </key>";
187     string qualityOfService = "";
188 
189     try {
190       xmlBlaster->subscribe(xmlKey.c_str(), qualityOfService.c_str());
191     } catch(XmlBlasterException e) {
192       cerr << "XmlBlasterException: " << e.errorCodeStr << ": " << e.message << endl;
193     }
194 
195     cout << "Subscribed to '" << publishOid << "' ..." << endl;
196 
197 
198     //-------------- wait for something to happen -------------------
199     orb->run ();
200 
201   } catch(serverIdl::XmlBlasterException e) {
202     cerr << "Caught Server Exception: " << e.errorCodeStr << ": " << e.message << endl;
203   } catch(const CORBA::Exception &ex) {
204     cerr << "CORBA: " << ex << endl;
205   } catch (...) {
206     cerr << "some other error" << endl;
207   }
208 
209   return 0;
210 }


syntax highlighted by Code2HTML, v. 0.9.1