KickJava   Java API By Example, From Geeks To Geeks.

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


1
2 package Jt.examples.patterns;
3
4 import java.util.*;
5 import java.lang.reflect.*;
6 import java.beans.*;
7 import java.io.*;
8 import Jt.*;
9
10 /**
11  * Chat room implementation based on the Mediator pattern. Colleagues run in separate
12  * threads.
13  */

14
15 public class Mediator extends JtMediator {
16
17
18
19   public Mediator () {
20   }
21
22
23   // Broadcast a message to all the members
24

25   private void broadcastMessage (JtMessage msg) {
26     JtMessage tmp;
27
28     if (msg == null)
29      return;
30     
31     tmp = new JtMessage ("JtBROADCAST");
32     tmp.setMsgContent (msg);
33
34     processMessage (tmp);
35   }
36
37
38
39
40   /**
41     * Process object messages.
42     * <ul>
43     * </ul>
44     * @param message Jt Message
45     */

46
47
48   public Object JavaDoc processMessage (Object JavaDoc message) {
49
50    String JavaDoc msgid = null;
51    JtMessage e = (JtMessage) message;
52    Object JavaDoc content;
53    Object JavaDoc data;
54    JtMessage tmp;
55    JtObject colleague;
56
57      if (e == null)
58     return null;
59
60      msgid = (String JavaDoc) e.getMsgId ();
61
62      if (msgid == null)
63     return null;
64
65      content = e.getMsgContent();
66      data = e.getMsgData ();
67
68      // Remove this object
69

70      if (msgid.equals ("JtREMOVE")) {
71        return (this);
72      }
73
74
75      // Join the chat room
76

77      if (msgid.equals ("JOIN")) {
78       
79
80
81        colleague = (JtObject) e.getMsgFrom ();
82        
83        System.out.println (colleague.getObjName() + " has joined");
84        tmp = new JtMessage ("MESSAGE");
85        tmp.setMsgContent (colleague.getObjName() + " has joined");
86        
87        // Alert everyone about the new member
88

89        broadcastMessage (tmp);
90
91        // Add the new member to the chat room
92

93        tmp = new JtMessage ("JtADD_CHILD");
94        tmp.setMsgContent (e.getMsgFrom ());
95        tmp.setMsgData (colleague.getObjName());
96        processMessage (tmp);
97             
98        return (this);
99
100      }
101
102      // Send a message to the chat room (everyone)
103

104      if (msgid.equals ("MESSAGE")) {
105
106        colleague = (JtObject) e.getMsgFrom ();
107
108        broadcastMessage (e);
109       
110        System.out.println (colleague.getObjName() + ":" + content);
111        return (this);
112
113      }
114
115      // Exit the chat room
116

117      if (msgid.equals ("EXIT")) {
118
119        colleague = (JtObject) e.getMsgFrom ();
120
121        tmp = new JtMessage ("MESSAGE");
122        tmp.setMsgContent (colleague.getObjName() + " is exiting ...");
123
124        broadcastMessage (tmp);
125       
126        System.out.println (colleague.getObjName() + " is exiting ...");
127
128        tmp = new JtMessage ("JtREMOVE_CHILD");
129        tmp.setMsgData (e.getMsgFrom ());
130
131        return (this);
132
133      }
134
135      // Let the superclass handle all the other messages
136

137      return (super.processMessage (message));
138
139
140   }
141
142
143   static private char waitForInputKey () {
144     char c = ' ';
145
146       try {
147
148       c = (char) System.in.read ();
149       while (System.in.available () > 0)
150         System.in.read ();
151
152       } catch (Exception JavaDoc e) {
153         e.printStackTrace ();
154       }
155
156       return (c);
157   }
158
159   // Test program
160

161   public static void main(String JavaDoc[] args) {
162
163     JtObject main = new JtFactory ();
164
165     JtMediator mediator;
166     Colleague colleague1, colleague2, colleague3;
167
168     System.out.println ("Press any key to start/stop the chat room demo ...");
169     waitForInputKey ();
170
171
172     // Create an instance of the chat room
173

174     mediator = (JtMediator) main.createObject ("Jt.examples.patterns.Mediator", "mediator");
175
176      colleague1 = (Colleague) main.createObject ("Jt.examples.patterns.Colleague", "Jenny");
177     main.setValue (colleague1, "greetingMessage", "Hi folks! How are you all doing ?");
178
179
180     // Activate the first member
181

182     main.setValue (colleague1, "mediator", mediator);
183     main.sendMessage (colleague1, new JtMessage ("JtACTIVATE"));
184
185
186     // Activate the second member
187

188     colleague2 = (Colleague) main.createObject ("Jt.examples.patterns.Colleague", "Daniel");
189
190     main.setValue (colleague2, "mediator", mediator);
191     main.sendMessage (colleague2, new JtMessage ("JtACTIVATE"));
192
193
194     // Activate the third member
195

196     colleague3 = (Colleague) main.createObject ("Jt.examples.patterns.Colleague", "Mary");
197
198     main.setValue (colleague3, "mediator", mediator);
199     main.sendMessage (colleague3, new JtMessage ("JtACTIVATE"));
200
201
202
203     waitForInputKey ();
204
205     // Remove the objects
206

207     main.removeObject ("mediator");
208     main.removeObject ("Jenny");
209     main.removeObject ("Daniel");
210     main.removeObject ("Mary");
211   }
212
213 }
214
215
216
Popular Tags