FileDocCategorySizeDatePackage
CoordinationDemo.javaAPI DocApache Tomcat 6.0.1414610Fri Jul 20 04:20:36 BST 2007org.apache.catalina.tribes.demos

CoordinationDemo

public class CoordinationDemo extends Object

Fields Summary
static int
CHANNEL_COUNT
static int
SCREEN_WIDTH
static long
SLEEP_TIME
static int
CLEAR_SCREEN
static boolean
MULTI_THREAD
static boolean[]
VIEW_EVENTS
StringBuffer
statusLine
Status[]
status
BufferedReader
reader
Constructors Summary
public CoordinationDemo()
Construct and show the application.

              
      
    
Methods Summary
public voidclearScreen()

        StringBuffer buf = new StringBuffer(700);
        for (int i=0; i<CLEAR_SCREEN; i++ ) buf.append("\n");
        System.out.println(buf);
    
private voidcmdStart(java.lang.String[] args)

        if ( args.length == 1 ) {
            setSystemStatus("System starting up...");
            Thread[] t = new Thread[CHANNEL_COUNT];
            for (int i = 0; i < status.length; i++) {
                final int j = i;
                t[j] = new Thread() {
                    public void run() {
                        status[j].start();
                    }
                };
            }
            for (int i = 0; i < status.length; i++) if (MULTI_THREAD ) t[i].start(); else t[i].run();
            setSystemStatus("System started.");
        } else { 
            int index = -1;
            try { index = Integer.parseInt(args[1])-1;}catch ( Exception x ) {setSystemStatus("Invalid index:"+args[1]);}
            if ( index >= 0 ) {
                setSystemStatus("Starting member:"+(index+1));
                status[index].start();
                setSystemStatus("Member started:"+(index+1));
            }
        }
    
private voidcmdStop(java.lang.String[] args)

        if ( args.length == 1 ) {
            setSystemStatus("System shutting down...");
            Thread[] t = new Thread[CHANNEL_COUNT];
            for (int i = 0; i < status.length; i++) {
                final int j = i;
                t[j] = new Thread() {
                    public void run() {
                        status[j].stop();
                    }
                };
            }
            for (int i = 0; i < status.length; i++) if (MULTI_THREAD ) t[i].start(); else t[i].run();
            setSystemStatus("System stopped.");
        } else { 
            int index = -1;
            try { index = Integer.parseInt(args[1])-1;}catch ( Exception x ) {setSystemStatus("Invalid index:"+args[1]);}
            if ( index >= 0 ) {
                setSystemStatus("Stopping member:"+(index+1));
                status[index].stop();
                setSystemStatus("Member stopped:"+(index+1));
            }
        }
    
public static java.lang.Stringfill(java.lang.String value, int length, java.lang.String ch, boolean left)

        StringBuffer buf = new StringBuffer();
        if ( !left ) buf.append(value.trim());
        for (int i=value.trim().length(); i<length; i++ ) buf.append(ch);
        if ( left ) buf.append(value.trim());
        return buf.toString();
    
public java.lang.StringgetHeader()

        //member - 30
        //running- 10
        //coord - 30
        //view-id - 24
        //view count - 8

        StringBuffer buf = new StringBuffer();
        buf.append(leftfill("Member",30," "));
        buf.append(leftfill("Running",10," "));
        buf.append(leftfill("Coord",30," "));
        buf.append(leftfill("View-id(short)",24," "));
        buf.append(leftfill("Count",8," "));
        buf.append("\n");
        
        buf.append(rightfill("==="+new java.sql.Timestamp(System.currentTimeMillis()).toString(),SCREEN_WIDTH,"="));
        buf.append("\n");
        return buf.toString();
    
public voidinit()

        reader = new BufferedReader(new InputStreamReader(System.in));
        status = new Status[CHANNEL_COUNT];
    
public static java.lang.Stringleftfill(java.lang.String value, int length, java.lang.String ch)

        return fill(value,length,ch,true);
    
public static voidmain(java.lang.String[] args)

        CoordinationDemo demo = new CoordinationDemo();
        run(args,demo);
    
public voidprintMenuOptions()

        System.out.println("Commands:");
        System.out.println("\tstart [member id]");
        System.out.println("\tstop  [member id]");
        System.out.println("\tprint (refresh)");
        System.out.println("\tquit");
        System.out.print("Enter command:");
    
public synchronized voidprintScreen()

        clearScreen();
        System.out.println(" ###."+getHeader());
        for ( int i=0; i<status.length; i++ ) {
            System.out.print(leftfill(String.valueOf(i+1)+".",5," "));
            if ( status[i] != null ) System.out.print(status[i].getStatusLine());
        }
        System.out.println("\n\n");
        System.out.println("Overall status:"+statusLine);
        printMenuOptions();
        
    
public static java.lang.Stringrightfill(java.lang.String value, int length, java.lang.String ch)

        return fill(value,length,ch,false);
    
public static voidrun(java.lang.String[] args, org.apache.catalina.tribes.demos.CoordinationDemo demo)

        usage();
        java.util.Arrays.fill(VIEW_EVENTS,true);

        for (int i=0; i<args.length; i++ ) {
            if ( "-c".equals(args[i]) )
                CHANNEL_COUNT = Integer.parseInt(args[++i]);
            else if ( "-t".equals(args[i]) )
                MULTI_THREAD = Boolean.parseBoolean(args[++i]);
            else if ( "-s".equals(args[i]) )
                SLEEP_TIME = Long.parseLong(args[++i]);
            else if ( "-sc".equals(args[i]) )
                CLEAR_SCREEN = Integer.parseInt(args[++i]);
            else if ( "-p".equals(args[i]) )
                setEvents(args[++i]);
            else if ( "-h".equals(args[i]) ) System.exit(0);
        }
        demo.init();
        demo.waitForInput();
    
public static voidsetEvents(java.lang.String events)

        java.util.Arrays.fill(VIEW_EVENTS,false);
        StringTokenizer t = new StringTokenizer(events,",");
        while (t.hasMoreTokens() ) {
            int idx = Integer.parseInt(t.nextToken());
            VIEW_EVENTS[idx] = true;
        }
    
public voidsetSystemStatus(java.lang.String status)

        statusLine.delete(0,statusLine.length());
        statusLine.append(status);
    
public java.lang.String[]tokenize(java.lang.String line)

        StringTokenizer tz = new StringTokenizer(line," ");
        String[] result = new String[tz.countTokens()];
        for (int i=0; i<result.length; i++ ) result[i] = tz.nextToken();
        return result;
    
private static voidusage()

        System.out.println("Usage:");
        System.out.println("\tjava org.apache.catalina.tribes.demos.CoordinationDemo -c channel-count(int) -t multi-thread(true|false) -s sleep-time(ms) -sc clear-screen(int) -p view_events_csv(1,2,5,7)");
        System.out.println("Example:");
        System.out.println("\tjava o.a.c.t.d.CoordinationDemo -> starts demo single threaded start/stop with 5 channels");
        System.out.println("\tjava o.a.c.t.d.CoordinationDemo -c 10 -> starts demo single threaded start/stop with 10 channels");
        System.out.println("\tjava o.a.c.t.d.CoordinationDemo -c 7 -t true -s 1000 -sc 50-> starts demo multi threaded start/stop with 7 channels and 1 second sleep time between events and 50 lines to clear screen");
        System.out.println("\tjava o.a.c.t.d.CoordinationDemo -t true -p 12 -> starts demo multi threaded start/stop with 5 channels and only prints the EVT_CONF_RX event");
        System.out.println();
    
public voidwaitForInput()

        for ( int i=0; i<status.length; i++ ) status[i] = new Status(this);
        printScreen();
        String l = reader.readLine();
        String[] args = tokenize(l);
        while ( args.length >= 1 && (!"quit".equalsIgnoreCase(args[0]))) {
            if ("start".equalsIgnoreCase(args[0])) {
                cmdStart(args);
            } else if ("stop".equalsIgnoreCase(args[0])) {
                cmdStop(args);

            }
            printScreen();
            l = reader.readLine();
            args = tokenize(l);
        }
        for ( int i=0; i<status.length; i++ ) status[i].stop();