11
Exemplo de CORBA Renato Cerqueira Departamento de Informática, PUC-Rio

Exemplo de CORB A - inf.puc-rio.brnoemi/sd-08/CORBA-exemplo.pdfInterface Java //Arquivo PropertyOperations.java public interface PropertyOperations {String name(); String get(); void

  • Upload
    ngohanh

  • View
    213

  • Download
    0

Embed Size (px)

Citation preview

Exemplo de CORBA

Renato Cerqueira

Departamento de Informática, PUC-Rio

Primeiro Exemplo

//Arquivo property.idl

interface Property { string name(); string get(); void set (in string value);};

Interface Java

//Arquivo PropertyOperations.java

public interface PropertyOperations { String name(); String get(); void set (String value);}

Classe C++

//Arquivo Property.h

class Property : virtual public CORBA::Object { …public: static inline Property_ptr _duplicate(Property_ptr p) { if(p) p -> _add_ref(); return p; } static Property_ptr _narrow(CORBA::Object_ptr); static Property_ptr _unchecked_narrow(CORBA::Object_ptr); virtual char* name() = 0; virtual char* get() = 0; virtual void set(const char* value) = 0;};

Servant em Java

import org.omg.CORBA.*;

public class PropertyServant extends PropertyPOA { private String val; PropertyServant (String value) { this.val = value; } public String name() {return “Prop Name”;} public String get() {return this.val;} public void set (String value) { this.val = value; }}

Servant em C++

class PropertyServant : virtual public POA_Property, virtual public PortableServer::RefCountServantBase {… CORBA::String_var val;public: virtual char* name() throw(CORBA::SystemException) { return CORBA::string_dup(”Prop Name"); }

virtual char* get() throw(CORBA::SystemException) { return CORBA::string_dup(val); }

virtual void set(const char* value) throw(CORBA::SystemException) { val = value; }};

Servidor em Java

public class Server { public static void main (String args[]) { try { ORB orb = ORB.init(args, null); PropertyServant serv = new PropertyServant(“Hello World”); CORBA.Object p = orb.resolve_initial_references("RootPOA"); POA poa = POAHelper.narrow(p); poa.the_POAManager().activate(); CORBA.Object obj = poa.servant_to_reference(serv); System.out.println(orb.object_to_string(obj)); orb.run(); } catch … { …} }}

Servidor em C++

int main (int argc, char** argv) { CORBA::ORB_var orb = CORBA::ORB_init(argc,argv); CORBA::Object_var obj; obj = orb->resolve_initial_references(“RootPOA”); PortableServer::POA_var poa = PortableServer::POA::_narrow(obj); PortableServer::POAManager_var mgr = poa->the_Manager(); mgr->activate(); PropertyServant servant (“Hello World!”); Property_var object = servant._this(); CORBA::String_var str = orb->object_to_string(object); cout << str << endl; orb->run(); return 0;}

Cliente em Java

public class Client { public static void main (String args[]) { try { ORB orb = ORB.init(args, null); CORBA.Object obj = orb.string_to_object(args[0]); Property prop = PropertyHelper.narrow(obj); if (prop == null) { System.err.println("object reference is of wrong type"); System.exit(-1); } System.out.println(prop.name()); System.out.println(prop.get()); prop.set("New Value"); System.out.println(prop.get()); } catch (…) { … } }}

Cliente em C++

int main (int argc, char** argv) { try { CORBA::ORB_var orb = CORBA::ORB_init(argc,argv); CORBA::Object_var obj = orb->string_to_object(argv[1]); Property_var prop = Property::_narrow(obj); if (CORBA::is_nil(prop)) { //Erro … } cout << prop->name() << endl << prop->get() << endl; prop->set("New Value"); cout << prop->get() << endl; } catch (…) { … } return 0;}

Mecanismo Tie

template<class T>class POA_Property_tie : virtual public POA_Property { T* ptr_; CORBA::Boolean rel_;public: POA_Property_tie(T& t); POA_Property_tie(T* p, CORBA::Boolean release = true); virtual ~POA_Property_tie() { if(rel_) delete ptr_; CORBA::release(poa_); } virtual char* name() throw(CORBA::SystemException) { return ptr_ -> name(); } virtual char* get() throw(CORBA::SystemException) { return ptr_ -> get(); } virtual void set(const char* value) throw(CORBA::SystemException) { ptr_ -> set(value); }};