KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > cs > Client


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.examples.cs;
32
33 import org.apache.log4j.Logger;
34
35
36 /**
37  * <p>
38  * A client/server example made using ProActive. In one window you
39  * can launch the server using the script cs_server without
40  * parameters. Then, in different windows, you can launch several
41  * clients using the script cs_client and passing the name of the
42  * client as a argument. Each client declares itself to the server
43  * and sends messages. The server broadcasts the messages to all
44  * referenced clients.
45  * </p>
46  *
47  * @author ProActive Team
48  * @version 1.0, 2001/10/23
49  * @since ProActive 0.9
50  *
51  */

52 public class Client {
53     
54     static Logger logger = Logger.getLogger(Client.class.getName());
55
56   protected String JavaDoc myName;
57   protected String JavaDoc serverHostName;
58   protected Server theServer;
59   private java.text.DateFormat JavaDoc dateFormat = new java.text.SimpleDateFormat JavaDoc("dd/MM/yyyy HH:mm:ss");
60
61
62   public Client() {
63   }
64
65
66   public Client(String JavaDoc clientName, String JavaDoc serverHostName) throws Exception JavaDoc {
67     this.myName = clientName;
68     this.serverHostName = serverHostName;
69     // Looks up for the server
70
String JavaDoc 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     // Registers myself with the server
79
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     // Gets the message from the server and prints it out
90
//System.out.println (this.myName+": message is "+this.theServer.getMessageOfTheDay());
91
// Sets a new message on the server
92
theServer.setMessageOfTheDay(this.myName + " is connected (" + dateFormat.format(new java.util.Date JavaDoc()) + ")");
93     //System.out.println (this.myName+": new message sent to the server");
94
}
95
96
97   public void messageChanged(String JavaDoc newMessage) {
98     System.out.println(this.myName + ": message changed: " + newMessage);
99   }
100
101
102   public static void main(String JavaDoc[] args) {
103     String JavaDoc clientName;
104     String JavaDoc 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       // Creates an active object for the client
118
Client theClient = (Client)org.objectweb.proactive.ProActive.newActive(Client.class.getName(), new Object JavaDoc[]{clientName,serverHostName});
119       theClient.init();
120       Thread JavaDoc t = new Thread JavaDoc(new RunClient(theClient));
121       t.start();
122     } catch (Exception JavaDoc e) {
123       e.printStackTrace();
124       System.exit(1);
125     }
126   }
127
128
129   private static class RunClient implements Runnable JavaDoc {
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 JavaDoc e) {
148           }
149         } catch (Exception JavaDoc e) {
150           shouldRun = false;
151         }
152       }
153     }
154   }
155 }
Popular Tags