CORBA

|

CORBA

CODIGO:

Calculadora.idl

module ejemplo {
interface Calculadora {
long sumar(in long a, in long b);
long restar(in long a, in long b);
long multiplicar(in long a, in long b);
long dividir(in long a, in long b);
readonly attribute long contadorOperaciones;
};
};

*****************************

EjemploCliente

import org.omg.CosNaming.*;

import ejemplo.*; // Importar las clases e interfaces generados a partir del IDL

public class EjemploCliente {

public static void main(String[] args) {
try {
// 1. Inicializar ORB
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);

// 2.1 Obtener instancia del servidor de nombres (initial naming context)
org.omg.CORBA.Object ncCorba = orb.resolve_initial_references("NameService");
NamingContextExt nc = NamingContextExtHelper.narrow(ncCorba);

// 2.2 Construir el nombre del objeto y obtener ref. desde servidor de nombres
org.omg.CORBA.Object calculadoraCorba = nc.resolve(nc.to_name("Calculadora"));

// 2.4 Convertir el objeto CORBA al tipo Calculadora (narrow)
Calculadora calculadora = CalculadoraHelper.narrow(calculadoraCorba);

// 3 Invocar métodos remotos
int resultado;
resultado = calculadora.sumar(100, 20);
System.out.println("Sumar 100 y 20 : " + resultado);

resultado = calculadora.restar(100, 20);
System.out.println("Restar 100 y 20 : " + resultado);

resultado = calculadora.multiplicar(100, 20);
System.out.println("Multiplicar 100 y 20 : " + resultado);

resultado = calculadora.dividir(100, 20);
System.out.println("Dividir 100 y 20 : " + resultado);

int contador = calculadora.contadorOperaciones();
System.out.println("Numero de operaciones realizadas : " + contador);
}
catch (Exception e) {
System.out.println("Error:" + e.getMessage());
System.exit(1);
}
}
} // Fin EjemploCliente

************************************
EjemploServidor

import org.omg.PortableServer.*;
import org.omg.CosNaming.*;


import ejemplo.*; // Importar las clases e interfaces generados a partir del IDL

public class EjemploServidor {

public static void main(String[] args) {
try {
// 1. Inicializar ORB
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);

// 2.1 Obtener POA raiz
POA raizPOA = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));

// 2.2 Activar el POA manager
raizPOA.the_POAManager().activate();

// 3.1 Crear instancia de la implementación (servant)
CalculadoraImpl calculadoraServant = new CalculadoraImpl();

// 3.2 Registrar en el POA y obtener referencia al objeto (IOR)
org.omg.CORBA.Object calculadoraCorba = raizPOA.servant_to_reference
(calculadoraServant);

// 4.1 Obtener el initial naming context
org.omg.CORBA.Object ncCorba = orb.resolve_initial_references("NameService");
NamingContextExt nc = NamingContextExtHelper.narrow(ncCorba);

// 4.2 Asociar un nombre (en el primer nivel)
nc.rebind(nc.to_name("Calculadora"), calculadoraCorba);

// 5 Quedar a la espera de peticiones
System.out.println("Proceso servidor en espera ... ");
orb.run();
} catch (Exception e) {
System.out.println("Error:" + e.getMessage());
System.exit(1);

}
}
} // Fin EjemploServidor

****************************

CalculadoraImpl

import ejemplo.CalculadoraPOA;
// Importar la superclase generada a partir del IDL


public class CalculadoraImpl extends CalculadoraPOA {
private int contador;

public CalculadoraImpl(){
contador = 0;
}

public int sumar(int a, int b){
System.out.println("Servant CalculadoraImpl:
operacion sumar("+a+","+b+")");

contador++;
return (a + b);
}

public int restar(int a, int b){
System.out.println("Servant CalculadoraImpl:
operacion restar("+a+","+b+")");

contador++;
return (a - b);
}

public int multiplicar(int a, int b){
System.out.println("Servant CalculadoraImpl:
operacion multiplicar("+a+","+b+")");

contador++;
return (a * b);
}


public int dividir(int a, int b){
System.out.println("Servant CalculadoraImpl:
operacion dividir("+a+","+b+")");

contador++;
return (a / b);
}

public int contadorOperaciones(){
System.out.println("Servant CalculadoraImpl:
operacion contadorOperaciones()");

return (contador);
}
}
************************************
_CalculadoraStub

package ejemplo;


/**
* ejemplo/_CalculadoraStub.java .
* Generated by the IDL-to-Java compiler (portable), version "3.2"
* from calculadora.idl
* martes 2 de diciembre de 2008 14H07' CET
*/

public class _CalculadoraStub extends
org.omg.CORBA.portable.ObjectImpl implements ejemplo.Calculadora
{

public int sumar (int a, int b)
{
org.omg.CORBA.portable.InputStream $in = null;
try {
org.omg.CORBA.portable.OutputStream $out =
_request ("sumar", true);
$out.write_long (a);
$out.write_long (b);
$in = _invoke ($out);
int $result = $in.read_long ();
return $result;
} catch (org.omg.CORBA.portable.ApplicationException $ex) {
$in = $ex.getInputStream ();
String _id = $ex.getId ();
throw new org.omg.CORBA.MARSHAL (_id);
} catch (org.omg.CORBA.portable.RemarshalException $rm) {
return sumar (a, b );
} finally {
_releaseReply ($in);
}
} // sumar

public int restar (int a, int b)
{
org.omg.CORBA.portable.InputStream $in = null;
try {
org.omg.CORBA.portable.OutputStream $out = _request ("restar", true);
$out.write_long (a);
$out.write_long (b);
$in = _invoke ($out);
int $result = $in.read_long ();
return $result;
} catch (org.omg.CORBA.portable.ApplicationException $ex) {
$in = $ex.getInputStream ();
String _id = $ex.getId ();
throw new org.omg.CORBA.MARSHAL (_id);
} catch (org.omg.CORBA.portable.RemarshalException $rm) {
return restar (a, b );
} finally {
_releaseReply ($in);
}
} // restar

public int multiplicar (int a, int b)
{
org.omg.CORBA.portable.InputStream $in = null;
try {
org.omg.CORBA.portable.OutputStream $out = _request ("multiplicar", true);
$out.write_long (a);
$out.write_long (b);
$in = _invoke ($out);
int $result = $in.read_long ();
return $result;
} catch (org.omg.CORBA.portable.ApplicationException $ex) {
$in = $ex.getInputStream ();
String _id = $ex.getId ();
throw new org.omg.CORBA.MARSHAL (_id);
} catch (org.omg.CORBA.portable.RemarshalException $rm) {
return multiplicar (a, b );
} finally {
_releaseReply ($in);
}
} // multiplicar

public int dividir (int a, int b)
{
org.omg.CORBA.portable.InputStream $in = null;
try {
org.omg.CORBA.portable.OutputStream $out = _request ("dividir", true);
$out.write_long (a);
$out.write_long (b);
$in = _invoke ($out);
int $result = $in.read_long ();
return $result;
} catch (org.omg.CORBA.portable.ApplicationException $ex) {
$in = $ex.getInputStream ();
String _id = $ex.getId ();
throw new org.omg.CORBA.MARSHAL (_id);
} catch (org.omg.CORBA.portable.RemarshalException $rm) {
return dividir (a, b );
} finally {
_releaseReply ($in);
}
} // dividir

public int contadorOperaciones ()
{
org.omg.CORBA.portable.InputStream $in = null;
try {
org.omg.CORBA.portable.OutputStream $out = _request ("_get_contadorOperaciones", true);
$in = _invoke ($out);
int $result = $in.read_long ();
return $result;
} catch (org.omg.CORBA.portable.ApplicationException $ex) {
$in = $ex.getInputStream ();
String _id = $ex.getId ();
throw new org.omg.CORBA.MARSHAL (_id);
} catch (org.omg.CORBA.portable.RemarshalException $rm) {
return contadorOperaciones ( );
} finally {
_releaseReply ($in);
}
} // contadorOperaciones

// Type-specific CORBA::Object operations
private static String[] __ids = {
"IDL:ejemplo/Calculadora:1.0"};

public String[] _ids ()
{
return (String[])__ids.clone ();
}

private void readObject (java.io.ObjectInputStream s) throws java.io.IOException
{
String str = s.readUTF ();
String[] args = null;
java.util.Properties props = null;
org.omg.CORBA.Object obj = org.omg.CORBA.ORB.init (args, props).string_to_object (str);
org.omg.CORBA.portable.Delegate delegate = ((org.omg.CORBA.portable.ObjectImpl) obj)._get_delegate ();
_set_delegate (delegate);
}

private void writeObject (java.io.ObjectOutputStream s) throws java.io.IOException
{
String[] args = null;
java.util.Properties props = null;
String str = org.omg.CORBA.ORB.init (args, props).object_to_string (this);
s.writeUTF (str);
}
} // class _CalculadoraStub

*************************
Calculadora

package ejemplo;


/**
* ejemplo/Calculadora.java .
* Generated by the IDL-to-Java compiler (portable), version "3.2"
* from calculadora.idl
* martes 2 de diciembre de 2008 14H07' CET
*/

public interface Calculadora extends CalculadoraOperations, org.omg.CORBA.Object, org.omg.CORBA.portable.IDLEntity
{
} // interface Calculadora


****************************************
_CalculadoraHelper
package ejemplo;


/**
* ejemplo/CalculadoraHelper.java .
* Generated by the IDL-to-Java compiler (portable), version "3.2"
* from calculadora.idl
* martes 2 de diciembre de 2008 14H07' CET
*/

abstract public class CalculadoraHelper
{
private static String _id = "IDL:ejemplo/Calculadora:1.0";

public static void insert (org.omg.CORBA.Any a, ejemplo.Calculadora that)
{
org.omg.CORBA.portable.OutputStream out = a.create_output_stream ();
a.type (type ());
write (out, that);
a.read_value (out.create_input_stream (), type ());
}

public static ejemplo.Calculadora extract (org.omg.CORBA.Any a)
{
return read (a.create_input_stream ());
}

private static org.omg.CORBA.TypeCode __typeCode = null;
synchronized public static org.omg.CORBA.TypeCode type ()
{
if (__typeCode == null)
{
__typeCode = org.omg.CORBA.ORB.init ().create_interface_tc (ejemplo.CalculadoraHelper.id (), "Calculadora");
}
return __typeCode;
}

public static String id ()
{
return _id;
}

public static ejemplo.Calculadora read (org.omg.CORBA.portable.InputStream istream)
{
return narrow (istream.read_Object (_CalculadoraStub.class));
}

public static void write (org.omg.CORBA.portable.OutputStream ostream, ejemplo.Calculadora value)
{
ostream.write_Object ((org.omg.CORBA.Object) value);
}

public static ejemplo.Calculadora narrow (org.omg.CORBA.Object obj)
{
if (obj == null)
return null;
else if (obj instanceof ejemplo.Calculadora)
return (ejemplo.Calculadora)obj;
else if (!obj._is_a (id ()))
throw new org.omg.CORBA.BAD_PARAM ();
else
{
org.omg.CORBA.portable.Delegate delegate = ((org.omg.CORBA.portable.ObjectImpl)obj)._get_delegate ();
ejemplo._CalculadoraStub stub = new ejemplo._CalculadoraStub ();
stub._set_delegate(delegate);
return stub;
}
}

public static ejemplo.Calculadora unchecked_narrow (org.omg.CORBA.Object obj)
{
if (obj == null)
return null;
else if (obj instanceof ejemplo.Calculadora)
return (ejemplo.Calculadora)obj;
else
{
org.omg.CORBA.portable.Delegate delegate = ((org.omg.CORBA.portable.ObjectImpl)obj)._get_delegate ();
ejemplo._CalculadoraStub stub = new ejemplo._CalculadoraStub ();
stub._set_delegate(delegate);
return stub;
}
}

}

CalculadoraHolder
package ejemplo;

/**
* ejemplo/CalculadoraHolder.java .
* Generated by the IDL-to-Java compiler (portable), version "3.2"
* from calculadora.idl
* martes 2 de diciembre de 2008 14H07' CET
*/

public final class CalculadoraHolder implements org.omg.CORBA.portable.Streamable
{
public ejemplo.Calculadora value = null;

public CalculadoraHolder ()
{
}

public CalculadoraHolder (ejemplo.Calculadora initialValue)
{
value = initialValue;
}

public void _read (org.omg.CORBA.portable.InputStream i)
{
value = ejemplo.CalculadoraHelper.read (i);
}

public void _write (org.omg.CORBA.portable.OutputStream o)
{
ejemplo.CalculadoraHelper.write (o, value);
}

public org.omg.CORBA.TypeCode _type ()
{
return ejemplo.CalculadoraHelper.type ();
}

}

CalculadoraOperations
package ejemplo;


/**
* ejemplo/CalculadoraOperations.java .
* Generated by the IDL-to-Java compiler (portable), version "3.2"
* from calculadora.idl
* martes 2 de diciembre de 2008 14H07' CET
*/

public interface CalculadoraOperations
{
int sumar (int a, int b);
int restar (int a, int b);
int multiplicar (int a, int b);
int dividir (int a, int b);
int contadorOperaciones ();
} // interface CalculadoraOperations


CalculadoraPOA
package ejemplo;


/**
* ejemplo/CalculadoraPOA.java .
* Generated by the IDL-to-Java compiler (portable), version "3.2"
* from calculadora.idl
* martes 2 de diciembre de 2008 14H07' CET
*/

public abstract class CalculadoraPOA extends org.omg.PortableServer.Servant
implements ejemplo.CalculadoraOperations, org.omg.CORBA.portable.InvokeHandler
{

// Constructors

private static java.util.Hashtable _methods = new java.util.Hashtable ();
static
{
_methods.put ("sumar", new java.lang.Integer (0));
_methods.put ("restar", new java.lang.Integer (1));
_methods.put ("multiplicar", new java.lang.Integer (2));
_methods.put ("dividir", new java.lang.Integer (3));
_methods.put ("_get_contadorOperaciones", new java.lang.Integer (4));
}

public org.omg.CORBA.portable.OutputStream _invoke (String $method,
org.omg.CORBA.portable.InputStream in,
org.omg.CORBA.portable.ResponseHandler $rh)
{
org.omg.CORBA.portable.OutputStream out = null;
java.lang.Integer __method = (java.lang.Integer)_methods.get ($method);
if (__method == null)
throw new org.omg.CORBA.BAD_OPERATION (0, org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE);

switch (__method.intValue ())
{
case 0: // ejemplo/Calculadora/sumar
{
int a = in.read_long ();
int b = in.read_long ();
int $result = (int)0;
$result = this.sumar (a, b);
out = $rh.createReply();
out.write_long ($result);
break;
}

case 1: // ejemplo/Calculadora/restar
{
int a = in.read_long ();
int b = in.read_long ();
int $result = (int)0;
$result = this.restar (a, b);
out = $rh.createReply();
out.write_long ($result);
break;
}

case 2: // ejemplo/Calculadora/multiplicar
{
int a = in.read_long ();
int b = in.read_long ();
int $result = (int)0;
$result = this.multiplicar (a, b);
out = $rh.createReply();
out.write_long ($result);
break;
}

case 3: // ejemplo/Calculadora/dividir
{
int a = in.read_long ();
int b = in.read_long ();
int $result = (int)0;
$result = this.dividir (a, b);
out = $rh.createReply();
out.write_long ($result);
break;
}

case 4: // ejemplo/Calculadora/_get_contadorOperaciones
{
int $result = (int)0;
$result = this.contadorOperaciones ();
out = $rh.createReply();
out.write_long ($result);
break;
}

default:
throw new org.omg.CORBA.BAD_OPERATION (0, org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE);
}

return out;
} // _invoke

// Type-specific CORBA::Object operations
private static String[] __ids = {
"IDL:ejemplo/Calculadora:1.0"};

public String[] _all_interfaces (org.omg.PortableServer.POA poa, byte[] objectId)
{
return (String[])__ids.clone ();
}

public Calculadora _this()
{
return CalculadoraHelper.narrow(
super._this_object());
}

public Calculadora _this(org.omg.CORBA.ORB orb)
{
return CalculadoraHelper.narrow(
super._this_object(orb));
}

0 comentarios:

Publicar un comentario