KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > examples > patterns > Colleague


1
2
3 package Jt.examples.patterns;
4
5 import java.util.*;
6 import java.lang.reflect.*;
7 import java.beans.*;
8 import java.io.*;
9 import Jt.*;
10
11 /**
12  * Chat room member (runs in a separate thread)
13  */

14
15 public class Colleague extends JtThread {
16
17 Object JavaDoc mediator;
18 String JavaDoc greetingMessage = "Hi there ";
19
20
21 /**
22   * Specifies the greeting message.
23   *
24   * @param greetingMessage greetingMessage
25   */

26
27   public void setGreetingMessage (String JavaDoc greetingMessage) {
28      this.greetingMessage = greetingMessage;
29
30   }
31
32 /**
33   * Returns the greeting message.
34   */

35
36   public String JavaDoc getGreetingMessage () {
37      return (greetingMessage);
38   }
39
40
41 /**
42   * Specifies the mediator object.
43   *
44   * @param mediator mediator
45   */

46
47   public void setMediator (Object JavaDoc mediator) {
48      this.mediator = mediator;
49
50   }
51
52 /**
53   * Returns the mediator.
54   */

55
56   public Object JavaDoc getMediator () {
57      return (mediator);
58   }
59
60
61   public Colleague () {
62   }
63
64
65   // sleep for a period of time
66

67   private void sleep (long period) {
68
69     try {
70         Thread.sleep (period);
71     } catch (Exception JavaDoc e) {
72         handleException (e);
73     }
74
75   }
76
77   /**
78     * Process object messages.
79     * <ul>
80     * </ul>
81     * @param message Jt Message
82     */

83
84
85   public Object JavaDoc processMessage (Object JavaDoc message) {
86
87    String JavaDoc msgid = null;
88    JtMessage e = (JtMessage) message;
89    Object JavaDoc content;
90    Object JavaDoc data;
91    JtMessage tmp;
92
93
94
95      if (e == null)
96     return null;
97
98      msgid = (String JavaDoc) e.getMsgId ();
99
100      if (msgid == null)
101     return null;
102
103      content = e.getMsgContent();
104      data = e.getMsgData ();
105
106      // Remove this object
107
if (msgid.equals ("JtREMOVE")) {
108        return (this);
109      }
110
111
112      if (msgid.equals ("JtACTIVATE")) {
113
114        if (mediator == null) {
115          handleError ("the mediator attribute needs to be set");
116          return (null);
117        }
118
119
120        // Join the chat room
121

122        tmp = new JtMessage ("JOIN");
123        tmp.setMsgFrom (this);
124        sendMessage (mediator, tmp);
125
126        // Send a message
127

128        tmp = new JtMessage ("MESSAGE");
129        tmp.setMsgContent (greetingMessage);
130        tmp.setMsgFrom (this);
131        sendMessage (mediator, tmp);
132
133        // Sleep for a few seconds
134

135        sleep (3000L);
136
137        // Exit the chat room
138

139        tmp = new JtMessage ("EXIT");
140        tmp.setMsgFrom (this);
141        sendMessage (mediator, tmp);
142
143        return (this);
144      }
145
146      return (super.processMessage (message));
147    
148
149   }
150
151
152
153
154 }
155
156
157
Popular Tags