FileDocCategorySizeDatePackage
TickerServerImpl.javaAPI DocExample1634Fri Apr 20 21:27:20 BST 2001com.darwinsys.callback

TickerServerImpl

public class TickerServerImpl extends UnicastRemoteObject implements TickerServer, Runnable
This is the main class of the server

Fields Summary
ArrayList
list
boolean
done
Random
rand
Constructors Summary
public TickerServerImpl()
Construct the object that implements the remote server. Called from main, after it has the SecurityManager in place.


	                  	 
	    
		super();	// sets up networking
	
Methods Summary
public voidconnect(Client da)
The remote method that "does all the work". This won't get called until the client starts up.

		System.out.println("Adding client " + da);
		list.add(da);
	
public voidrun()


	   
		while (!done) {
			try {
				Thread.sleep(10 * 1000);
				System.out.println("Tick");
			} catch (InterruptedException unexpected) {
				System.out.println("WAHHH!");
				done = true;
			}
			Iterator it = list.iterator();
			while (it.hasNext()){
				String mesg = ("Your stock price went " +
					(rand.nextFloat() > 0.5 ? "up" : "down") + "!");
				// Send the alert to the given user.
				// If this fails, remove them from the list
				try {
					((Client)it.next()).alert(mesg);
				} catch (RemoteException re) {
                    System.out.println(
						"Exception alerting client, removing it.");
					System.out.println(re);
					it.remove();
				}
			}
		}
	
public voidstart()
Start background thread to track stocks :-) and alert users.

		new Thread(this).start();