]> pilppa.org Git - libplpbus.git/blob - src/plpbus/BusClient.cc
added .gitignore and started converting to use libplp for debug
[libplpbus.git] / src / plpbus / BusClient.cc
1 /*
2  * BusClient.cc
3  *
4  *  Created on: Jun 7, 2010
5  *      Author: lamikr
6  */
7
8 #include <plp/log.h>
9
10 #include "BusClient.hh"
11 #include "BusMessageInternal.hh"
12
13 using namespace std;
14 using namespace plpbus;
15
16 class OrbClientImpl : public virtual POA_plpbus_orb::OrbClient
17 {
18         private:
19                 IClientListener *listener;
20         public:
21                 OrbClientImpl(IClientListener *listener_param)  {
22                         listener        = listener_param;
23                 }
24                 virtual ~OrbClientImpl() {}
25
26                 virtual void receive_event_message(const char *event_param) {
27                         if (listener != NULL) {
28                                 listener->event_received(event_param);
29                         }
30                 }
31
32                 virtual void receive_event_dataitem_sequence(const ::plpbus_orb::DataItemSequence& event_param) {
33                         BusMessageInternal      *bus_msg;
34
35                         if (listener != NULL) {
36                                 bus_msg = new BusMessageInternal(event_param);
37                                 listener->event_received(bus_msg);
38                         }
39                 }
40
41                 virtual void receive_response_message(const char *msg_rsp_param) {
42                         if (listener != NULL) {
43                                 listener->response_received(msg_rsp_param);
44                         }
45                 }
46
47                 virtual void receive_response_dataitem_sequence(const ::plpbus_orb::DataItemSequence& msg_rsp_param) {
48                         BusMessageInternal      *bus_msg;
49
50                         if (listener != NULL) {
51                                 bus_msg = new BusMessageInternal(msg_rsp_param);
52                                 listener->response_received(bus_msg);
53                         }
54                 }
55 };
56
57 BusClient::BusClient()
58 {
59         log_debug("created\n");;
60         _orb            = NULL;
61         _poa            = NULL;
62 }
63
64 BusClient::~BusClient()
65 {
66         if (_orb != NULL) {
67                 _orb->destroy();
68                 _orb    = NULL;
69         }
70         log_debug("destroyed\n");
71 }
72
73 int BusClient::init(const char *server_name) {
74         int                     argc;
75         char                    **argv;
76         CORBA::Object_var       server_obj;
77
78         argc    = 0;
79         argv    = NULL;
80         log_info("init() started\n");;
81         _orb    = CORBA::ORB_init(argc, argv);
82         if (_orb != NULL) {
83                 log_debug("ORB_init() done, finding server: %s\n", server_name);
84                 server_obj      = find_server_object_by_name(_orb,
85                                                         CONST_CONTEXT_NAME__PLPBUS,
86                                                         CONST_CONTEXT_KIND__PLPBUS,
87                                                         server_name,
88                                                         CONST_CONTEXT_NAME__PLPBUS);
89                 if (CORBA::is_nil(server_obj) == false) {
90                         _server         = plpbus_orb::OrbServer::_narrow(server_obj);
91                         log_info("Server object found: %s", server_name);
92                 }
93                 else {
94                         log_error("Could not find server object: %s\n", server_name);
95                 }
96         }
97         return 0;
98 }
99
100 int BusClient::add_client_listener(IClientListener *listener_param) {
101         log_debug("add_client_listener() started\n");
102         if (_poa == NULL) {
103                 OrbClientImpl                   *client_cb;
104                 PortableServer::ObjectId_var    oid;
105
106                 _poa            = create_poa(_orb);
107                 client_cb       = new OrbClientImpl(listener_param);
108                 oid             = _poa->activate_object(client_cb);
109                 _client         = client_cb->_this();
110                 client_cb->_remove_ref();
111                 _server->add_event_listener(_client, "event_message", CONST_DEFAULT_EVENT_INTERVAL);
112         }
113         log_debug("add_client_listener() done\n");
114         return 0;
115 }
116
117 int BusClient::send_message_and_request_response(const char *msg_req_param) {
118         _server->send_message_and_request_response(_client, msg_req_param);
119         return 0;
120 }
121
122 int BusClient::send_message_and_wait_response(const char *msg_req_param, char **msg_rsp_param) {
123         CORBA::Long     ret_val;
124
125         *msg_rsp_param  = _server->send_message_and_wait_response(msg_req_param, ret_val);
126         return ret_val;
127 }
128
129 int BusClient::send_message_and_request_response(BusMessage *msg_req_param) {
130         _server->send_dataitem_message_and_request_response(_client, msg_req_param->_dataItemSeq);
131         return 0;
132 }
133
134 int BusClient::send_message_and_wait_response(BusMessage *msg_req_param, BusMessage **msg_rsp_param) {
135         plpbus_orb::DataItemSequence    *seq_rsp;
136
137         seq_rsp = NULL;
138         _server->send_dataitem_message_and_wait_response(msg_req_param->_dataItemSeq, seq_rsp);
139 }
140
141 void BusClient::request_shutdown() {
142         if (CORBA::is_nil(_server) == false) {
143                 _server->shutdown();
144         }
145 }
146
147 PortableServer::POA_var BusClient::create_poa(CORBA::ORB_var orb) {
148         PortableServer::POA_var         ret_val;
149         CORBA::Object_var               poa_obj;
150         CORBA::PolicyList               policy_list;
151         CORBA::Any                      policyVal;
152         PortableServer::POAManager_var  poa_man;
153         PortableServer::POA_var         rootpoa;
154
155         ret_val = NULL;
156         poa_obj = orb->resolve_initial_references(CONST_ROOT_POA_NAME);
157         if (poa_obj != NULL) {
158                 rootpoa = PortableServer::POA::_narrow(poa_obj);
159                 if (rootpoa != NULL) {
160                         poa_man = rootpoa->the_POAManager();
161                         if (poa_man != NULL) {
162                                 poa_man->activate();
163                                 // bidirectional policy
164                                 policy_list.length(1);
165                                 policyVal       <<= BiDirPolicy::BOTH;
166                                 policy_list[0] = orb->create_policy(BiDirPolicy::BIDIRECTIONAL_POLICY_TYPE, policyVal);
167                                 ret_val = rootpoa->create_POA(CONST_ROOT_POA_BIDIR_POLICY_NAME,
168                                                         poa_man,
169                                                         policy_list);
170                         }
171                 }
172         }
173         else {
174                 cerr << "Failed to create RootPOA." << endl;
175         }
176         return ret_val;
177 }
178
179 CosNaming::NamingContext_var BusClient::get_name_service_context(CORBA::ORB_var orb_param) {
180         CORBA::Object_var               ns_obj;
181         CosNaming::NamingContext_var    ret_val;
182
183         ret_val = NULL;
184         try {
185                 ns_obj  = orb_param->resolve_initial_references(CONST_NAME_SERVICE_NAME);
186                 if (CORBA::is_nil(ns_obj) == false) {
187                         // narrow the object reference
188                         ret_val = CosNaming::NamingContext::_narrow(ns_obj);
189                 }
190         }
191         catch (CORBA::ORB::InvalidName&) {
192                 // This should not happen!
193                 cerr << "Could not find name service." << endl;
194         }
195         catch(CORBA::TRANSIENT& ex) {
196                 cerr << "Could not find name service, verify that name service is running. " << endl;
197         }
198         catch (CORBA::NO_RESOURCES&) {
199                 cerr << "Could not find name service, verify that name service is running. " << endl;
200         }
201         catch(CORBA::SystemException& ex) {
202                 cerr << "Could not find name service, unknown reason. " << endl;
203         }
204         return ret_val;
205 }
206
207 CORBA::Object_var BusClient::find_server_object_by_name(CORBA::ORB_var orb_param,
208                                                         const char *leaf_name_param,
209                                                         const char *leaf_kind_param,
210                                                         const char *service_name_param,
211                                                         const char *service_kind_param)
212 {
213         CORBA::Object_var               ret_val;
214         CosNaming::NamingContext_var    ns_root_context;
215         CosNaming::Name                 service_data;
216
217         ret_val         = CORBA::Object::_nil();
218         ns_root_context = get_name_service_context(orb_param);
219         if (CORBA::is_nil(ns_root_context) == false) {
220                 // define search criteria
221                 service_data.length(2);
222                 service_data[0].id   = leaf_name_param;
223                 service_data[0].kind = leaf_kind_param;
224                 service_data[1].id   = service_name_param;
225                 service_data[1].kind = service_kind_param;
226                 try {
227                         ret_val = ns_root_context->resolve(service_data);
228                 }
229                 catch(CosNaming::NamingContext::NotFound& ex) {
230                         log_error("Could not find service from the name server.\n");
231                 }
232                 catch(CORBA::TRANSIENT& ex) {
233                         log_error("Could not find service from the name server.\n");
234                 }
235                 catch(CORBA::SystemException& ex) {
236                         log_error("Could not find service from the name server, unknown error.\n");
237                 }
238         }
239         else {
240                 log_error("Could not find naming service.\n");
241         }
242         return ret_val;
243 }