1 31 package org.objectweb.proactive.examples.cs; 32 33 import org.apache.log4j.Logger; 34 35 36 52 public class Client { 53 54 static Logger logger = Logger.getLogger(Client.class.getName()); 55 56 protected String myName; 57 protected String serverHostName; 58 protected Server theServer; 59 private java.text.DateFormat dateFormat = new java.text.SimpleDateFormat ("dd/MM/yyyy HH:mm:ss"); 60 61 62 public Client() { 63 } 64 65 66 public Client(String clientName, String serverHostName) throws Exception { 67 this.myName = clientName; 68 this.serverHostName = serverHostName; 69 String urlAsString = "//" + serverHostName + "/theServer"; 71 logger.info("Client " + clientName + " is looking up server at " + urlAsString); 72 this.theServer = (Server)org.objectweb.proactive.ProActive.lookupActive(Server.class.getName(), urlAsString); 73 logger.info("Client " + this.myName + " successfully found the server"); 74 } 75 76 77 public void init() { 78 Client myself = (Client)org.objectweb.proactive.ProActive.getStubOnThis(); 80 if (myself != null) { 81 theServer.register(myself); 82 } else { 83 logger.info("Cannot get a stub on myself"); 84 } 85 } 86 87 88 public void doStuff() { 89 theServer.setMessageOfTheDay(this.myName + " is connected (" + dateFormat.format(new java.util.Date ()) + ")"); 93 } 95 96 97 public void messageChanged(String newMessage) { 98 System.out.println(this.myName + ": message changed: " + newMessage); 99 } 100 101 102 public static void main(String [] args) { 103 String clientName; 104 String serverHostName; 105 if (args.length < 1) { 106 System.out.println("Correct syntax is: client <client name> [server host name]"); 107 return; 108 } else if (args.length == 1) { 109 clientName = args[0]; 110 serverHostName = ""; 111 } else { 112 clientName = args[0]; 113 serverHostName = args[1]; 114 } 115 116 try { 117 Client theClient = (Client)org.objectweb.proactive.ProActive.newActive(Client.class.getName(), new Object []{clientName,serverHostName}); 119 theClient.init(); 120 Thread t = new Thread (new RunClient(theClient)); 121 t.start(); 122 } catch (Exception e) { 123 e.printStackTrace(); 124 System.exit(1); 125 } 126 } 127 128 129 private static class RunClient implements Runnable { 130 131 private Client client; 132 private boolean shouldRun = true; 133 134 135 public RunClient(Client client) { 136 this.client = client; 137 } 138 139 140 public void run() { 141 while (shouldRun) { 142 try { 143 client.doStuff(); 144 long l = 500 + (long)(Math.random() * 5000); 145 try { 146 Thread.sleep(l); 147 } catch (InterruptedException e) { 148 } 149 } catch (Exception e) { 150 shouldRun = false; 151 } 152 } 153 } 154 } 155 } | Popular Tags |