VisClient/org/hfbk/vis/mcp/Pinger.java

Go to the documentation of this file.
00001 
00004 package org.hfbk.vis.mcp;
00005 
00006 import java.net.DatagramPacket;
00007 import java.net.DatagramSocket;
00008 import java.net.InetAddress;
00009 import java.net.SocketAddress;
00010 import java.util.*;
00011 
00024 abstract class Pinger extends Thread {
00025 
00026         DatagramSocket socket;
00027 
00028         DatagramPacket paket = new DatagramPacket(new byte[32], 32);
00029 
00030         InetAddress broadcastIP;
00031         
00033         static int pingport=7779;
00034         
00035         
00036         public Pinger() {
00037                 super("Pinger, port "+pingport);
00038                 try {
00039                         broadcastIP=InetAddress.getByName("255.255.255.255");                   
00040                         socket=new DatagramSocket(pingport);
00041                         socket.setSoTimeout(2000);
00042                         socket.setBroadcast(true);                              
00043                 } catch (Exception e1) {
00044                         throw new RuntimeException (e1);
00045                 }
00046                 start();
00047         }
00048         
00049         public void run() {
00050                 
00051                 //list keep found clients.
00052                 LinkedList<SocketAddress> clientsFound=new LinkedList<SocketAddress>();
00053                 
00054                 while(true) try {
00055                         clientsFound.clear();
00056                         
00057                         byte[] data="PING\n".getBytes();
00058                         socket.send(new DatagramPacket(data,data.length, broadcastIP, VisMCP.clientport));
00059                         
00060                         while(true){ //not forever: either we got another answer or we leave via timeout
00061                                 socket.receive(paket); 
00062                                 String result = new String(paket.getData(), 0, paket.getLength());
00063                                 if (result.equals("PONG\n"))
00064                                         clientsFound.add(paket.getSocketAddress());
00065                         }                                                                       
00066                 } catch (Exception e) { //timeout, handle all answering remote sockets.
00067                         pinged(clientsFound);                   
00068                 }       
00069         }
00070         
00072         abstract void pinged(List<SocketAddress> addresses);
00073 }

Generated on Tue Apr 7 17:57:20 2009 for visclient by  doxygen 1.5.1