FileDocCategorySizeDatePackage
DistThreadGroup.javaAPI DocExample4283Sun Oct 31 16:02:58 GMT 1999dcj.utils.Thread

DistThreadGroup

public class DistThreadGroup extends Thread
Source code from "Java Distributed Computing", by Jim Farley. Class: DistThreadGroup Example: 4-4 Description: A container for a set of threads distributed across the network.

Fields Summary
protected ThreadGroup
localGroup
protected Hashtable
remoteGroups
protected ServerSocket
incoming
protected int
localPort
static final int
hostIdx
static final int
portIdx
Constructors Summary
public DistThreadGroup(ThreadGroup g, int port)


  // Public constructors

       
    localGroup = g;
    localPort = port;
  
public DistThreadGroup(int port)

    localGroup = new ThreadGroup("local:" + port);
    localPort = port;
  
public DistThreadGroup(String rHost, int rPort, String gname, int port)

    localGroup = new ThreadGroup("local:" + port);
    localPort = port;
    Add(gname, rHost, rPort);
  
Methods Summary
public voidAdd(java.lang.String gname, java.lang.String host, int port)

    RmtThreadGroup rg = new RmtThreadGroup(host, port);
    remoteGroups.put(gname, rg);
  
public java.lang.ThreadGroupGetLocalGroup()

    return localGroup;
  
public voidRemove(java.lang.String gname)

    remoteGroups.remove(gname);
  
protected voidbroadcastCmd(java.lang.String cmd)

    Enumeration e = remoteGroups.elements();
    while (e.hasMoreElements()) {
      RmtThreadGroup rg = (RmtThreadGroup)e.nextElement();
      try {
        Socket s = new Socket(rg.getHost(), rg.getPort());
        DataOutputStream os = new DataOutputStream(s.getOutputStream());
        os.writeUTF(cmd);
	s.close();
	os.close();
      }
      catch (Exception ex) {
        System.out.println("DistThreadGroup: Failed to " + cmd +
                           " group at \"" + rg.getHost() + ":"
                           + rg.getPort());
      }
    }
  
public synchronized voidresume(boolean bcast)

    if (bcast)
      broadcastCmd("resume");

    if (localGroup != null)
      localGroup.resume();
  
public voidrun()

    try {
      incoming = new ServerSocket(localPort);
    }
    catch (IOException ioe) {
      System.out.println("Failed to bind to port " + localPort);
      return;
    }
    while (true) {
      try {
        Socket peer = incoming.accept();
        DataInputStream is = new DataInputStream(peer.getInputStream());
        String input = is.readUTF();
        if (input.compareTo("suspend") == 0)
          suspend();
        else if (input.compareTo("resume") == 0)
          resume();
        //
        // Check for other messages here ("stop", "start", etc.)
        //                            .
        //                            .
        //                            .
        else {
          System.out.println("DistThreadGroup: Received unknown command \""
                             + input + "\"");
        }
      }
      catch (IOException ioe) {
        System.out.println("Network exception reading message");
      }
    }
  
public synchronized voidsuspend(boolean bcast)

    if (bcast)
      broadcastCmd("suspend");

    if (localGroup != null)
      localGroup.suspend();