ChatDemo xref

    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activecluster;
    19
    20  import java.io.BufferedReader;
    21  import java.io.InputStreamReader;
    22  import java.util.HashMap;
    23  import java.util.Map;
    24  import javax.jms.JMSException;
    25  import org.codehaus.activecluster.impl.ActiveMQClusterFactory;
    26
    27  /***
    28   * @version $Revision: 1.5 $
    29   */
    30  public class ChatDemo implements ClusterListener {
    31      private Cluster cluster;
    32      private String name = "unknown";
    33
    34      public static void main(String[] args) {
    35          try {
    36              ChatDemo test = new ChatDemo();
    37              test.run();
    38          }
    39          catch (JMSException e) {
    40              System.out.println("Caught: " + e);
    41              e.printStackTrace();
    42              Exception c = e.getLinkedException();
    43              if (c != null) {
    44                  System.out.println("Cause: " + c);
    45                  c.printStackTrace();
    46              }
    47          }
    48          catch (Exception e) {
    49              System.out.println("Caught: " + e);
    50              e.printStackTrace();
    51          }
    52      }
    53
    54      public void run() throws Exception {
    55          cluster = createCluster();
    56          cluster.addClusterListener(this);
    57          cluster.start();
    58
    59
    60          System.out.println();
    61          System.out.println();
    62          System.out.println("Welcome to the ActiveCluster Chat Demo!");
    63          System.out.println();
    64          System.out.println("Enter text to talk or type");
    65          System.out.println("  /quit      to terminate the application");
    66          System.out.println("  /name foo  to change your name to be 'foo'");
    67
    68          BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    69          boolean running = true;
    70          while (running) {
    71              String line = reader.readLine();
    72              if (line == null || line.trim().equalsIgnoreCase("quit")) {
    73                  break;
    74              }
    75              else {
    76                  running = processCommand(line.trim());
    77              }
    78          }
    79          stop();
    80      }
    81
    82      protected boolean processCommand(String text) throws JMSException {
    83          if (text.equals("/quit")) {
    84              return false;
    85          }
    86          else {
    87              if (text.startsWith("/name")) {
    88                  name = text.substring(5).trim();
    89                  System.out.println("* name now changed to: " + name);
    90              }
    91              else {
    92                  // lets talk
    93                  Map map = new HashMap();
    94                  map.put("text", text);
    95                  map.put("name", name);
    96                  cluster.getLocalNode().setState(map);
    97              }
    98              return true;
    99          }
    100     }
    101
    102
    103     public void onNodeAdd(ClusterEvent event) {
    104         System.out.println("* " + getName(event) + " has joined the room");
    105     }
    106
    107     public void onNodeUpdate(ClusterEvent event) {
    108         System.out.println(getName(event) + "> " + getText(event));
    109     }
    110
    111     public void onNodeRemoved(ClusterEvent event) {
    112         System.out.println("* " + getName(event) + " has left the room");
    113     }
    114
    115     public void onNodeFailed(ClusterEvent event) {
    116         System.out.println("* " + getName(event) + " has failed unexpectedly");
    117     }
    118
    119     public void onCoordinatorChanged(ClusterEvent event){
    120
    121     }
    122
    123     protected Object getName(ClusterEvent event) {
    124         return event.getNode().getState().get("name");
    125     }
    126
    127     protected Object getText(ClusterEvent event) {
    128         return event.getNode().getState().get("text");
    129     }
    130
    131     protected void stop() throws JMSException {
    132         cluster.stop();
    133     }
    134
    135     protected Cluster createCluster() throws JMSException, ClusterException {
    136         ClusterFactory factory = new ActiveMQClusterFactory();
    137         return factory.createCluster("ORG.CODEHAUS.ACTIVEMQ.TEST.CLUSTER");
    138     }
    139
    140 }