Ok i am having a tough time setting a client as an observer to a package i have being run on another server my client code is as follows
Code:
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.*;
public class Client
{
private static chessModel.Game game = null;
private static Controller stub = null;
private static Scanner input;
private static View view;
private Client() {}
public static void main(String[] args)
{
String name = args[0];
try
{
Registry reg = LocateRegistry.getRegistry("localhost");
stub = (Controller) reg.lookup("Chess Game");
}
catch (Exception e)
{
System.err.println("Client exception thrown: " + e.toString());
e.printStackTrace();
}
try
{
/*if(args[2].equals("s"))
{
game = stub.startGame();
stub.addPlayer(args[3]);
view = new View(game);
//chess
}
else
{
stub.addPlayer(args[3]);
game = stub.getGame();
view = new View(game);
}*/
/*if(stub.getGame().equals(null))
{
}
else
{
game = stub.getGame();
}*/
//game = stub.getGame();
if(game == null)
{
game = stub.startGame();
stub.addPlayer(name);
view = new View(game);
stub.registerObserver((chessModel.BoardObserver)vi ew);
//stub.registerObserver((chessModel.BoardObserver)vi ew);
}
else
{
//game = stub.getGame();
stub.addPlayer("Andrew");
view = new View(game);
//stub.registerObserver((chessModel.BoardObserver)vi ew);
}
//stub.startGame();
//chessModel.Board test = stub.getBoardOne();
//test.printBoard(test);
}
catch(Exception e)
{
System.out.println(e.getMessage());
//e.printStackTrace();
}
input = new Scanner(System.in);
String text = "";
while(!text.equals("exit"))
{
text = input.nextLine();
}
}
}
That creates a simple view defined below
Code:
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.*;
public class View implements chessModel.BoardObserver, java.io.Serializable
{
public View(chessModel.Game game)
{
// game.registerObserver((chessModel.BoardObserver)th is);
}
public void print()
{
//game.getGame
System.out.println("YES");
}
}
my stub and wrapper class for the model are as follows
Code:
import java.rmi.RemoteException;
public class ControllerImpl implements Controller
{
chessModel.Board b1, b2;
chessModel.Player p1, p2, p3, p4;
private int connected = 0;
chessModel.Game game = null;
public void addPlayer(String name) throws RemoteException
{
if(connected == 0)
{
p1 = game.addPlayer(name,++connected,chessModel.Piece.W HITE,1);
}
else if(connected == 1)
{
p2 = game.addPlayer(name,++connected,chessModel.Piece.B LACK,1);
}
else if(connected == 3)
{
p3 = game.addPlayer(name,++connected,chessModel.Piece.W HITE,2);
}
else if(connected == 4)
{
p4 = game.addPlayer(name,++connected,chessModel.Piece.B LACK,2);
game.startGame(true);
}
}
public chessModel.Game getGame() throws RemoteException
{
return this.game;
}
public chessModel.Board getBoardOne() throws RemoteException
{
//System.out.println("STUB");
return b1;
//return b1.printBoard(b1);
}
public chessModel.Game startGame() throws RemoteException
{
if(game == null)
{
game = new chessModel.Game();
}
return this.game;
}
public String getBoardTwo() throws RemoteException
{
System.out.println("STUB");
return "Stub";
}
public void registerObserver(chessModel.BoardObserver o) throws RemoteException
{
game.registerObserver(o);
}
}
and model wrapper
Code:
package chessModel;
import java.util.*;
public class Game implements java.io.Serializable
{
private boolean colour = true;
private boolean start = false;
private ArrayList<Player> players;
private ArrayList<Board> boards;
private ArrayList<chessModel.BoardObserver> observers = new ArrayList<chessModel.BoardObserver>();
public Game()
{
players = new ArrayList<Player>();
boards = new ArrayList<Board>();
boards.add(new Board());
boards.add(new Board());
start = false;
}
public Player addPlayer(String name, int playerID, int colour, int BID)
{
Player player = new Player(name,playerID,colour,BID);
players.add(player);
notifyObservers();
return player;
}
public void startGame(boolean start)
{
this.start = start;
}
public void registerObserver(BoardObserver o)
{
System.out.println("added");
observers.add(o);
}
private void notifyObservers()
{
int i = 0;
System.out.println("ok" + observers.size());
while(i < observers.size())
{
observers.get(i).print();
i++;
}
}
}
as you can see from my comments that i have tried registering to be a observer in numerous spots. When i use the Controller class to register all the printing is done on the server terminal as opposed to teh client views. And if i try to registerObserver() in the view it prints at the client, but as i connecte clients it is shown that i have no registeredobservers
Long story short how do i invoke a method on the client from the server side.
Thanks