C. Miranda
C. Miranda

Reputation: 75

Non remote methods in remote object implementation

I am working on a distributed version (rmi) of the game Battleship for school. In the game, each player has a fleet and a grid, so I wanted to be able to use iterators to write code as such

class Fleet implements Iterable<Ship> {

    private List<Ship> fleet;
    ...
    @Override
    public Iterator<Ship> iterator() { return fleet.iterator(); }
}

class Player {

    private Fleet fleet;
    ...
    public void placeFleet() {
        for (Ship s : fleet) {
            ...
        }
    }
}

But my class Fleet also needs to be a remote class, so I have something like this

interface IFleet extends Remote, Iterable<IShip> {...}

class Fleet extends UnicastRemoteObject implements IFleet {...}

However, this causes an error because the iterator() method is not a remote method, i.e. I get illegal remote method encountered: public abstract java.util.Iterator java.lang.Iterable.iterator()

I've also tried with

interface IFleet extends Remote {...}

class Fleet extends UnicastRemoteObject implements IFleet, Iterable<IShip> {...}

And so I have two questions :

  1. Can a remote object implement Remote and its subinterfaces only?
  2. Can I somehow hide methods in remote objects from the rmi client (make them unavailable and thus allow them NOT to respect the Remote interface needs)?

Thank you!

Ps: I know I can use an iterator over the list in the client in some other way, but this error just made me wonder if it is possible to implement other interfaces along with Remote.

Upvotes: 1

Views: 401

Answers (1)

user207421
user207421

Reputation: 310850

implements Remote

This is already wrong. You have to define your own remote interface, that extends Remote, and implement that. I suspect this is causing the problem. You don't have any actual remote methods in your class at all. And as a matter of fact UnicastRemoteObject already implements Remote.

The answers to your questions are:

  1. The remote object can implement whatever interfaces it likes, whether remote or otherwise.
  2. They are already hidden if they don't appear in your remote interface.

Your code doesn't make sense unless Player is a local object at the server end. Is that the case.

EDIT Your edit is also wrong:

interface IFleet extends Remote, Iterable<IShip> {...}

class Fleet extends UnicastRemoteObject implements IFleet {...}

A remote interface can only contain remote methods, and they must be declared to throw RemoteException, and that is the reason for the error message you got: the method(s) of Iterable don't throw RemoteException.

So, your remote interface must extend Remote, and not extend Iterable. It doesn't make sense anyway if you want to hide that from the client. It is the remote object that must implement Iterable.

I've also tried with

interface IFleet extends Remote {...}

class Fleet extends UnicastRemoteObject implements IFleet, Iterable<IShip> {...}

This is the only thing you should ever have tried. It works, and it satisfies your question (2).

Upvotes: 1

Related Questions