Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 1st, 2005, 05:41 PM
Moleme Moleme is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 2 Moleme User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 6 sec
Reputation Power: 0
RMI/Observer problem

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

Reply With Quote
  #2  
Old December 2nd, 2005, 02:36 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Novice (500 - 999 posts)
 
Join Date: Sep 2005
Posts: 632 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 2 Days 4 h 45 m 4 sec
Reputation Power: 3
http://www.ryerson.ca/~dgrimsha/cou...ICallbacks.html

I think you'll find your answer there. The idea is to also let a client object implement the Remote interface and export it, but you don't need a registry since you'll pass the reference to the server object yourself.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > RMI/Observer problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway