Contact us | Links

Accessing a RMI/IIOP-based CORBA object with .NET remoting

In this tutorial a .NET remoting client calls a method on a RMI/IIOP-based CORBA object: an adder. This RMI/IIOP adder provides a method to add two double summands.

This tutorial consists of the following three steps:

  1. Implementing the server
  2. Implementing the client
  3. Running the Demo

Implementing the RMI/IIOP server

In this section the RMI/IIOP adder is implemented. The following source files are created:

  • Adder.java: contains the adder interface
  • AdderImpl.java: contains the adder implementation
  • AdderServer.java: contains code for publishing the adder object

The Adder interface specifies the interface of the remote adder object:


    package Tutorial.GettingStarted;

    import java.rmi.Remote;
    import java.rmi.RemoteException;

    public interface Adder extends Remote {
    
        public double add(double arg1, double arg2) throws RemoteException;
    
    }

The AdderImpl class implements the adder functionality:


    package Tutorial.GettingStarted;

    import java.rmi.Remote;
    import java.rmi.RemoteException;
    import javax.rmi.PortableRemoteObject;

    public class AdderImpl extends PortableRemoteObject implements Adder {

        public AdderImpl() throws java.rmi.RemoteException {
            super();     // invoke rmi linking and remote object initialization
        }

        public double add(double arg1, double arg2) throws RemoteException {
            return arg1 + arg2;
        }

    }

Every remotable RMI/IIOP object must implement an interface extending java.rmi.Remote. Therefore the adder implementation class implements the Adder interface.
The AdderImpl class implements the adder functionality in the add method.

The AdderServer class creates an adder object and publishes it with a CORBA naming service:


    import javax.naming.InitialContext;
    import javax.naming.Context;
    import javax.rmi.PortableRemoteObject;
    import Tutorial.GettingStarted.Adder;
    import Tutorial.GettingStarted.AdderImpl;


    public class AdderServer {

        public static void main(String[] args) {
            try {
                
                // Instantiate the service
                AdderImpl adder = new AdderImpl();

                // publish the reference with the naming service:
                Context initialNamingContext = new InitialContext();
                initialNamingContext.rebind("adder", adder);

                System.out.println("Server Ready...");

            } catch (Exception e) {
                System.out.println("Trouble: " + e); e.printStackTrace();
            }
        }
    }

The files can be found here:

Compiling

The files are compiled in the following way:

  • javac -classpath . AdderServer.java
  • rmic -classpath . -iiop Tutorial.GettingStarted.AdderImpl

Implementing the .NET Remoting Client

Creating IDL for java types

Before creating the .NET client, the java types must be mapped to IDL. This mapping is done using the rmic tool:
rmic -idl Tutorial.GettingStarted.AdderImpl
The following interface represents the java types Adder and AdderImpl in OMG ILD:

    module Tutorial {
        module GettingStarted {

            interface Adder {
    
                double add(
                    in double arg0,
                    in double arg1 );
     
            };


        };
    };

Mapping IDL to CLS

In the next step the IDL is mapped to CLS using the IDL to CLS compiler (according to the IDL -> CLS mapping specification). The following CLS type is generated for the above IDL:


using Ch.Elca.Iiop.Idl;

namespace Tutorial.GettingStarted {

    [RepositoryIDAttribute("RMI:Tutorial.GettingStarted.Adder:0000000000000000")]
    [InterfaceTypeAttribute(IdlTypeInterface.ConcreteInterface)]
    public interface Adder : IIdlEntity {
        double add(double sum1, double sum2);
    }

}

The predefined CORBA types are provided by IIOP.NET in the file IDLToCLSCompiler\IDL\orb.idl. Copy this file into your java source directory JAVA_SOURCES_DIRECTORY.
Launch the following command in the java sources directory JAVA_SOURCES_DIRECTORY to generate the CLS for the IDL in the .NET source directory N_SOURCES:

IDLToCLSCompiler.exe -o N_SOURCES\bin adder Tutorial\GettingStarted\Adder.idl.

This creates a CLS multi module assembly containing the CLS types for the IDL.

Client code

Now we are ready to implement the client. The following listing shows the important parts of the implementation:

using System;
using System.Runtime.Remoting.Channels;
using Ch.Elca.Iiop;
using Ch.Elca.Iiop.Services;
using omg.org.CosNaming;

namespace Tutorial.GettingStarted {

    public class NClient {

        [STAThread]
        public static void Main(string[] args) {
	    try {
                string nameServiceHost = "localhost";
                int nameServicePort = 1050;

                double sum1 = Double.Parse(Console.ReadLine());
                double sum2 = Double.Parse(Console.ReadLine());
			
                // register the channel
                IiopClientChannel channel = new IiopClientChannel();
                ChannelServices.RegisterChannel(channel);

                // access COS nameing service
                CorbaInit init = CorbaInit.GetInit();
                NamingContext nameService = init.GetNameService(nameServiceHost, nameServicePort);
                NameComponent[] name = new NameComponent[] { new NameComponent("adder", "") };
                // get the reference to the adder
                Adder adder = (Adder)nameService.resolve(name);
                // call add
                double result = adder.add(sum1, sum2);
                Console.WriteLine("result: " + result);
	    } catch (Exception e) {
                Console.WriteLine("exception: " + e);
	    }
        }

    }
}

A client must register the IIOP.NET channel with the .NET remoting framework before it can access CORBA objects. After the channel registration, a reference to the CORBA nameing service, knowing the adder object, is retrieved. Afterwards the client looks up the adder at the name service. Finally it uses the adder to add sum1 and sum2.

The NClient.cs file can be downloaded from here.

Compiling

The client is compiled with the following steps:

  • copy the IIOPChannel.dll into the bin directory
  • run the following command in the source directory N_SOURCES:
    csc /r:bin\Adder.dll /r:bin\IIOPChannel.dll /out:bin\NClient.exe NClient.cs

Running the Demo

The server must be started first. This is done by the following steps:

  • start orbd -ORBInitialPort 1050
  • java -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory
    -Djava.naming.provider.url=iiop://localhost:1050
    -cp .
    AdderServer

The client is afterwards started with the follwoing command line:
NClient.exe localhost 1050

About this project

This project is maintained by Elca Informatique SA and was developed in collaboration with the Programming Languages and Runtime Systems Research Group of the ETH-Zurich as part of Dominic Ullmann's diploma thesis.

IIOP.NET Use Cases

Read the IIOP.NET success stories.

News

 
© 2003-2004 ELCA All Rights Reserved