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 14 15 public class Mediator extends JtMediator { 16 17 18 19 public Mediator () { 20 } 21 22 23 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 46 47 48 public Object processMessage (Object message) { 49 50 String msgid = null; 51 JtMessage e = (JtMessage) message; 52 Object content; 53 Object data; 54 JtMessage tmp; 55 JtObject colleague; 56 57 if (e == null) 58 return null; 59 60 msgid = (String ) e.getMsgId (); 61 62 if (msgid == null) 63 return null; 64 65 content = e.getMsgContent(); 66 data = e.getMsgData (); 67 68 70 if (msgid.equals ("JtREMOVE")) { 71 return (this); 72 } 73 74 75 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 89 broadcastMessage (tmp); 90 91 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 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 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 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 e) { 153 e.printStackTrace (); 154 } 155 156 return (c); 157 } 158 159 161 public static void main(String [] 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 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 182 main.setValue (colleague1, "mediator", mediator); 183 main.sendMessage (colleague1, new JtMessage ("JtACTIVATE")); 184 185 186 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 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 207 main.removeObject ("mediator"); 208 main.removeObject ("Jenny"); 209 main.removeObject ("Daniel"); 210 main.removeObject ("Mary"); 211 } 212 213 } 214 215 216 | Popular Tags |