/* * account-server program. Hacked from Frank Rehberger * . */ #include #include #include #include #include #include "account.h" #include "account-skelimpl.c" /** * test for exception */ static gboolean raised_exception(CORBA_Environment *ev) { return ((ev)->_major != CORBA_NO_EXCEPTION); } /** * in case of any exception this macro will abort the process */ static void abort_if_exception(CORBA_Environment *ev, const char* mesg) { if (raised_exception (ev)) { g_error ("%s %s", mesg, CORBA_exception_id (ev)); CORBA_exception_free (ev); abort(); } } static CORBA_ORB global_orb = CORBA_OBJECT_NIL; /* global orb */ /* Is called in case of process signals. it invokes CORBA_ORB_shutdown() * function, which will terminate the processes main loop. */ static void account_server_shutdown (int sig) { CORBA_Environment local_ev[1]; CORBA_exception_init(local_ev); if (global_orb != CORBA_OBJECT_NIL) { CORBA_ORB_shutdown (global_orb, FALSE, local_ev); abort_if_exception (local_ev, "ORB shutdown failed"); } } /* Inits ORB @orb using @argv arguments for configuration. For each * consumed option from vector @argv the counter of @argc_ptr * will be decremented. Signal handler is set to call * account_server_shutdown function in case of SIGINT and SIGTERM * signals. If error occures @ev points to exception object on * return. */static void account_server_init (int *argc_ptr, char *argv[], CORBA_ORB *orb, CORBA_Environment *ev) { /* init signal handling */ signal(SIGINT, account_server_shutdown); signal(SIGTERM, account_server_shutdown); /* create Object Request Broker (ORB) */ (*orb) = CORBA_ORB_init(argc_ptr, argv, "orbit-local-orb", ev); if (raised_exception(ev)) return; } /* Creates servant and registers in context of ORB @orb. The ORB will * delegate incoming requests to specific servant object. @return * object reference. If error occures @ev points to exception object * on return. */ static Account account_server_activate_service (CORBA_ORB orb, CORBA_Environment *ev) { Account servant = CORBA_OBJECT_NIL; PortableServer_POA poa = CORBA_OBJECT_NIL; PortableServer_POAManager poa_manager = CORBA_OBJECT_NIL; /* get Portable Object Adaptor (POA) */ poa = (PortableServer_POA) CORBA_ORB_resolve_initial_references(orb, "RootPOA", ev); if (raised_exception(ev)) return CORBA_OBJECT_NIL; /* create servant in context of poa container */ servant = impl_Account__create (poa, ev); if (raised_exception(ev)) return CORBA_OBJECT_NIL; /* activate POA Manager */ poa_manager = PortableServer_POA__get_the_POAManager(poa, ev); if (raised_exception(ev)) return CORBA_OBJECT_NIL; PortableServer_POAManager_activate(poa_manager, ev); if (raised_exception(ev)) return CORBA_OBJECT_NIL; return servant; } /* Writes stringified object reference of @servant to file-stream * @stream. If error occures @ev points to exception object on * return. */ static void account_server_export_service_to_stream (CORBA_ORB orb, Account servant, FILE *stream, CORBA_Environment *ev) { CORBA_char *objref = NULL; /* write objref to file */ objref = CORBA_ORB_object_to_string (orb, servant, ev); if (raised_exception(ev)) return; /* print ior to terminal */ fprintf (stream, "%s\n", objref); fflush (stream); CORBA_free (objref); } /* Entering main loop @orb handles incoming request and delegates to * servants. If error occures @ev points to exception object on * return. */ static void account_server_run (CORBA_ORB orb, CORBA_Environment *ev) { /* enter main loop until SIGINT or SIGTERM */ CORBA_ORB_run(orb, ev); if (raised_exception(ev)) return; /* user pressed SIGINT or SIGTERM and in signal handler * CORBA_ORB_shutdown(.) has been called */ } /* Releases @servant object and finally destroys @orb. If error * occures @ev points to exception object on return. */ static void account_server_cleanup (CORBA_ORB orb, Account servant, CORBA_Environment *ev) { /* releasing managed object */ CORBA_Object_release(servant, ev); if (raised_exception(ev)) return; /* tear down the ORB */ if (orb != CORBA_OBJECT_NIL) { /* going to destroy orb.. */ CORBA_ORB_destroy(orb, ev); if (raised_exception(ev)) return; } } /* * main */ int main (int argc, char *argv[]) { Account servant = CORBA_OBJECT_NIL; CORBA_Environment ev[1]; CORBA_exception_init(ev); account_server_init (&argc, argv, &global_orb, ev); abort_if_exception(ev, "init failed"); servant = account_server_activate_service (global_orb, ev); abort_if_exception(ev, "activating service failed"); account_server_export_service_to_stream (global_orb, /* ORB */ servant, /* object */ stdout, /* stream */ ev); abort_if_exception(ev, "exporting IOR failed"); account_server_run (global_orb, ev); abort_if_exception(ev, "entering main loop failed"); account_server_cleanup (global_orb, servant, ev); abort_if_exception(ev, "cleanup failed"); exit (0); }