TicTacToepublic class TicTacToe extends JFrame implements Runnable, RegisterListener, BrowseListener, ListSelectionListener
Fields Summary |
---|
public static final String | ServiceType | public String | myName | public HashMap | activeGames | private DefaultListModel | gameList | private JList | players | private ServerSocketChannel | listentingChannel | private int | listentingPort |
Constructors Summary |
---|
public TicTacToe()
// NOTE: Because a TicTacToe is a JFrame, the caller MUST be running
// on the event-dispatching thread before trying to create one.
super("Tic-Tac-Toe");
try
{
// 1. Make the browsing window, and start browsing
activeGames = new HashMap();
gameList = new DefaultListModel();
players = new JList(gameList);
players.addListSelectionListener(this);
getContentPane().add(new JScrollPane(players));
setSize(200, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
DNSSD.browse(ServiceType, this);
// 2. Make listening socket and advertise it
listentingChannel = ServerSocketChannel.open();
listentingChannel.socket().bind(new InetSocketAddress(0));
listentingPort = listentingChannel.socket().getLocalPort();
setTitle(listentingPort + " registering");
DNSSD.register(null, ServiceType, listentingPort, this);
// 3. If we sit here and hog the event-dispatching thread
// the whole UI will freeze up, so instead we create a new
// background thread to receive incoming connection requests.
new Thread(this).start();
}
catch (Exception e) { e.printStackTrace(); }
|
Methods Summary |
---|
public static void | main(java.lang.String[] args)
Runnable runOnSwingThread = new Runnable()
{ public void run() { new TicTacToe(); } };
try { SwingUtilities.invokeAndWait(runOnSwingThread); }
catch (Exception e) { e.printStackTrace(); }
| public void | operationFailed(DNSSDService service, int errorCode)
System.out.println("Bonjour operation failed " + errorCode);
System.exit(-1);
| public void | run()
try
{
while (true)
{
SocketChannel sc = listentingChannel.accept();
if (sc != null) new Thread(new GameReceiver(sc)).start();
}
}
catch (Exception e) { e.printStackTrace(); }
| public void | serviceFound(DNSSDService browser, int flags, int ind, java.lang.String name, java.lang.String type, java.lang.String domain)
if (name.equals(myName)) return; // Don't add ourselves to the list
DiscoveredInstance x = new DiscoveredInstance(ind, name, domain);
try { SwingUtilities.invokeAndWait(new Adder(x)); }
catch (Exception e) { e.printStackTrace(); }
| public void | serviceLost(DNSSDService browser, int flags, int ind, java.lang.String name, java.lang.String regType, java.lang.String domain)
DiscoveredInstance x = new DiscoveredInstance(ind, name, domain);
try { SwingUtilities.invokeAndWait(new Remover(x)); }
catch (Exception e) { e.printStackTrace(); }
| public void | serviceRegistered(DNSSDRegistration sd, int flags, java.lang.String serviceName, java.lang.String regType, java.lang.String domain)
if (!domain.equalsIgnoreCase("local.")) return;
myName = serviceName;
Runnable r = new Runnable()
{ public void run() { setTitle(listentingPort + " " + myName); } };
try { SwingUtilities.invokeAndWait(r); }
catch (Exception e) { e.printStackTrace(); }
| public void | valueChanged(javax.swing.event.ListSelectionEvent event)
int selected = players.getSelectedIndex();
if (selected != -1)
{
DiscoveredInstance x =
(DiscoveredInstance)players.getSelectedValue();
GameBoard game = (GameBoard)activeGames.get(x.toString());
if (game != null) game.toFront();
else x.resolve(new GameBoard(this, x.toString(), null));
}
|
|