KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.objectweb.proactive.core.config.ProActiveConfiguration;
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 Server {
53     
54     static Logger logger = Logger.getLogger(Server.class.getName());
55
56   protected String JavaDoc messageOfTheDay;
57   protected java.util.ArrayList JavaDoc clients;
58
59
60   public Server() {
61   }
62
63
64   public Server(String JavaDoc messageOfTheDay) {
65     this.clients = new java.util.ArrayList JavaDoc();
66     this.messageOfTheDay = messageOfTheDay;
67   }
68
69
70   public String JavaDoc getMessageOfTheDay() {
71     return messageOfTheDay;
72   }
73
74
75   public void setMessageOfTheDay(String JavaDoc messageOfTheDay) {
76     logger.info("Server: new message: " + messageOfTheDay);
77     this.messageOfTheDay = messageOfTheDay;
78     this.notifyClients();
79   }
80
81
82   public void register(Client c) {
83     this.clients.add(c);
84   }
85
86
87   public void unregister(Client c) {
88     this.clients.remove(c);
89   }
90
91
92   protected void notifyClients() {
93     java.util.Iterator JavaDoc it = this.clients.iterator();
94     Client currentClient;
95
96     while (it.hasNext()) {
97       currentClient = (Client)it.next();
98       try {
99         currentClient.messageChanged(this.messageOfTheDay);
100       } catch (Exception JavaDoc t) {
101         it.remove();
102       }
103     }
104   }
105
106
107   public static void main(String JavaDoc[] args) {
108     ProActiveConfiguration.load();
109     try {
110       // Creates an active object for the server
111
Server theServer = (Server)org.objectweb.proactive.ProActive.newActive(Server.class.getName(), new Object JavaDoc[]{"This is the first message"});
112       //Server theServer = (Server) org.objectweb.proactive.ProActive.newActive(Server.class.getName(), null, null);
113

114       // Binds the server to a specific URL
115
org.objectweb.proactive.ProActive.register(theServer, "///theServer");
116
117       System.out.println("Server is ready.");
118     } catch (Exception JavaDoc e) {
119       e.printStackTrace();
120     }
121   }
122 }
Popular Tags