sources:
Aufgabe2.cpp (7.7k)
Aufgabe2.h (2.9k)
Client.cpp (1.8k)
Helper.h (741 bytes)
Server.cpp (3.7k)
aufgabe2.idl (85 bytes)


website:
more info here
studies/corba/Corba-Code3/Server.cpp
download file

  1 // ////////////////////////////////////////////////////////
  2 // Lecture on the CORBA Component Model, summer term 2003
  3 // Assignment 3, Stephan Brumme, 702544
  4 //
  5 // CORBA server that provides an operation to reverse
  6 // a string, can be reached via the Naming Service
  7 //
  8
  9 #include <iostream>
 10 using namespace std;
 11
 12 #include "Helper.h"
 13
 14 // IDL compiler generated a skeleton
 15 #include "Aufgabe2.h"
 16 using namespace POA_Aufgabe2;
 17 // CORBA's naming service
 18 #include <coss/CosNaming.h>
 19
 20
 21
 22 // the server
 23 class Server_Impl : public virtual Server
 24 {
 25 public:
 26     // IDL: string reverse(in string message);
 27     char* reverse(const char* message);
 28 };
 29
 30
 31 // reverse a given message
 32 char* Server_Impl::reverse(const char* message)
 33 {
 34     // some nice info
 35     cout << "Invoked by client, incoming: '" << message << "'";
 36
 37     // create new string
 38     const unsigned int length = strlen(message);
 39     char* result = CORBA::string_alloc(length);
 40     if (result == NULL)
 41         return result;
 42
 43     // zero-terminated !
 44     result[length] = 0;
 45
 46     // reverse the message
 47     for (unsigned int i = 0; i < length; i++)
 48         result[i] = message[length-i-1];
 49
 50     // again some info
 51     cout << " - returning: '" << result << "'" << endl;
 52     // done !
 53     return result;
 54 }
 55
 56
 57
 58
 59 // here we go ...
 60 int main(int argc, char* argv[])
 61 {
 62     try
 63     {
 64         // initialize CORBA
 65         CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
 66
 67         //////////////////////////////////////
 68         // POA related stuff
 69         cout << "Initialize POA ..." << endl;
 70         // first, locate the RootPOA
 71         CORBA::Object_var poaObject = orb->resolve_initial_references("RootPOA");
 72
 73         // gain access to the POA server and its manager
 74         PortableServer::POA_var poa = PortableServer::POA::_narrow(poaObject);
 75         CorbaAssert(poa, "Failed to obtain POA server.");
 76
 77         PortableServer::POAManager_var poaManager = poa->the_POAManager();
 78         CorbaAssert(poaManager, "Failed to obtain POA manager.");
 79
 80         //////////////////////////////////////
 81         // Setup server
 82         cout << "Initialize server ..." << endl;
 83         // new server object
 84         Server_Impl* server = new Server_Impl;
 85         // generate unique server ID
 86         PortableServer::ObjectId_var objectId = poa->activate_object(server);
 87         // extract reference
 88         CORBA::Object_var objectReference = poa->id_to_reference(objectId.in());
 89
 90         // for debug use: print IOR
 91         cout << orb->object_to_string(objectReference.in()) << endl;
 92
 93         //////////////////////////////////////
 94         // Connect to Naming Service
 95         cout << "Resolve Naming Service ..." << endl;
 96         // resolve naming service
 97         CORBA::Object_var namingService = orb->resolve_initial_references("NameService");
 98         // not found ?
 99         CorbaAssert(namingService, "Couldn't locate the Naming Service.");
100
101         // get the naming context
102         CosNaming::NamingContext_var namingContext = CosNaming::NamingContext::_narrow(namingService);
103         // not found ?
104         CorbaAssert(namingContext, "Couldn't locate the Naming Service.");
105
106         // my Windows(R)(TM)(C) login name: "Stephan.Brumme"
107         CosNaming::Name newName;
108         newName.length(1);
109         newName[0].id   = CORBA::string_dup("Stephan.Brumme");
110         newName[0].kind = CORBA::string_dup("CCM");
111
112         // register server at the naming service
113         try
114         {
115             //namingContext->bind(newName, objectReference);
116             namingContext->rebind(newName, objectReference);
117         }
118         catch (CosNaming::NamingContext::NotFound& e)
119         {
120             CorbaError("Naming context not found.");
121         }
122         catch (CosNaming::NamingContext::InvalidName& e)
123         {
124             CorbaError("Invalid name.");
125         }
126
127         cout << "Server is ready." << endl;
128
129         poaManager->activate();
130         orb->run();
131
132         // game over
133         poa->destroy(true, true);
134         delete server;
135        
136         return 0;
137     }
138     catch (CORBA::Exception& e)
139     {
140         CorbaError("Corba failed to run properly.");
141     }
142 }
143