Java Corba Client Server Communications


Corba (Common Object Request Broker Architecture) is similar to RMI, only complicated and circumferential, which is also the reason, why it takes in practice only a small niche.

Based on a middleware (ORB – Object Request Broker), which is responsible for managing the service and the authentication of the user, Users can take advantage of services, running on another PC. The system has the following advantages:

  • OS independent
  • programming language independent, by the IDL (Interface Definition Language), With this interface, users can communicate with the middleware
  • skaliebar, Services can be easily replicated and resources (Server) be added, loose binding of services
  • migratable, because the ORB manages services of the name and not the IP, which of course is at the same URLs
  • concurrently, Processes run in their own threads
  • robust, through central Failure Handling
  • connectionless, Service can be used at any time, as often

Client:

public class Client
{
    public static void main(String[] args) throws InvalidName, NotFound, CannotProceed, org.omg.CosNaming.NamingContextPackage.InvalidName
    {
            String[] orbArgs = new String[] {
                "-ORBInitialPort", "1050",
        };

        // Object Request Broker (ORB) initialisieren
        ORB orb = ORB.init(orbArgs, null);

        // NameService ermitteln
        org.omg.CORBA.Object namingRef = orb.resolve_initial_references("NameService");
        NamingContextExt naming = NamingContextExtHelper.narrow(namingRef);

        // Referenz auffinden
        org.omg.CORBA.Object parkhausRef = naming.resolve_str("Parkhaus");
        Parkhaus parkhaus = (Parkhaus) ParkhausHelper.narrow(parkhausRef);

                 BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
                 String fromUser = null;
                 String responseOrb = null;
                 while (true) {
                        //schicke Input zum ORB
                            try {
                                fromUser = stdIn.readLine();
                            } catch (IOException ex) {
                                Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
                            }
                            responseOrb = handleUserinput(fromUser,  parkhaus);
                            if(responseOrb == null)
                            {
                                System.out.println("sry, empty response");
                            }
                            else if(responseOrb.equals("quit"))
                            {
                                //beenden
                                break;
                            }                           
                            else
                            {
                                System.out.println(responseOrb);
                            }


                }
                try {
                    stdIn.close();
                } catch (IOException ex) {
                    Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
                }
    }



    public static String handleUserinput(String fromUser,  Parkhaus parkhaus) throws NumberFormatException{
        String ServerResponse = null;
        Integer parameter1 = null;
        String[] wordsFromUser = Pattern.compile(" ").split(fromUser);
        if (wordsFromUser.length == 1) {
            fromUser = wordsFromUser[0];
        } else if (wordsFromUser.length > 1) {
            fromUser = wordsFromUser[0];
            parameter1 = Integer.parseInt(wordsFromUser[1]);
        }
        if (parameter1 != null) {
            if (fromUser.equals("in")) {
                ServerResponse = parkhaus.einfahren(parameter1);
            } else if (fromUser.equals("out")) {
                ServerResponse = parkhaus.ausfahren(parameter1);
            }
        } else {
            if (fromUser.equals("free")) {
                ServerResponse = parkhaus.frei();
            } else if (fromUser.equals("quit")) {
                ServerResponse = "quit";
            } else {
                //do nothing
            }
        }
        return ServerResponse;
    }
}

Server:

public class Server
{
    public static void main(String[] args)
    {
        ServerThread _server = new ServerThread();
        _server.start();
        try {
                Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
    }
  
}

ServerThread:

public class ServerThread extends Thread {

    private ORB _orb;
    private ParkhausServant _parkhaus;

    public void run() {
        try {
            // run "orbd -ORBInitialPort 1050"

            // ORB initialisieren
            String[] orbArgs = new String[] { "-ORBInitialPort", "1050"};

            _orb = ORB.init(orbArgs, null);

            // Root Objekt ermitteln und aktivieren
            org.omg.CORBA.Object rootRef = _orb.resolve_initial_references("RootPOA");
            POA rootPOA = POAHelper.narrow(rootRef); // Cast (POA) rootRef
            rootPOA.the_POAManager().activate();

            // NameService ermitteln
            org.omg.CORBA.Object namingRef = _orb.resolve_initial_references("NameService");
            NamingContextExt naming = NamingContextExtHelper.narrow(namingRef); // Cast (NamingContextExt)

            // Server-Objekt erstellen und binden
            _parkhaus = new ParkhausServant();
            org.omg.CORBA.Object serviceRef = rootPOA.servant_to_reference(_parkhaus);
            NameComponent[] path = naming.to_name("Parkhaus");
            naming.rebind(path, serviceRef);

            // Auf Aufrufe warten
            _orb.run();

            System.out.println("Server is running ...");
        } catch (Exception e) {
            System.out.println("Error!");
        }
        System.out.println("Server stopped.");
    }

    public void shutdown() {
        _orb.shutdown(false);
    }
}

Park IDL file:

// Definiert die Methoden, die der Server breitstellt in der systemunabhängigen Sprache
// IDL, der Server muss diese natürlich implementieren.
// Die nötigen generierten Dateien werden mit "idlj -fall parkhaus.idl" auf der
// Konsole generiert.
// Der Orb wird mit "start orbd -ORBInitialPort 1050" gestartet.
module parkhaus {

    interface Parkhaus {

             string einfahren(in long anzahlAutos) ;

             string ausfahren(in long anzahlAutos);

             string frei();
    };
};

ParkhausServant:

public class ParkhausServant extends ParkhausPOA {

 private Parkhaus _parkhaus = null;

    public ParkhausServant()
    {
        _parkhaus = new Parkhaus(5);
    }
    public String einfahren(int anzahlAutos)
    {
            try
            {
                _parkhaus.einfahrenAuto(anzahlAutos);
                return anzahlAutos + " Autos eingefahren";
            }
            catch(IllegalStateException ex)
            {
                return "Fehler beim Einfahren";
            }
        }

    public String ausfahren(int anzahlAutos)
    {
            try
            {
                _parkhaus.rausfahrenAuto(anzahlAutos);
                return anzahlAutos + " Autos rausgefahren";
            }
            catch(IllegalStateException ex)
            {
                return "Fehler beim Rausfahren";
            }
    }
    public String frei()
    {
            try
            {
                int freiParkplaetze = _parkhaus.freieParkplaetze();
                return freiParkplaetze + " freie Parkplaetze";
            }
            catch(IllegalStateException ex)
            {
                return "Fehler freie Parkplaetze";
            }
    }
}