diff options
| -rw-r--r-- | common/Makefile | 6 | ||||
| -rw-r--r-- | common/common.cpp | 87 | ||||
| -rw-r--r-- | common/common.h | 40 |
3 files changed, 131 insertions, 2 deletions
diff --git a/common/Makefile b/common/Makefile index 441b13f..af3963e 100644 --- a/common/Makefile +++ b/common/Makefile @@ -2,7 +2,9 @@ CC := $(CROSS_COMPILE)gcc CXX := $(CROSS_COMPILE)g++ LD := $(CROSS_COMPILE)g++ -CFLAGS += -fPIC +DISTRIO_IO = ../io + +CFLAGS += -fPIC -I. -I$(DISTRIO_IO) CXXFLAGS += $(CFLAGS) TAO_IDL := tao_idl @@ -11,7 +13,7 @@ DESTDIR := /usr COMPONENT = distrio_common LIB = lib$(COMPONENT).so -OBJ = $(COMPONENT)C.o $(COMPONENT).o $(COMPONENT)S.o +OBJ = $(COMPONENT)C.o $(COMPONENT).o $(COMPONENT)S.o common.o IDL_CLEANFILES = $(COMPONENT)C.cpp $(COMPONENT)C.inl $(COMPONENT)I.h \ $(COMPONENT)S.h $(COMPONENT)C.h $(COMPONENT)S.cpp diff --git a/common/common.cpp b/common/common.cpp new file mode 100644 index 0000000..b8c4453 --- /dev/null +++ b/common/common.cpp @@ -0,0 +1,87 @@ +/** + * @author Manuel Traut <manut@mecka.net> + * @licence GPLv2 + */ + +#include <iostream> + +#include "common.h" + +int init_corba (int argc, char **argv) +{ + int ret = 0; + CORBA::Object_var obj, root_poa, naming_service; + + try { + ref.orb = CORBA::ORB_init (argc, argv); + root_poa = ref.orb->resolve_initial_references ("RootPOA"); + ref.poa = PortableServer::POA::_narrow (root_poa.in ()); + + naming_service = ref.orb->resolve_initial_references ("NameService"); + if (CORBA::is_nil (naming_service)) { + std::cerr << "can't resolve NameService" << std::endl; + ret = -EINVAL; + goto out; + } + ref.nc = CosNaming::NamingContext::_narrow (naming_service.in ()); + if (CORBA::is_nil (ref.nc)) { + std::cerr << "resolved invalid NameService object" << std::endl; + ret = -EINVAL; + goto out; + } + } catch (CORBA::Exception &e) { + std::cerr << "CORBA initialization failed: " << e << std::endl; + ret = -EINVAL; + goto out; + } + +out: + return ret; +} + +int register_digital (char *_name, Distrio_Digital_i *digital) +{ + CosNaming::Name name; + CORBA::Object_var obj; + PortableServer::ObjectId_var oid; + + if (!ref.init) { + std::cerr << "corba not initialized" << std::endl; + return -1; + } + + try { + oid = ref.poa->activate_object (digital); + obj = digital->_this (); + + /* TODO: find out how to build a tree @ the nameservice "distrio/manager" */ + name.length (1); + name[0].id = CORBA::string_dup (_name); + ref.nc->rebind (name, obj.in ()); + } catch (CORBA::Exception &e) { + std::cerr << "CORBA initialization failed: " << e << std::endl; + return -1; + } + + return 0; +} + +int run_orb () +{ + if (!ref.init) { + std::cerr << "corba not initialized" << std::endl; + return -1; + } + + try { + ref.poa_mgr = ref.poa->the_POAManager (); + ref.poa_mgr->activate (); + ref.orb->run (); + ref.orb->destroy (); + } catch (CORBA::Exception &e) { + std::cerr << "CORBA initialization failed: " << e << std::endl; + return -1; + } + + return 0; +} diff --git a/common/common.h b/common/common.h new file mode 100644 index 0000000..8070544 --- /dev/null +++ b/common/common.h @@ -0,0 +1,40 @@ +/** + * CORBA helpers + * + * - init orb + * - get reference to name service + * - register objects @ name service + * + * @author Manuel Traut <manut@mecka.net> + * @licence GPLv2 + */ + +#include <distrio_ioI.h> +#include <distrio_ioC.h> +#include <distrio_ioS.h> + +#include <orbsvcs/CosNamingC.h> + +/** + * handle to corba objects needed for registration of new objects and running + * the ORB + */ +typedef struct _corba_ref { + /** init > 0 if orb is initialized */ + int init; + CORBA::ORB_var orb; + PortableServer::POA_var poa; + PortableServer::POAManager_var poa_mgr; + CosNaming::NamingContext_var nc; +} corba_ref; + +static corba_ref ref = { + .init = 0, +}; + +/** initialize corba orb - argc, argv as passed to main() */ +int init_corba (int argc, char **argv); +/** register a digital io with a common name at the naming service */ +int register_digital (char *_name, Distrio_Digital_i *digital); +/** run the orb - function blocks until orb shutdown */ +int run_orb (void); |
