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