19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

}<br />

return false;<br />

/** Check if the board is full */<br />

public boolean isFull() {<br />

for (int i = 0; i < 3; i++)<br />

for (int j = 0; j < 3; j++)<br />

if (board[i][j] == '\u0000')<br />

return false;<br />

}<br />

return true;<br />

}<br />

public static void main(String[] args) {<br />

try {<br />

TicTacToeInterface obj = new TicTacToeImpl();<br />

Registry registry = LocateRegistry.getRegistry();<br />

registry.rebind("TicTacToeImpl", obj);<br />

System.out.println("Server " + obj + " registered");<br />

}<br />

catch (Exception ex) {<br />

ex.printStackTrace();<br />

}<br />

}<br />

2. Create CallBackImpl.java (Listing 46.10) <strong>to</strong> implement the<br />

CallBack interface.<br />

<br />

<br />

<br />

Listing 46.10 CallBackImpl.java<br />

import java.rmi.*;<br />

import java.rmi.server.*;<br />

public class CallBackImpl extends UnicastRemoteObject<br />

implements CallBack {<br />

// The client will be called by the server through callback<br />

private TicTacToeClientRMI thisClient;<br />

/** Construc<strong>to</strong>r */<br />

public CallBackImpl(Object client) throws RemoteException {<br />

thisClient = (TicTacToeClientRMI)client;<br />

}<br />

/** The server notifies the client for taking a turn */<br />

public void takeTurn(boolean turn) throws RemoteException {<br />

thisClient.setMyTurn(turn);<br />

}<br />

/** The server sends a message <strong>to</strong> be displayed by the client */<br />

public void notify(String message) throws RemoteException {<br />

thisClient.setMessage(message);<br />

}<br />

/** The server notifies a client of the other player's move */<br />

21

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!