KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > ext > util > SimpleLocationServer


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.ext.util;
32
33 import org.apache.log4j.Logger;
34
35 import org.objectweb.proactive.ProActive;
36 import org.objectweb.proactive.core.UniqueID;
37 import org.objectweb.proactive.core.body.BodyMap;
38 import org.objectweb.proactive.core.body.UniversalBody;
39 import org.objectweb.proactive.core.node.NodeFactory;
40 import org.objectweb.proactive.ext.locationserver.LocationServer;
41
42
43 /**
44  * An implementation of a Location Server
45  */

46 public class SimpleLocationServer implements org.objectweb.proactive.RunActive,
47     LocationServer {
48     static Logger logger = Logger.getLogger(SimpleLocationServer.class.getName());
49     private BodyMap table;
50     private String JavaDoc url;
51
52     public SimpleLocationServer() {
53     }
54
55     public SimpleLocationServer(String JavaDoc url) {
56         this.url = normalizeURL(url);
57         this.table = new BodyMap();
58     }
59
60     /**
61  * Update the location for the mobile object s
62  * with id
63  */

64     public void updateLocation(UniqueID i, UniversalBody s) {
65         // System.out.println("Server: updateLocation() " + i + " object = " + s);
66
table.updateBody(i, s);
67     }
68
69     /**
70  * Return a reference to the remote body if available.
71  * Return null otherwise
72  */

73     public UniversalBody searchObject(UniqueID id) {
74         return (UniversalBody) table.getBody(id);
75     }
76
77     /**
78  * First register with the specified url
79  * Then wait for request
80  */

81     public void runActivity(org.objectweb.proactive.Body body) {
82         this.register();
83         org.objectweb.proactive.Service service = new org.objectweb.proactive.Service(body);
84         service.fifoServing();
85     }
86
87     protected String JavaDoc normalizeURL(String JavaDoc url) {
88         String JavaDoc tmp = url;
89
90         //if it starts with rmi we remove it
91
if (url.startsWith("rmi:")) {
92             tmp = url.substring(4);
93         }
94         if (url.startsWith("ibis:")) {
95                    tmp = url.substring(5);
96                }
97
98         if (!tmp.startsWith("//")) {
99             tmp = "//" + tmp;
100         }
101         return tmp;
102     }
103
104     protected void register() {
105         try {
106             logger.info("Attempt at binding : " + url);
107             ProActive.register(ProActive.getStubOnThis(), url);
108             logger.info("Location Server bound in registry : " + url);
109         } catch (Exception JavaDoc e) {
110             logger.fatal("Cannot bind in registry - aborting " + url);
111             e.printStackTrace();
112             return;
113         }
114     }
115
116     public static void main(String JavaDoc[] args) {
117         if (args.length < 1) {
118             logger.error(
119                 "usage: java org.objectweb.proactive.ext.util.SimpleLocationServer <server url> [node]");
120             System.exit(-1);
121         }
122         Object JavaDoc[] arg = new Object JavaDoc[1];
123         arg[0] = args[0];
124         SimpleLocationServer server = null;
125         try {
126             if (args.length == 2) {
127                 server = (SimpleLocationServer) ProActive.newActive("org.objectweb.proactive.ext.util.SimpleLocationServer",
128                         arg, NodeFactory.getNode(args[1]));
129             } else {
130                 server = (SimpleLocationServer) ProActive.newActive("org.objectweb.proactive.ext.util.SimpleLocationServer",
131                         arg);
132             }
133         } catch (Exception JavaDoc e) {
134             e.printStackTrace();
135         }
136     }
137 }
138
Popular Tags