/** * * Client * * Describes one subscribed client, specified by a unicast destination * address and a port on that machine. Ie, simply a place to send things * to. * * This also maintains a timestamp field, which represents the last * time we heard from this client, via a keep-alive packet receipt. * * Author: Don McGregor * Date: 2/19/99 * */ package mil.navy.nps.bridge; import java.util.*; import java.net.*; public class Client { InetAddress clientAddress; // where to send unicast stuff int port; // what port there long timestamp; // last time we got a keepalive, or were created /** Constructor; gets the whole subscribe command from the packet, which we parse out into the inet address and port of where we should send this. */ public Client(String pSubscribeCommand) { StringTokenizer tokenizer = new StringTokenizer(pSubscribeCommand); String token; String clientString; timestamp = System.currentTimeMillis(); // Command should be in the format // connect clientHostAddress clientHostPort mcastGroup mcastPort tokenizer.nextToken(); // throw away connect string (should check for command validity) // Client descripton is 'x.x.x.x port' clientString = tokenizer.nextToken(); // dotted decimal token = tokenizer.nextToken(); // port try { clientAddress = InetAddress.getByName(clientString); port = Integer.parseInt(token); } catch(UnknownHostException unkhe) { System.out.println("cannot find address " + clientString); } } /** * Constructor; takes as arguments the place to forward packets * to, in the form of a unicast address, and a port on that machine * to send packets to. Client is automatically timestamped. */ public Client(InetAddress pClientAddress, int pPort) { clientAddress = pClientAddress; port = pPort; timestamp = System.currentTimeMillis(); } /** * Hashcode, used for fast access in various data structures. * This is just the inet address hashcode; good enough. */ public int hashCode() { return clientAddress.hashCode(); } /** * One client is equal to anothr if it has the same address and * the same port. */ public boolean equals(Object pObject) { Client aClient; if(!(pObject instanceof Client)) return false; aClient = (Client)pObject; if((clientAddress.equals(aClient.clientAddress)) && (port == aClient.port)) return true; return false; } /** * set the timestamp to the current time. */ public synchronized void setTimestamp() { timestamp = System.currentTimeMillis(); } /** * Returns time this client was last heard from */ public synchronized long getTimestamp() { return timestamp; } }