Search code examples
javaandroidraspberry-pirmi

Java RMI - Passing Objects


I'm running a Java program on a Raspberry Pi and an Android-App on a smartphone. The app should be able to invoke methods running on the Raspi. Since Android does not include the standard Java RMI package, I used LipeRMI: http://lipermi.sourceforge.net/

So far so good, LipeRMI does work well for passing primitive data types such as boolean or int.

What I want to do now is passing an ArrayList<String> from the Server to the app but every time I run the method, I receive following error:

LipeRMIException:Class java.util.ArrayList is not assignable from control.ServerInt
at lipermi.handler.CallHandler.exportObject(CallHandler.java:54)
at lipermi.handler.CallHandler.exportObject(CallHandler.java:48)
at control.RMIServer.createServer(RMIServer.java:26)
at control.Main.main(Main.java:17)

I do not understand what I'm doing wrong here.

RMIServer.java

public class RMIServer implements ServerInt{
    private static final long serialVersionUID = 1L;
    private ArrayList<String> list;

    public RMIServer()  {
    list = new ArrayList<String>();
        list.add("50:25:5:-1");
        list.add("99:42:6:4");

        createServer();
    }

    public void createServer() {
        try {
            CallHandler callHandler = new CallHandler();
            callHandler.registerGlobal(ServerInt.class, this);
            callHandler.exportObject(ServerInt.class, list);
            Server server = new Server();
            server.bind(7777, callHandler);

            server.addServerListener(new IServerListener() {

                @Override
                public void clientDisconnected(Socket socket) {}

                @Override
                public void clientConnected(Socket socket) {}
            });

        } catch (LipeRMIException | IOException e) {
            e.printStackTrace();
        }    
    }


    @Override
    public ArrayList<String> getPWMLines() {       
        return list;
    }   
}

ServerInt.java

import java.util.ArrayList;

public interface ServerInt{          
    public ArrayList<String> getPWMLines();   
}

Solution

  • You obviously misuse the method CallHandler#exportObject(Class, Object) the first parameter must be a class representing an interface and the second parameter must be an instance of a class that implements this interface. Here you provide an ArrayList as second parameter which is clearly not an instance of ServerInt that is why you get this exception.

    Here is a good example of how to use this method:

    MyRemoteListener listenerImplementation = new MyRemoteListenerImpl();
    callHandler.exportObject(MyRemoteListener.class, listenerImplementation);
    

    In this case the first parameter is the interface MyRemoteListener and the second is an instance of MyRemoteListenerImpl which is an implementation of the interface MyRemoteListener.


    To expose your List you can try callHandler.registerGlobal(List.class, list)