1 /*------------------------------------------------------------------------------
2 Name: ConnectionsHandler.h
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Handles the org::xmlBlaster::client::protocol::I_XmlBlasterConnections
6 ------------------------------------------------------------------------------*/
7 #ifndef _UTIL_DISPATCH_CONNECTIONSHANDLER_H
8 #define _UTIL_DISPATCH_CONNECTIONSHANDLER_H
9
10 #include <vector>
11 #include <util/xmlBlasterDef.h>
12 #include <util/dispatch/I_ConnectionsHandler.h>
13 #include <util/dispatch/I_PostSendListener.h>
14 #include <client/I_ConnectionProblems.h>
15 #include <util/XmlBlasterException.h>
16 #include <util/thread/ThreadImpl.h>
17 #include <util/I_Timeout.h>
18 #include <util/queue/MsgQueueEntry.h>
19 #include <util/queue/I_Queue.h>
20 //#include <util/queue/I_Queue.h>
21 // #include <util/queue/PublishQueueEntry.h>
22 // #include <util/queue/ConnectQueueEntry.h>
23
24 #ifndef _UTIL_QUEUE_I_QUEUE_H
25 namespace org { namespace xmlBlaster { namespace util { namespace queue {
26 class I_Queue;
27 }}}}
28 #endif
29
30 namespace org { namespace xmlBlaster { namespace util { namespace dispatch {
31
32 /**
33 * Interface for XmlBlaster, the supported methods on c++ client side. This is
34 * a pure virtual class.
35 * <p />
36 * @see <a href="http://www.xmlBlaster.org/xmlBlaster/src/java/org/xmlBlaster/protocol/corba/xmlBlaster.idl" target="others">CORBA xmlBlaster.idl</a>
37 */
38 class Dll_Export ConnectionsHandler : public I_Timeout, public org::xmlBlaster::util::dispatch::I_ConnectionsHandler
39 {
40 private:
41 const std::string ME;
42 org::xmlBlaster::util::qos::ConnectQosRef connectQos_;
43 org::xmlBlaster::util::qos::ConnectReturnQosRef connectReturnQos_;
44 org::xmlBlaster::client::I_ConnectionProblems* connectionProblemsListener_;
45 org::xmlBlaster::client::protocol::I_XmlBlasterConnection* connection_;
46 enum States status_;
47 org::xmlBlaster::util::Global& global_;
48 org::xmlBlaster::util::I_Log& log_;
49 org::xmlBlaster::util::thread::Mutex connectMutex_;
50 org::xmlBlaster::util::thread::Mutex publishMutex_;
51 int retries_;
52 int currentRetry_;
53 org::xmlBlaster::util::Timestamp pingPollTimerKey_;
54 org::xmlBlaster::util::queue::I_Queue* queue_;
55 org::xmlBlaster::util::dispatch::I_PostSendListener* postSendListener_;
56 const std::string instanceName_;
57 bool doStopPing_; // used to stop the pinger when destroying the object
58
59 public:
60 ConnectionsHandler(org::xmlBlaster::util::Global& global, const std::string& instanceName);
61
62 virtual ~ConnectionsHandler();
63
64
65 /**
66 * connect() is a login or authentication as well, the authentication schema
67 * is transported in the qos.
68 * It is more general then the login() method, since it allows
69 * to transport any authentication info in the xml based qos.
70 *
71 * You can still use login() for simple name/password based authentication.
72 *
73 * @param qos The authentication and other informations
74 * @param client A handle to your callback if desired or null
75 * @return org::xmlBlaster::util::qos::ConnectReturnQos
76 */
77 org::xmlBlaster::util::qos::ConnectReturnQosRef connect(const org::xmlBlaster::util::qos::ConnectQosRef& qos);
78
79 /**
80 * Logout from xmlBlaster. If the status is DEAD it returns false and writes a warning. If the status
81 * is ALIVE it disconnects. If the status is something else, it throws an exception.
82 * @param qos The QoS or null
83 */
84 bool disconnect(const org::xmlBlaster::util::qos::DisconnectQos& qos);
85
86 /**
87 * @return The connection protocol name "IOR" or "SOCKET" etc.
88 */
89 std::string getProtocol();
90
91 /**
92 * Is invoked when we poll for the server, for example after we have lost the connection.
93 */
94 // std::string loginRaw();
95
96 bool shutdown();
97
98 std::string getLoginName();
99
100 bool isLoggedIn();
101
102 std::string ping(const std::string& qos);
103
104 org::xmlBlaster::client::qos::SubscribeReturnQos subscribe(const org::xmlBlaster::client::key::SubscribeKey& key, const org::xmlBlaster::client::qos::SubscribeQos& qos);
105
106 std::vector<org::xmlBlaster::util::MessageUnit> get(const org::xmlBlaster::client::key::GetKey& key, const org::xmlBlaster::client::qos::GetQos& qos);
107
108 std::vector<org::xmlBlaster::client::qos::UnSubscribeReturnQos>
109 unSubscribe(const org::xmlBlaster::client::key::UnSubscribeKey& key, const org::xmlBlaster::client::qos::UnSubscribeQos& qos);
110
111 org::xmlBlaster::client::qos::PublishReturnQos publish(const org::xmlBlaster::util::MessageUnit& msgUnit);
112
113 void publishOneway(const std::vector<org::xmlBlaster::util::MessageUnit> &msgUnitArr);
114
115 std::vector<org::xmlBlaster::client::qos::PublishReturnQos> publishArr(const std::vector<org::xmlBlaster::util::MessageUnit> &msgUnitArr);
116
117 std::vector<org::xmlBlaster::client::qos::EraseReturnQos> erase(const org::xmlBlaster::client::key::EraseKey& key, const org::xmlBlaster::client::qos::EraseQos& qos);
118
119 /**
120 * Register a listener for to receive information about the progress of incoming data.
121 * Only one listener is supported, the last call overwrites older calls.
122 * @param listener Your listener, pass 0 to unregister.
123 * @return The previously registered listener or 0
124 */
125 org::xmlBlaster::util::dispatch::I_PostSendListener* registerPostSendListener(org::xmlBlaster::util::dispatch::I_PostSendListener *listener);
126
127 void initFailsafe(org::xmlBlaster::client::I_ConnectionProblems* connectionProblems);
128
129 void timeout(void *userData);
130
131 /**
132 * On POLLING or if the client side queue contains entries
133 * further messages need to be put to the queue to maintain sequence.
134 * @return true if the message must be put to queue.
135 */
136 bool putToQueue();
137
138 bool isRecoverable(const org::xmlBlaster::util::XmlBlasterException* reason);
139
140 /**
141 * Flushes all entries in the queue, i.e. the entries of the queue are sent to xmlBlaster.
142 * If the queue is empty or NULL, then 0 is returned. If the state is in POLLING or DEAD, then -1 is
143 * returned.. This method blocks until all entries in the queue have been sent.
144 */
145 long flushQueue();
146
147 org::xmlBlaster::util::queue::I_Queue* getQueue();
148
149 bool isFailsafe() const;
150
151 /**
152 * Same as isAlive() || isPolling()
153 * @return true if connect() call was successful, even if we are polling
154 */
155 bool isConnected() const;
156
157 /**
158 * @return true if connected with server and ready
159 */
160 bool isAlive() const;
161
162 /**
163 * @return true if polling for the server
164 */
165 bool isPolling() const;
166
167 /**
168 * @return true if we have given up
169 */
170 bool isDead() const;
171
172 /**
173 * Get connection status string for logging.
174 * @return "ALIVE" | "POLLING" | "DEAD"
175 */
176 std::string getStatusString() const;
177
178 org::xmlBlaster::util::qos::ConnectReturnQosRef connectRaw(const org::xmlBlaster::util::qos::ConnectQosRef& connectQos);
179
180 virtual org::xmlBlaster::client::protocol::I_XmlBlasterConnection& getConnection() const;
181
182 virtual org::xmlBlaster::util::qos::ConnectReturnQosRef getConnectReturnQos();
183
184 virtual org::xmlBlaster::util::qos::ConnectQosRef getConnectQos();
185
186 protected:
187 /** only used inside the class to avoid deadlock */
188 long flushQueueUnlocked(org::xmlBlaster::util::queue::I_Queue *queueToFlush, bool doRemove=true);
189 org::xmlBlaster::client::qos::PublishReturnQos queuePublish(const org::xmlBlaster::util::MessageUnit& msgUnit);
190 org::xmlBlaster::client::qos::SubscribeReturnQos queueSubscribe(const org::xmlBlaster::client::key::SubscribeKey& key, const org::xmlBlaster::client::qos::SubscribeQos& qos);
191 org::xmlBlaster::util::qos::ConnectReturnQosRef& queueConnect();
192 /**
193 * @param withInitialPing If true do an immediate ping without delay
194 */
195 bool startPinger(bool withInitialPing);
196
197 /**
198 * Going to polling status in case we are in failsafe mode or to DEAD if we are not in failsafe mode.
199 * Only for communication.* exceptions
200 */
201 void toPollingOrDead(const org::xmlBlaster::util::XmlBlasterException* reason);
202 /**
203 * Going to dead and shutdown the socket connection
204 */
205 void toPolling(const org::xmlBlaster::util::XmlBlasterException* reason);
206 /**
207 * Going to dead and shutdown the socket connection
208 */
209 void toDead(const org::xmlBlaster::util::XmlBlasterException* reason);
210 /**
211 * Convenience method to notify client code
212 */
213 bool sendingFailedNotification(const std::vector<org::xmlBlaster::util::queue::EntryType> &entries, const org::xmlBlaster::util::XmlBlasterException &exception);
214 };
215
216
217 }}}} // namespaces
218
219 #endif
syntax highlighted by Code2HTML, v. 0.9.1