?? tokenring.java
字號:
try
{
senderService = DatagramService.getService(myAddress);
}
catch(Exception e)
{
System.out.println("Unable to getServerService.");
System.exit(0);
}
dgCreator = new DatagramCreator(senderService, rg.getAllAddressAddresses());
}
/**
* Notify listeners of a new turn event
* @param notification The event to send to all listeners
*/
private void processReceivedTurn(ReceivedTurnEvent notification)
{
for (Enumeration en = listeners.elements(); en.hasMoreElements();)
{
ReceivedTurnListener listener = (ReceivedTurnListener)en.nextElement();
listener.TurnReceived(notification);
}
}
/**
* Passes a String to the
* {@link com.symbian.devnet.whist.tokenRing.DatagramCreator#createAndSend(String, int, int) createAndSend}
* . The unique
* identification number for the message is added and incremented. This
* method sends the datagram to the next address in the array of addresses.
* @param s the String to be sent.
*/
public void sendMove(String s)
{
dgCreator.createAndSend(s, nextAddress, ++idNo);
}
/**
* Passes a String to the
* {@link com.symbian.devnet.whist.tokenRing.DatagramCreator#createAndSend(String, int, int) createAndSend}
* . The unique identification number for the message is incremented and
* added. This method sends the datagram to the address requested.
* @param s the String to be sent.
* @param next the position in the array of addresses to which the datagram
* is to be sent.
*/
public void sendMove(String s, int next)
{
dgCreator.createAndSend(s, next, ++idNo);
}
/**
* Passes a String to the
* {@link com.symbian.devnet.whist.tokenRing.DatagramCreator#createAndSend(String, int, int) createAndSend}
* . The unique identification number for the message is incremented
* by 8 to ensure that it will not be discarded as an old message.
* This method sends the datagram to all the players in the game.
* @param s the String to be sent.
*/
public void sendMoveToAll(String s)
{
int[] addresses = new int[3];
int count = 0;
for (int i = 0; i < 4; i++)
{
if (playerNo-1 != i)
addresses[count++] = i;
}
dgCreator.createAndSend(s, addresses, (idNo+8));
}
/**
* Register interest in received turn events
* @param listener The event listener
*/
public void addReceivedTurnListener(ReceivedTurnListener listener)
{
listeners.addElement(listener);
}
/**
* Deregister interest in received turn events
* @param listener The event listener
*/
public void removeReceivedTurnListener(ReceivedTurnListener listener)
{
listeners.removeElement(listener);
}
/**
* Get the current player number
* @return the current player number
*/
public int getPlayerNo()
{
return playerNo;
}
/**
* Utility to get IP address of this node
* @return String containing IP address of this node
*/
public String getNodeIPNo()
{
Address nodeAddress = null;
if (protocol == UDP)
{
try
{
nodeAddress = DatagramService.parseAddress(DUMMY_ADDRESS_UDP);
}
catch(Exception ex)
{
System.out.println("Server address could not be parsed.");
System.exit(0);
}
}
else // protocol == WDPSMS
{
try
{
nodeAddress = DatagramService.parseAddress(DUMMY_ADDRESS_WDPSMS);
}
catch(Exception ex)
{
System.out.println("Server address could not be parsed.");
System.exit(0);
}
}
try
{
dummy = DatagramService.getService(nodeAddress);
}
catch (Exception e)
{
System.out.println("Could not get service: " + e.getMessage());
System.exit(0);
}
try
{
nodeAddress = dummy.getAddress();
}
catch (IOException ioe)
{
System.out.println("Address could not be parsed: " + ioe.getMessage());
System.exit(0);
}
int noChars;
if (protocol == UDP)
noChars = 6;
else
noChars = 9;
char[] add = nodeAddress.toString().toCharArray();
char[] tele = new char[(add.length - noChars - 5)];
int count = 0;
int i = noChars;
while (add[i] !=':') //(i < add.length)
tele[(count++)] = add[(i++)];
String t = new String(tele);
try
{
dummy.close();
}
catch (IOException ioe)
{
System.out.println("Could not close service: " + ioe.getMessage());
System.exit(0);
}
return t;
}
/**
* Returns the name of the requested player.
* @param no the number in the array of the player whose name has
* been requested
* @return the player name
*/
public String getPlayerName(int no)
{
return rg.getPlayerName(no);
}
/**
* Returns the port number the <code>Receiver</code> is listening on.
* @return the port number the <code>Receiver</code> is listening on
*/
public String getPort()
{
return port;
}
/**
* Resets the player number and Id number to allow a new game to be started.
*/
public void resetNos()
{
playerNo = 0;
idNo = 0;
}
/**
* Method to return the current ID number.
* @return the current Id number
*/
public int getIdNo()
{
return idNo;
}
/**
* Takes the buffer from the received datagram and stores it in a locally
* accessible array, using the {@link com.symbian.devnet.whist.tokenRing.DatagramEvent#getData() DatagramEvent getData}
* method. This data is kept
* in a synchronized array and a waiting thread is then notified.
* @param de a DatagramEvent
* @see com.symbian.devnet.whist.tokenRing.DatagramEvent DatagramEvent
*/
public void DatagramReceived(DatagramEvent de)
{
dataReceived = de.getData();
synchronized(this)
{
notify();
}
}
/**
* Creates a forever loop around a synchronized block (this), which waits until
* woken up by another thread. Once the thread has been woken up, a copy of
* the data from the receive thread is made and the method
* {@link com.symbian.devnet.whist.tokenRing.TokenRing#processMessage() processMessage}
* is called.
*/
public void eventLoop()
{
while (true)
{
try
{
synchronized(this)
{
wait();
}
}
catch(InterruptedException ie)
{
System.out.println("Could not wait: " + ie.getMessage());
System.exit(0);
}
data = dataReceived;
processMessage();
}
}
/**
* Closes down the UDP <code>Receiver</code> and starts an SMS
* <code>Receiver</code>
*/
public void initiateSMS()
{
receive.close();
protocol = WDPSMS;
boolean b = setupEngine("1010");
}
/**
* Creates the initialising datagram containing the details of the
* particitients (i.e. the names and addresses) and also initialises
* the <code>Ring</code>
* @param nNames the names of the players in the game
* @param aAddresses the addresses of the players in the game
*/
public void start(String[] nNames, String[] aAddresses)
{
names = nNames;
adds = aAddresses;
rg = new Ring(nNames, aAddresses);
createSender();
String temp = "";
for (int i = 0; i < names.length; i++)
temp = temp + adds[i] + "?";
for (int i = 0; i < names.length; i++)
temp = temp + names[i] + "?";
dgCreator.createAndSend(temp, 1, ++idNo);
idNo = 1;
playerNo = 1;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -