1 2 package org.objectweb.proactive.examples.chat; 3 4 import java.io.IOException ; 5 import java.util.Vector ; 6 7 import org.apache.log4j.Logger; 8 import org.objectweb.proactive.ActiveObjectCreationException; 9 import org.objectweb.proactive.Body; 10 import org.objectweb.proactive.ProActive; 11 import org.objectweb.proactive.RunActive; 12 import org.objectweb.proactive.Service; 13 import org.objectweb.proactive.core.body.migration.Migratable; 14 import org.objectweb.proactive.core.body.migration.MigrationException; 15 import org.objectweb.proactive.core.config.ProActiveConfiguration; 16 import org.objectweb.proactive.core.group.ProActiveGroup; 17 import org.objectweb.proactive.core.mop.ClassNotReifiableException; 18 import org.objectweb.proactive.core.node.Node; 19 import org.objectweb.proactive.core.node.NodeException; 20 import org.objectweb.proactive.ext.migration.MigrationStrategyManagerImpl; 21 22 27 public class Chat implements java.io.Serializable , RunActive { 28 29 static Logger logger = Logger.getLogger(Chat.class.getName()); 30 31 32 private String name = ""; 33 34 private Chat diffusionGroup = null; 35 36 private Vector messageLogger; 37 38 private transient ChatGUI frame; 39 40 private MigrationStrategyManagerImpl migrationStrategy = null; 41 42 private String listOfName = ""; 43 44 47 public Chat () {} 48 49 53 public Chat (String identity) { 54 this.name = identity; 55 this.messageLogger = new Vector (); 56 } 57 58 62 public String getName() { 63 return this.name; 64 } 65 66 70 public Chat getDiffusionGroup() { 71 return this.diffusionGroup; 72 } 73 74 77 public void startAlone () { 78 try { 79 this.diffusionGroup = (Chat) ProActiveGroup.newGroup(Chat.class.getName()); } 80 catch (ClassNotReifiableException e) { e.printStackTrace(); } 81 catch (ClassNotFoundException e) { e.printStackTrace(); } 82 this.addIntoDiffusionGroup((Chat) ProActive.getStubOnThis(), this.name); 83 this.writeMessage(new Message(" *** " + this.name + " has joined the place")); 84 } 85 86 98 public void connect (String hostName, String userName) { 99 Chat neighbour = null; 100 try { 101 neighbour = (Chat) ProActive.lookupActive(Chat.class.getName(), "//" + hostName + "/" + userName); this.diffusionGroup = neighbour.getDiffusionGroup(); this.writeUsersInTheList(); 104 this.diffusionGroup.addIntoDiffusionGroup((Chat) ProActive.getStubOnThis(), this.name); ProActiveGroup.getGroup(this.diffusionGroup).add((Chat) ProActive.getStubOnThis()); this.frame.list.append(this.name+"\n"); 107 this.writeMessage(new Message(" *** " + this.name + " has joined the place")); 108 } 109 catch (ActiveObjectCreationException e) { e.printStackTrace(); } 110 catch (IOException e) { 111 this.writePrivateMessage(new Message(" *** WARNING : Unable to contact " + userName + "@" + hostName + " !")); 112 this.writePrivateMessage(new Message(" *** WARNING : Starting alone !")); 113 this.startAlone(); 114 } 115 } 116 117 122 public void addIntoDiffusionGroup(Chat c, String name) { 123 ProActiveGroup.getGroup(this.diffusionGroup).add(c); 124 this.frame.list.append(name + "\n"); 125 } 126 127 130 public void disconnect () { 131 this.writeMessage(new Message(" *** " + this.name + " has left")); 132 this.diffusionGroup.removeUserFromTheList(this.name); 133 this.diffusionGroup.removeFromDiffusionGroup((Chat) ProActive.getStubOnThis()); 134 ProActiveGroup.getGroup(this.diffusionGroup).remove((Chat) ProActive.getStubOnThis()); 135 } 136 137 141 public void removeFromDiffusionGroup(Chat c) { 142 ProActiveGroup.getGroup(this.diffusionGroup).remove(c); 143 } 144 145 146 149 public void runActivity(Body body) { 150 Service service = new Service(body); 151 this.register(); 152 this.rebuildFrame(); 153 this.initializeMigrationStrategy(); 154 this.replayMessages(); 155 while (body.isActive()) { 156 service.blockingServeOldest(); 157 } 158 } 159 160 163 public void initializeMigrationStrategy () { 164 if (this.migrationStrategy == null) { 165 this.migrationStrategy = new MigrationStrategyManagerImpl((Migratable) ProActive.getBodyOnThis()); 166 this.migrationStrategy.onDeparture("onDeparture"); 167 } 168 } 169 170 173 public void onDeparture () { 174 this.disposeFrame(); 175 this.unregister(); 176 } 177 178 181 public void register () { 182 try { 183 ProActive.register(ProActive.getStubOnThis(), "//localhost/" + this.name); } 184 catch (IOException e) { e.printStackTrace(); } 185 } 186 187 190 public void unregister () { 191 try { 192 ProActive.unregister("//localhost/" + this.name); } 193 catch (IOException e) { e.printStackTrace(); } 194 } 195 196 199 public void disposeFrame() { 200 this.listOfName = this.frame.list.getText(); 201 if (this.frame != null) { 202 this.frame.dispose(); 203 this.frame = null; 204 } 205 } 206 207 210 public void rebuildFrame() { 211 this.frame = new ChatGUI((Chat)ProActive.getStubOnThis(),name); 212 this.frame.list.setText(this.listOfName); 213 } 214 215 218 public void replayMessages() { 219 for (int i=0 ; i < this.messageLogger.size() ; i++) { 220 this.frame.text.append(((Message)this.messageLogger.get(i)).toString()); 221 } 222 } 223 224 228 public void migrateTo(String nodeURL) { 229 this.writePrivateMessage(new Message(" *** I move to " + nodeURL)); 230 try { 231 ProActive.migrateTo(nodeURL); } 232 catch (MigrationException e) { 233 this.writePrivateMessage(new Message (" *** WARNING : Unable to move to " + nodeURL + " !")); 234 } 235 } 236 237 238 242 public void writePrivateMessage (Message m) { 243 this.messageLogger.add(m); 244 this.frame.text.append(m.toString()); 245 } 246 247 251 public void writeMessage (Message m) { 252 this.diffusionGroup.writePrivateMessage(m); 253 } 254 255 258 public void writeUsersInTheList () { 259 java.util.Iterator it = ProActiveGroup.getGroup(this.diffusionGroup).iterator(); 260 while (it.hasNext()) 261 this.frame.list.append(((Chat) it.next()).getName()+"\n"); 262 } 263 264 268 public void removeUserFromTheList (String userName) { 269 this.frame.list.setText(this.frame.list.getText().replaceAll(userName+"\n","")); 270 } 271 272 public static void main (String [] args) { 273 274 String userName; 275 String neighbourHost = null; 276 String neighbourName = null; 277 ProActiveConfiguration.load(); 278 279 if ((args.length != 1) && (args.length != 3)) { 280 logger.info("usage : chat.[sh|bat] UserName [ServerHost ServerName]"); 281 System.exit(0); 282 } 283 284 userName = args[0]; 285 if (args.length == 3) { 286 neighbourHost = args[1]; 287 neighbourName = args[2]; 288 } 289 290 291 Chat chat = null; 292 try { 293 Object [] param = new Object [1]; param[0] = new String (userName); 294 chat = (Chat) ProActive.newActive(Chat.class.getName(), param, (Node) null); } 295 catch (ActiveObjectCreationException e) { e.printStackTrace(); } 296 catch (NodeException e) { e.printStackTrace(); } 297 298 if ((neighbourHost == null) || (neighbourName == null)) 299 chat.startAlone(); 300 else 301 chat.connect(neighbourHost,neighbourName); 302 303 } 304 305 306 } 307 | Popular Tags |