raspisensormodule.BusRMIConnector.java Source code

Java tutorial

Introduction

Here is the source code for raspisensormodule.BusRMIConnector.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package raspisensormodule;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.RemoteServer;
import java.rmi.server.ServerNotActiveException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import nodeRMI.RegisterRMI;
import nodeRMI.UseRMI;

/**
 *
 * @author thor
 */
public class BusRMIConnector extends BusConnecter implements RegisterRMI {

    String funkyTopic = "";

    public BusRMIConnector(Connection busConnection, String exchange, String baseRoutingKey) throws Exception {
        super(busConnection, exchange, baseRoutingKey);
        try {

            //           // Server obj = new Server();
            //            BusRMIConnector stub = (BusRMIConnector) UnicastRemoteObject.exportObject(this, 0);
            //
            //            // Bind the remote object's stub in the registry
            //            Registry registry = LocateRegistry.getRegistry();
            //            registry.bind("RegisterRMI", stub);
            java.rmi.registry.LocateRegistry.createRegistry(1099); // start i server-JVM
            Naming.rebind("rmi://localhost/RegisterRMI", this);
        } catch (RemoteException ex) {
            Logger.getLogger(BusRMIConnector.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("RegisterRMI oppe");
    }

    @Override
    public String register(String Sensor_name, HashMap<String, Integer> sensors) throws RemoteException {

        try {
            System.out.println("Connection from " + RemoteServer.getClientHost());
        } catch (ServerNotActiveException ex) {
            Logger.getLogger(BusRMIConnector.class.getName()).log(Level.SEVERE, null, ex);
        }

        UseRMI sensorCall = null;
        Channel busChannel = null;
        try {
            busChannel = busConnection.createChannel();
            busChannel.exchangeDeclare(exchange, "topic");
        } catch (IOException ex) {
            Logger.getLogger(BusRMIConnector.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            System.out.println(RemoteServer.getClientHost());

            sensorCall = (UseRMI) Naming.lookup("rmi://" + RemoteServer.getClientHost() + "/UseRMI");
        } catch (ServerNotActiveException | NotBoundException | MalformedURLException | RemoteException ex) {
            Logger.getLogger(BusRMIConnector.class.getName()).log(Level.SEVERE, null, ex);
        }

        ArrayList<String> sens = new ArrayList<>();
        Integer oldKey = sensors.entrySet().iterator().next().getValue();

        for (Map.Entry<String, Integer> entry : sensors.entrySet()) {
            if (entry.getValue().equals(oldKey)) {
                sens.add(entry.getKey());
            } else {
                RMI_sensor s = new RMI_sensor(oldKey, sens, sensorCall, busChannel, exchange,
                        baseRoutingKey + '.' + Sensor_name + funkyTopic, this);
                Thread t = new Thread(s);
                addThread(t);
                sens = new ArrayList<>();
                sens.add(entry.getKey());
                oldKey = entry.getValue();
            }

        }
        RMI_sensor s = new RMI_sensor(oldKey, sens, sensorCall, busChannel, exchange,
                baseRoutingKey + '.' + Sensor_name + funkyTopic, this);
        Thread t = new Thread(s);
        addThread(t);

        return "" + sensorCount;
    }

    @Override
    public String register(String Sensor_name, HashMap<String, Integer> sensors, String funky_topic)
            throws RemoteException {
        this.funkyTopic = funky_topic;
        return register(Sensor_name, sensors);
    }

}