1 #ifdef COMPILE_CORBA_PLUGIN
2
3 #include <client/XmlBlasterAccess.h>
4 #include <util/XmlBlasterException.h>
5 #include <util/Global.h>
6 #include <util/Timestamp.h>
7 #include <client/protocol/corba/CorbaDriverFactory.h>
8 #include <iostream>
9
10 /**
11 * This demo shows how to use XmlBlasterAccess with an orb which is already initialized. Since
12 * XmlBlasterAccess does not know anything about CORBA, you must instantiate a CorbaDriver outside.
13 * To make sure that the XmlBlasterAccess instance you use will internally use the CorbaDriver you
14 * instantiated, you have to pass the same instanceName to both.
15 * When passing an orb to CorbaDriver::getInstance(...) the CorbaDriver does not start a thread to
16 * perform the orb work. You have to provide it yourself (See here the run() method provided).
17 *
18 *
19 * This client connects to xmlBlaster and subscribes to a message.
20 * <p />
21 * We then publish the message and receive it asynchronous in the update() method.
22 * <p />
23 * Invoke: ExternOrb
24 * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/interface.html" target="others">xmlBlaster interface</a>
25 */
26
27 using namespace std;
28 using namespace org::xmlBlaster::util;
29 using namespace org::xmlBlaster::util::qos;
30 using namespace org::xmlBlaster::util::thread;
31 using namespace org::xmlBlaster::util::dispatch;
32 using namespace org::xmlBlaster::client;
33 using namespace org::xmlBlaster::client::protocol::corba;
34 using namespace org::xmlBlaster::client::qos;
35 using namespace org::xmlBlaster::client::key;
36
37 namespace org { namespace xmlBlaster { namespace demo {
38
39 class ExternOrb : public I_Callback, // for the asynchroneous updates
40 public I_ConnectionProblems, // for notification of connection problems when failsafe
41 public Thread // the thread to perform the orb work
42 {
43 private:
44 string ME; // the string identifying this class when logging
45 Global& global_;
46 I_Log& log_; // the reference to the log object for this instance
47 bool doRun_;
48 CORBA::ORB_ptr orb_;
49 CorbaDriverFactory& factory_;
50 public:
51 ExternOrb(Global& glob, CORBA::ORB_ptr orb)
52 : Thread(),
53 ME("ExternOrb"),
54 global_(glob),
55 log_(glob.getLog("demo")),
56 factory_(CorbaDriverFactory::getFactory(glob, orb))
57 {
58 doRun_ = true;
59 orb_ = orb;
60 }
61
62
63 virtual ~ExternOrb() // the constructor does nothing for the moment
64 {
65 doRun_ = false;
66 this->join();
67 }
68
69
70 void run()
71 {
72 log_.info(ME, "the corba loop starts now");
73 while (doRun_) {
74 while (orb_->work_pending()) orb_->perform_work();
75 sleep(20); // sleep 20 milliseconds
76 }
77 log_.info(ME, "the corba loop has ended now");
78 }
79
80
81 bool reachedAlive(StatesEnum /*oldState*/, I_ConnectionsHandler* /*connectionsHandler*/)
82 {
83 log_.info(ME, "reconnected");
84 return true;
85 }
86
87 void reachedDead(StatesEnum /*oldState*/, I_ConnectionsHandler* /*connectionsHandler*/)
88 {
89 log_.info(ME, "lost connection");
90 }
91
92 void reachedPolling(StatesEnum /*oldState*/, I_ConnectionsHandler* /*connectionsHandler*/)
93 {
94 log_.info(ME, "going to poll modus");
95 }
96
97 void execute()
98 {
99 start(false); // to start the orb worker ...
100 try {
101 // CorbaDriver driver =
102 XmlBlasterAccess con(global_);
103 con.initFailsafe(this);
104
105 // Creates a connect qos with the user 'joe' and the password 'secret'
106 ConnectQos qos(global_, "joe", "secret");
107 log_.info(ME, string("connecting to xmlBlaster. Connect qos: ") + qos.toXml());
108
109 // connects to xmlBlaster and gives a pointer to this class to tell which update method to invoke
110 // when callbacks come from the server.
111 ConnectReturnQos retQos = con.connect(qos, this); // Login to xmlBlaster, register for updates
112 log_.info(ME, "successfully connected to xmlBlaster. Return qos: " + retQos.toXml());
113
114 // subscribe key. By invoking setOid you implicitly choose the 'EXACT' mode. If you want to
115 // subscribe with XPATH use setQueryString instead.
116 SubscribeKey subKey(global_);
117 subKey.setOid("ExternOrb");
118 SubscribeQos subQos(global_);
119 log_.info(ME, string("subscribing to xmlBlaster with key: ") + subKey.toXml() + " and qos: " + subQos.toXml());
120
121 SubscribeReturnQos subRetQos = con.subscribe(subKey, subQos);
122 log_.info(ME, string("successfully subscribed to xmlBlaster. Return qos: ") + subRetQos.toXml());
123
124 // publish a message with the oid 'ExternOrb'
125 PublishQos publishQos(global_);
126 PublishKey publishKey(global_);
127 publishKey.setOid("ExternOrb");
128 MessageUnit msgUnit(publishKey, string("Hi"), publishQos);
129 log_.info(ME, string("publishing to xmlBlaster with message: ") + msgUnit.toXml());
130 PublishReturnQos pubRetQos = con.publish(msgUnit);
131 log_.info(ME, "successfully published to xmlBlaster. Return qos: " + pubRetQos.toXml());
132 try {
133 Thread::sleepSecs(1);
134 }
135 catch(XmlBlasterException e) {
136 cout << e.toXml() << endl;
137 }
138
139 // now an update should have come. Its time to erase the message (otherwise you would get directly
140 // an update the next time you connect to the same xmlBlaster server. Specify which messages you
141 // want to erase. Note that you will get an update with the status of the UpdateQos set to 'ERASED'.
142 EraseKey eraseKey(global_);
143 eraseKey.setOid("ExternOrb");
144 EraseQos eraseQos(global_);
145 log_.info(ME, string("erasing the published message. Key: ") + eraseKey.toXml() + " qos: " + eraseQos.toXml());
146 vector<EraseReturnQos> eraseRetQos = con.erase(eraseKey, eraseQos);
147 for (size_t i=0; i < eraseRetQos.size(); i++ ) {
148 log_.info(ME, string("successfully erased the message. return qos: ") + eraseRetQos[i].toXml());
149 }
150
151 log_.info(ME, "going to sleep for one minute");
152 org::xmlBlaster::util::thread::Thread::sleep(60000);
153
154 DisconnectQos disconnectQos(global_);
155 con.disconnect(disconnectQos);
156 }
157 catch (XmlBlasterException e) {
158 cout << e.toXml() << endl;
159 }
160 }
161
162 string update(const string& /*sessionId*/,
163 UpdateKey& updateKey,
164 const unsigned char* /*content*/,
165 long /*contentSize*/,
166 UpdateQos& updateQos)
167 {
168 log_.info(ME, "update: key: " + updateKey.toXml());
169 log_.info(ME, "update: qos: " + updateQos.toXml());
170 return "";
171 }
172
173 };
174
175 }}} // namespace
176 /**
177 * Try
178 * <pre>
179 * java ExternOrb -help
180 * </pre>
181 * for usage help
182 */
183 int main(int args, char ** argv)
184 {
185 org::xmlBlaster::util::Object_Lifetime_Manager::init();
186 // suppose you have already initialized an orb
187 CORBA::ORB_ptr orb = CORBA::ORB_init(args, argv);
188
189 Global& glob = Global::getInstance();
190 glob.initialize(args, argv);
191
192 org::xmlBlaster::demo::ExternOrb hello(glob, orb);
193 hello.execute();
194 org::xmlBlaster::util::Object_Lifetime_Manager::fini();
195 return 0;
196 }
197
198 #else // COMPILE_CORBA_PLUGIN
199 #include <iostream>
200 int main(int args, char ** argv)
201 {
202 ::std::cout << "ExternOrb: COMPILE_CORBA_PLUGIN is not defined, nothing to do" << ::std::endl;
203 return 0;
204 }
205 #endif // COMPILE_CORBA_PLUGIN
syntax highlighted by Code2HTML, v. 0.9.1