gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
ORB.H
Go to the documentation of this file.
1 /* Copyright 2000-2018 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16  */
17 #ifndef ORB_H_
18 #define ORB_H_
19 
20 #ifndef _MSC_VER
21 #include "config.h" // generated as part of autotools
22 #endif
23 
24 #ifndef ZEROC_ICE_EMBEDDED
25 #include <Ice/Ice.h>
26 #include <IceUtil/Mutex.h>
27 #else
28 #include <IceE/IceE.h>
29 #include <IceE/Mutex.h>
30 #endif
31 
32 #include <list>
33 #include <iostream>
34 #include <sstream>
35 #include <vector>
36 using namespace std;
37 
38 #include "ORBICE.H"
39 
40 /*#ifdef _MSC_VER
41 #ifdef CREATING_DLL
42 #define SHARED __declspec( dllexport )
43 #else
44 #define SHARED __declspec( dllimport )
45 #endif
46 #else
47 #define SHARED
48 #endif
49 */
50 
51 namespace ORB {
52 
53 #define ORB_DEFAULT_BASEPORT 10000
54 
55 
88 template <class ZEROCICETYPE>
89 class ORBObject : public ZEROCICETYPE {
90 public:
91  const static string name;
92 };
93 
101 class ORBBase {
102  int basePort;
103  IceUtil::Mutex countMutex;
104  list<int> offsetList;
105 
108  void init(int basePortDifferent) {
109  basePort=basePortDifferent;
110  }
111 public:
113  ORBBase(void) {
114  init(ORB_DEFAULT_BASEPORT);
115  }
116  ORBBase(int basePortDifferent) {
117  init(basePortDifferent);
118  }
122  int getPortOffset(void) {
123  countMutex.lock();
124 
125  int i=-1;
126  if (offsetList.size()>0) {
127  // insert the first missing number
128  offsetList.sort();
129  std::list<int>::iterator offset=offsetList.begin();
130  while (*(offset++)==++i) ; // find the first missing number
131  } else // the list is empty
132  i=0;
133  offsetList.push_back(i); // add the missing number to the list
134 
135  countMutex.unlock();
136  return i;
137  }
138 
142  void removePortOffset(int whichOffset) {
143  countMutex.lock();
144  offsetList.remove(whichOffset);
145  countMutex.unlock();
146  }
147 
152  int getTCPUDPPort(int portOffset) {
153  return basePort+2*portOffset;
154  }
155 
161  int getSSLPort(int portOffset) {
162  return getTCPUDPPort(portOffset)+1;
163  }
164 
169  int getBasePort(void) {
170  return basePort;
171  }
172 
179  string generateEndPoint(const string& transportName, std::string& ipAddress, int& portOffset) {
180  string tn=transportName, ia=ipAddress;
181  int po=portOffset;
182  return generateEndPoint(tn, ia, po);
183  }
184 
191  string generateEndPoint(string &transportName, string ipAddress, int portOffset) {
192  stringstream endPoint;
193  if (ipAddress.size()>0) { // if we are setting up a proxy string
194  ipAddress=string(" -h ")+ipAddress+' ';
195  endPoint<<transportName<<":";
196  }
197  endPoint<<"tcp " <<ipAddress<< "-p "<<getTCPUDPPort(portOffset);
198 #ifndef ZEROC_ICE_EMBEDDED
199  endPoint<<" : udp "<<ipAddress<<"-p "<<getTCPUDPPort(portOffset);
200 #endif
201  cout<<"endpoint :"<<endl;
202  cout<<endPoint.str()<<endl;
203  // currently ssl is not activated ... this can be activated but would require certs
204  //endPoint<<"tcp " <<ipAddress<< "-p "<<getTCPUDPPort(portOffset)<<":udp "<<ipAddress<<"-p "<<getTCPUDPPort(portOffset)<<":ssl "<<ipAddress<<"-p "<<getSSLPort(portOffset); // get the TCP, USP and SSL ports
205  return endPoint.str();
206  }
207 };
208 
222 class ORBOriginator : public ORB::ORBICE, virtual public ORBBase {
223 protected:
225  string hostAddress;
226 
227  string transportName;
228  Ice::CommunicatorPtr communicator;
229  Ice::ObjectAdapterPtr adapterPtr;
230 
237  ORBOriginator(int argc, char *argv[], string transportNameIn, string ipAddress) {
238  initialise(argc, argv, transportNameIn, ipAddress, vector<pair<string,string> >()); // initialise the communicator and adapter
239  }
240 
248  ORBOriginator(int argc, char *argv[], string transportNameIn, string ipAddress, const vector<pair<string,string> > extraProperties) {
249  initialise(argc, argv, transportNameIn, ipAddress, extraProperties); // initialise the communicator and adapter
250  }
251 
259  ORBOriginator(int argc, char *argv[], string transportNameIn, string ipAddress, int basePortDifferent): ORBBase(basePortDifferent) {
260  initialise(argc, argv, transportNameIn, ipAddress, vector<pair<string,string> >()); // initialise the communicator and adapter
261  }
262 
271  ORBOriginator(int argc, char *argv[], string transportNameIn, string ipAddress, const vector<pair<string,string> > extraProperties, int basePortDifferent): ORBBase(basePortDifferent) {
272  initialise(argc, argv, transportNameIn, ipAddress, extraProperties); // initialise the communicator and adapter
273  }
274 
284  void initialise(int argc, char *argv[], string transportNameIn, string ipAddress, const vector<pair<string,string> > extraProperties) {
285  communicator=NULL;
286  hostAddress=ipAddress;
287  portOffset=getPortOffset(); // get a unique port offset
288  transportName=transportNameIn; // set the ZeroC ICE application layer name
289  Ice::InitializationData initData; // generate required initialisation data
290  initData.properties = Ice::createProperties();
291  initData.properties->setProperty("Ice.Warn.Connections", "2");
292  initData.properties->setProperty("Ice.Warn.UnusedProperties", "1");
293  // parse the extra properties
294  for (vector<pair<string,string> >::const_iterator pss=extraProperties.begin(); pss!=extraProperties.end(); ++pss)
295  initData.properties->setProperty(pss->first, pss->second);
296 
297  communicator = Ice::initialize(argc, argv, initData); // create the communicator
298  }
299 
302  void initialiseAdapter(void) {
303  //cout<<"enpoint string "<<generateEndPoint(transportName, string(), portOffset).c_str()<<endl;
304  adapterPtr=communicator->createObjectAdapterWithEndpoints(transportName+".Control",generateEndPoint(transportName, hostAddress, portOffset).c_str());
305  adapterPtr->add((ORB::ORBICE*)this, communicator->stringToIdentity(transportName)); // add this class to the adapter
306  adapterPtr->activate();
307  }
308 
312  void getObjectPointer(const string &name, Ice::ObjectPtr &objectPointer, const Ice::Current& cur) {
313  Ice::Identity identity;
314  identity.name=name;
315  objectPointer=adapterPtr->find(identity);
316  //cout<<"objectPointer = "<<objectPointer<<endl;
317  }
318 
319 public:
327  ORBOriginator(int argc, char *argv[], string transportNameIn) {
328  initialise(argc, argv, transportNameIn, string(), vector<pair<string,string> >()); // initialise the communicator and adapter
329  initialiseAdapter();
330  }
331 
340  ORBOriginator(int argc, char *argv[], string transportNameIn, int basePortDifferent): ORBBase(basePortDifferent) {
341  initialise(argc, argv, transportNameIn, string(), vector<pair<string,string> >()); // initialise the communicator and adapter
342  initialiseAdapter();
343  }
344 
353  ORBOriginator(int argc, char *argv[], string transportNameIn, const vector<pair<string,string> > extraProperties) {
354  initialise(argc, argv, transportNameIn, string(), extraProperties); // initialise the communicator and adapter
355  initialiseAdapter();
356  }
357 
367  ORBOriginator(int argc, char *argv[], string transportNameIn, const vector<pair<string,string> > extraProperties, int basePortDifferent): ORBBase(basePortDifferent) {
368  initialise(argc, argv, transportNameIn, string(), extraProperties); // initialise the communicator and adapter
369  initialiseAdapter();
370  }
371 
373  virtual ~ORBOriginator(void) {
374  removePortOffset(portOffset);
375  if (communicator)
376  communicator->shutdown();
377  }
378 
382  void shutdown(const Ice::Current& cur) {
383  cur.adapter->getCommunicator()->shutdown();
384  }
385 
389  bool isShutdown(){
390  if (communicator)
391  return communicator->isShutdown();
392  return true;
393  }
394 
399  void addClass(Ice::ObjectPtr objectToORB, const string &name) {
400  Ice::Identity identity;
401  identity.name=name;
402  if (adapterPtr->find(identity)) // check whether it already exists
403  cerr<<"ORBOriginator::addClass : WARNING : that class is already serlialised on the adapter"<<endl;
404  adapterPtr->add(objectToORB, identity);
405  }
406 
409  void waitForShutdown(void) {
410  communicator->waitForShutdown();
411  }
412 
413 // void remove(const string &name){
414 // Ice::Identity ident;
415 // ident.name=name;
416 // if (adapterPtr->find(ident)){
417 // cout<<name<<" found, removing"<<endl;
418 // adapterPtr->remove(ident);
419 // }
420 // }
421 };
422 
431 class ORBReplicator : public Ice::ObjectFactory, public ORBOriginator {
432  ORB::ORBICEPrx oRBOriginatorPrx;
433 
436  void destroy() {
437  }
438 
442  void initialise(void) {
443  try {
444  oRBOriginatorPrx = ORB::ORBICEPrx::checkedCast(communicator->stringToProxy(generateEndPoint(transportName, hostAddress, portOffset).c_str())->ice_twoway()->ice_timeout(-1)->ice_secure(false));
445  } catch (const Ice::Exception& e) {
446  cerr << e << endl;
447  } catch (...) { cout << "unknown exception"; }
448  if (!oRBOriginatorPrx) {
449  cerr<<"ORBReplicator::initialise : couldn't get the ORBOriginator proxy, can't continue \n\n\tPlease check that the intended application on the other side of the netowrk is up and running!"<<endl;
450  //assert(false);
451  }
452  }
453 
454 public:
463  ORBReplicator(int argc, char *argv[], string transportNameIn, string ipAddress) : ORBOriginator(argc, argv, transportNameIn, ipAddress) {
464  initialise();
465  }
466 
476  ORBReplicator(int argc, char *argv[], string transportNameIn, string ipAddress, int basePortDifferent) : ORBOriginator(argc, argv, transportNameIn, ipAddress, basePortDifferent) {
477  initialise();
478  }
479 
489  ORBReplicator(int argc, char *argv[], string transportNameIn, string ipAddress, const vector<pair<string,string> > extraProperties) : ORBOriginator(argc, argv, transportNameIn, ipAddress, extraProperties) {
490  initialise();
491  }
492 
503  ORBReplicator(int argc, char *argv[], string transportNameIn, string ipAddress, const vector<pair<string,string> > extraProperties, int basePortDifferent) : ORBOriginator(argc, argv, transportNameIn, ipAddress, extraProperties, basePortDifferent) {
504  initialise();
505  }
506 
510  virtual Ice::ObjectPtr create(const std::string&)=0;
511 
512 // /* Register a class with the object factory.
513 // When required ORBOriginator::create will make the required class.
514 // \param The unique id which identifies the class to create
515 // \tparam ICEIDTYPE The class to get the static id from
516 // */
517 // template<typename ICEIDTYPE>
518 // void addObjectFactory(void) {
519 // communicator->addObjectFactory(static_cast<Ice::ObjectFactory*>(this), ICEIDTYPE::ice_staticId()); // register that the factory can now generate the specified class
520 // }
521 
526  void addClass(Ice::ObjectPtr objectToORB, const string &name) {
527  cerr<<"ORBReplicator::addClass : Only ORBOriginators can add classes to the ORB application layer\n\t You need an originator on this side to add your classes to the ORB application layer"<<endl;
528  }
529 
532  void shutdown(void) {
533  oRBOriginatorPrx->shutdown(); // call the Originator's shutdown method
534  }
535 
542  template<typename PRXTYPE>
543  PRXTYPE getObjectProxy(const string &nameIn) {
544  return PRXTYPE::checkedCast(communicator->stringToProxy(generateEndPoint(nameIn, hostAddress, portOffset).c_str()));
545  }
546 
557  template<typename CLASSTYPE>
558  CLASSTYPE *getObjectPointer(const string &nameIn) {
559  if (!communicator->findObjectFactory(CLASSTYPE::ice_staticId())) // add the object to the factory if it is missing
560  communicator->addObjectFactory(static_cast<Ice::ObjectFactory*>(this), CLASSTYPE::ice_staticId()); // register that the factory can now generate the specified class
561 
562  // request the object from the ORBOriginator, and create it using the factory.
563  Ice::ObjectPtr objectPtr;
564  oRBOriginatorPrx->getObjectPointer(nameIn, objectPtr);
565  objectPtr->__setNoDelete(true); // we have to memory manage ourselves
566  //return PTRTYPE::dynamicCast(objectPtr); // this was used previously to return the pointer
567  return dynamic_cast<CLASSTYPE *>(objectPtr.get());
568  }
569 
573  bool connectedOK(){
574  return (bool)oRBOriginatorPrx!=NULL;
575  }
576 };
577 
578 };
579 #endif // ORB_H_
ORBReplicator(int argc, char *argv[], string transportNameIn, string ipAddress)
Definition: ORB.H:463
void shutdown(const Ice::Current &cur)
Definition: ORB.H:382
Ice::CommunicatorPtr communicator
Resident communicator.
Definition: ORB.H:228
ORBReplicator(int argc, char *argv[], string transportNameIn, string ipAddress, const vector< pair< string, string > > extraProperties, int basePortDifferent)
Definition: ORB.H:503
int getBasePort(void)
Definition: ORB.H:169
void initialise(void)
Definition: ORB.H:442
ORBOriginator(int argc, char *argv[], string transportNameIn, int basePortDifferent)
Definition: ORB.H:340
The namespace is ORB.
Definition: ORB.H:51
#define ORB_DEFAULT_BASEPORT
The default base port for networking.
Definition: ORB.H:53
int basePort
The starting network port.
Definition: ORB.H:102
int getSSLPort(int portOffset)
Definition: ORB.H:161
ORBOriginator(int argc, char *argv[], string transportNameIn, string ipAddress, const vector< pair< string, string > > extraProperties, int basePortDifferent)
Definition: ORB.H:271
string hostAddress
The hostname or IP address of the node to connect to, empty for the originator and set for the replic...
Definition: ORB.H:225
STL namespace.
void addClass(Ice::ObjectPtr objectToORB, const string &name)
Definition: ORB.H:526
ORBBase(int basePortDifferent)
Definition: ORB.H:116
ORBOriginator(int argc, char *argv[], string transportNameIn, const vector< pair< string, string > > extraProperties, int basePortDifferent)
Definition: ORB.H:367
void initialiseAdapter(void)
Definition: ORB.H:302
void removePortOffset(int whichOffset)
Definition: ORB.H:142
void waitForShutdown(void)
Definition: ORB.H:409
bool connectedOK()
Definition: ORB.H:573
ORBOriginator(int argc, char *argv[], string transportNameIn, string ipAddress)
Definition: ORB.H:237
ORBOriginator(int argc, char *argv[], string transportNameIn, string ipAddress, const vector< pair< string, string > > extraProperties)
Definition: ORB.H:248
ORBReplicator(int argc, char *argv[], string transportNameIn, string ipAddress, int basePortDifferent)
Definition: ORB.H:476
string generateEndPoint(const string &transportName, std::string &ipAddress, int &portOffset)
Definition: ORB.H:179
int getTCPUDPPort(int portOffset)
Definition: ORB.H:152
static const string name
The name of this class - once set, it can&#39;t be changed.
Definition: ORB.H:91
ORBOriginator(int argc, char *argv[], string transportNameIn)
Definition: ORB.H:327
void getObjectPointer(const string &name, Ice::ObjectPtr &objectPointer, const Ice::Current &cur)
Definition: ORB.H:312
void initialise(int argc, char *argv[], string transportNameIn, string ipAddress, const vector< pair< string, string > > extraProperties)
Definition: ORB.H:284
virtual ~ORBOriginator(void)
Destructor.
Definition: ORB.H:373
void create(int n)
ORB::ORBICEPrx oRBOriginatorPrx
The proxy of the ORBOriginator on the other side of the network.
Definition: ORB.H:432
ORBReplicator(int argc, char *argv[], string transportNameIn, string ipAddress, const vector< pair< string, string > > extraProperties)
Definition: ORB.H:489
Ice::ObjectAdapterPtr adapterPtr
Resident adapter.
Definition: ORB.H:229
ORBOriginator(int argc, char *argv[], string transportNameIn, const vector< pair< string, string > > extraProperties)
Definition: ORB.H:353
string transportName
Zeroc ICE application layer name.
Definition: ORB.H:227
int getPortOffset(void)
Definition: ORB.H:122
void destroy()
Definition: ORB.H:436
int portOffset
The port offset of this application layer.
Definition: ORB.H:224
void addClass(Ice::ObjectPtr objectToORB, const string &name)
Definition: ORB.H:399
list< int > offsetList
The list of port offsets used.
Definition: ORB.H:104
string generateEndPoint(string &transportName, string ipAddress, int portOffset)
Definition: ORB.H:191
void shutdown(void)
Definition: ORB.H:532
bool isShutdown()
Definition: ORB.H:389
PRXTYPE getObjectProxy(const string &nameIn)
Definition: ORB.H:543
ORBOriginator(int argc, char *argv[], string transportNameIn, string ipAddress, int basePortDifferent)
Definition: ORB.H:259
CLASSTYPE * getObjectPointer(const string &nameIn)
Definition: ORB.H:558
ORBBase(void)
Constructor using ORB_DEFAULT_BASEPORT as the starting base network port.
Definition: ORB.H:113
void init(int basePortDifferent)
Definition: ORB.H:108
IceUtil::Mutex countMutex
Mutex semaphore to enable the manipulation of the offsetList.
Definition: ORB.H:103
gtkIOStream: /tmp/gtkiostream/include/ORB.H Source File
GTK+ IOStream  Beta