1 Aniruddha Gokhale Vanderbilt University, Spring 2003 Intro to TAO Programming Study the Hello...

15
Aniruddha Gokhale Vanderbilt University, Spring 2003 1 Intro to TAO Programming • Study the Hello example in ACE_wrappers/TAO/tests – Show IDL definition, servant, server, and client code – Show some IDL compiler generated code – Show how to compile and run the test • Number of examples available under ACE_wrappers/TAO/tests and ACE_wrappers/TAO/examples • Understand the IDL->C++ Mapping for memory management issues

Transcript of 1 Aniruddha Gokhale Vanderbilt University, Spring 2003 Intro to TAO Programming Study the Hello...

Page 1: 1 Aniruddha Gokhale Vanderbilt University, Spring 2003 Intro to TAO Programming Study the Hello example in ACE_wrappers/TAO/tests –Show IDL definition,

Aniruddha Gokhale Vanderbilt University, Spring 20031

Intro to TAO Programming• Study the Hello example in

ACE_wrappers/TAO/tests– Show IDL definition, servant, server, and

client code– Show some IDL compiler generated code– Show how to compile and run the test

• Number of examples available under ACE_wrappers/TAO/tests and ACE_wrappers/TAO/examples

• Understand the IDL->C++ Mapping for memory management issues

Page 2: 1 Aniruddha Gokhale Vanderbilt University, Spring 2003 Intro to TAO Programming Study the Hello example in ACE_wrappers/TAO/tests –Show IDL definition,

Aniruddha Gokhale Vanderbilt University, Spring 20032

IDL definitionmodule Test{ /// A very simple interface interface Hello { /// Return a simple string string get_string ();

/// A method to shutdown the ORB /// This method is used to simplify shutdown process oneway void shutdown (); };};

Page 3: 1 Aniruddha Gokhale Vanderbilt University, Spring 2003 Intro to TAO Programming Study the Hello example in ACE_wrappers/TAO/tests –Show IDL definition,

Aniruddha Gokhale Vanderbilt University, Spring 20033

IDL Compiler-generated Skeleton Header/* ONLY RELEVANT DETAILS SHOWN */// module becomes namespacenamespace POA_Test { // interface becomes a class class Hello: public PortableServer::ServantBase { public:

// returns an object reference. Used for implicit activation ::Test::Hello*_this ();

// servant implements this operation virtual char * get_string () throw ((CORBA::SystemException)) = 0;};

Page 4: 1 Aniruddha Gokhale Vanderbilt University, Spring 2003 Intro to TAO Programming Study the Hello example in ACE_wrappers/TAO/tests –Show IDL definition,

Aniruddha Gokhale Vanderbilt University, Spring 20034

IDL Compiler-generated Stub Header File/* ONLY RELEVANT DETAILS SHOWN */// module becomes namespacenamespace Test { /* note how this is different */ // declare a smart pointer class Hello_var { /* details not shown */ };

// interface becomes a class class Hello: public virtual CORBA::Object { public: typedef Hello_ptr _ptr_type; typedef Hello_var _var_type; // The static operations. static Hello_ptr _duplicate (Hello_ptr obj); static Hello_ptr _narrow (CORBA::Object_ptr obj);

Page 5: 1 Aniruddha Gokhale Vanderbilt University, Spring 2003 Intro to TAO Programming Study the Hello example in ACE_wrappers/TAO/tests –Show IDL definition,

Aniruddha Gokhale Vanderbilt University, Spring 20035

IDL Compiler-generated Stub header file (contd)

static Hello_ptr _nil (void) { return (Hello_ptr)0; } // is_a virtual CORBA::Boolean _is_a (const char *type_id);

// operation virtual char * get_string() throw ((CORBA::SystemException)); }; // end of class}; // end of namespace

Page 6: 1 Aniruddha Gokhale Vanderbilt University, Spring 2003 Intro to TAO Programming Study the Hello example in ACE_wrappers/TAO/tests –Show IDL definition,

Aniruddha Gokhale Vanderbilt University, Spring 20036

Servant Header (Hello.h)// include the skeleton header#include “TestS.h“

// our implementationclass Hello: public POA_Hello::Test{public: // ctor Hello ();

virtual char * get_string () throw ((CORBA::SystemException));

};

Page 7: 1 Aniruddha Gokhale Vanderbilt University, Spring 2003 Intro to TAO Programming Study the Hello example in ACE_wrappers/TAO/tests –Show IDL definition,

Aniruddha Gokhale Vanderbilt University, Spring 20037

Servant Impl (Hello.cpp)#include “Hello.h"

// operationChar * Hello::get_string() throw ((CORBA::SystemException)) { return CORBA::string_dup (“Hello There”);}

Page 8: 1 Aniruddha Gokhale Vanderbilt University, Spring 2003 Intro to TAO Programming Study the Hello example in ACE_wrappers/TAO/tests –Show IDL definition,

Aniruddha Gokhale Vanderbilt University, Spring 20038

Server Code (server.cpp)#include “Hello.h"

int main (int argc, char* argv[])

{

try {

// initialize the ORB

CORBA::ORB_var orb =

CORBA::ORB_init (argc, argv,

"" /* the ORB name, it can be anything! */ );

// Get a reference to the RootPOA

CORBA::Object_var poa_object =

orb->resolve_initial_references ("RootPOA“);

Page 9: 1 Aniruddha Gokhale Vanderbilt University, Spring 2003 Intro to TAO Programming Study the Hello example in ACE_wrappers/TAO/tests –Show IDL definition,

Aniruddha Gokhale Vanderbilt University, Spring 20039

Server Code contd // narrow down to the correct reference PortableServer::POA_var poa = PortableServer::POA::_narrow (poa_object.in ());

// Set a POA Manager PortableServer::POAManager_var poa_manager = poa->the_POAManager ();

// Activate the POA Manager (recall the state transition // diagram for POA Manager. By activating we are telling // the POA manager to start accepting requests poa_manager->activate ();

Page 10: 1 Aniruddha Gokhale Vanderbilt University, Spring 2003 Intro to TAO Programming Study the Hello example in ACE_wrappers/TAO/tests –Show IDL definition,

Aniruddha Gokhale Vanderbilt University, Spring 200310

Server Code contd // Create the servant Hello hello; // This is not the best way, but for the sake // of simplicity lets have it that way.

// Activate it and obtain the object reference Test::Hello_var h = hello._this ();

CORBA::String_var ior =

orb->object_to_string (h.in ());

// Write the IOR to a file. More sophisticated methods available

Page 11: 1 Aniruddha Gokhale Vanderbilt University, Spring 2003 Intro to TAO Programming Study the Hello example in ACE_wrappers/TAO/tests –Show IDL definition,

Aniruddha Gokhale Vanderbilt University, Spring 200311

Server Code contd poa_manager->activate ();

// Run the orb orb->run (); // Destroy the POA, waiting until the destruction terminates poa->destroy (1, 1); orb->destroy (); } // catch any exceptions beyond this}

Page 12: 1 Aniruddha Gokhale Vanderbilt University, Spring 2003 Intro to TAO Programming Study the Hello example in ACE_wrappers/TAO/tests –Show IDL definition,

Aniruddha Gokhale Vanderbilt University, Spring 200312

Client code (client.cpp)int main (int argc, char *argv[]) { try { // initialize the ORB CORBA::ORB_var orb = CORBA::init (argc, argv, “”);

// Make a CORBA object

CORBA::Object_var tmp =

orb->string_to_object(ior);

// Create an Invocation Object Test::Hello_var hello =

Test::Hello::_narrow(tmp.in ());

// Error checking

if (CORBA::is_nil (hello.in ()))

Page 13: 1 Aniruddha Gokhale Vanderbilt University, Spring 2003 Intro to TAO Programming Study the Hello example in ACE_wrappers/TAO/tests –Show IDL definition,

Aniruddha Gokhale Vanderbilt University, Spring 200313

Client code contd

// now invoke a request CORBA::String_var the_string = hello->get_string (); // check return value and print it // Shutdown the server hello->shutdown ();

} // put catch clauses here

} // end of main

Page 14: 1 Aniruddha Gokhale Vanderbilt University, Spring 2003 Intro to TAO Programming Study the Hello example in ACE_wrappers/TAO/tests –Show IDL definition,

Aniruddha Gokhale Vanderbilt University, Spring 200314

MakefileIDL_FILES = TestIDL_SRC = TestC.cpp TestS.cppBIN = client server

SRC = $(addsuffix .cpp, $(BIN) Hello) $(IDL_SRC)

CLIENT_OBJS = client.o TestC.oSERVER_OBJS = server.o Hello.o $(IDL_SRC:.cpp=.o)

TAO_IDLFLAGS += -Ge 1server: $(addprefix $(VDIR),$(SERVER_OBJS))

$(LINK.cc) $(LDFLAGS) -o $@ $^ $(TAO_SRVR_LIBS) $(POSTLINK)

client: $(addprefix $(VDIR),$(CLIENT_OBJS))$(LINK.cc) $(LDFLAGS) -o $@ $^ $(TAO_CLNT_LIBS) $(POSTLINK)

Page 15: 1 Aniruddha Gokhale Vanderbilt University, Spring 2003 Intro to TAO Programming Study the Hello example in ACE_wrappers/TAO/tests –Show IDL definition,

Aniruddha Gokhale Vanderbilt University, Spring 200315

Makefile (Contd.)server: $(addprefix (VDIR),$(SERVER_OBJS))

$(LINK.cc) $(LDFLAGS) -o $@ $^ $(TAO_SRVR_LIBS) $(POSTLINK)

client: $(addprefix $(VDIR),$(CLIENT_OBJS))

$(LINK.cc) $(LDFLAGS) -o $@ $^ $(TAO_CLNT_LIBS) $(POSTLINK)