KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > body > migration > MigrationManagerImpl


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.core.body.migration;
32
33 import org.apache.log4j.Logger;
34
35 import org.objectweb.proactive.Body;
36 import org.objectweb.proactive.core.ProActiveException;
37 import org.objectweb.proactive.core.body.UniversalBody;
38 import org.objectweb.proactive.core.body.reply.ReplyReceiver;
39 import org.objectweb.proactive.core.body.reply.ReplyReceiverForwarder;
40 import org.objectweb.proactive.core.body.request.RequestReceiver;
41 import org.objectweb.proactive.core.body.request.RequestReceiverForwarder;
42 import org.objectweb.proactive.core.event.AbstractEventProducer;
43 import org.objectweb.proactive.core.event.MigrationEvent;
44 import org.objectweb.proactive.core.event.MigrationEventListener;
45 import org.objectweb.proactive.core.event.ProActiveEvent;
46 import org.objectweb.proactive.core.event.ProActiveListener;
47 import org.objectweb.proactive.core.node.Node;
48 import org.objectweb.proactive.core.node.NodeFactory;
49 import org.objectweb.proactive.core.runtime.ProActiveRuntime;
50
51
52 public class MigrationManagerImpl extends AbstractEventProducer
53     implements MigrationManager, java.io.Serializable JavaDoc {
54     protected static Logger logger = Logger.getLogger(MigrationManagerImpl.class.getName());
55
56     //
57
// -- CONSTRUCTORS -----------------------------------------------
58
//
59
public MigrationManagerImpl() {
60         super(true);
61     }
62
63     //
64
// -- PUBLIC METHODS -----------------------------------------------
65
//
66
//
67
// -- Implements MigrationManager -----------------------------------------------
68
//
69
public Node checkNode(Node node) throws MigrationException {
70         if (node == null) {
71             throw new MigrationException(
72                 "The RemoteNodeImpl could not be found");
73         }
74
75         // check if the node is remote
76
if (NodeFactory.isNodeLocal(node)) {
77             MigrationException me = new MigrationException("The given node " +
78                     node.getNodeInformation().getURL() +
79                     " is in the same virtual machine");
80             if (hasListeners()) {
81                 notifyAllListeners(new MigrationEvent(me));
82             }
83             throw me;
84         }
85         return node;
86     }
87
88     public UniversalBody migrateTo(Node node, Body body)
89         throws MigrationException {
90         if (hasListeners()) {
91             notifyAllListeners(new MigrationEvent(body,
92                     MigrationEvent.BEFORE_MIGRATION));
93         }
94         try {
95             long l1 = 0;
96             if (logger.isDebugEnabled()) {
97                 l1 = System.currentTimeMillis();
98             }
99
100             //
101
//UniversalBody remoteBody = node.receiveBody(body);
102
//--------------------added lines---------------------------
103
ProActiveRuntime part = node.getProActiveRuntime();
104             UniversalBody remoteBody = part.receiveBody(node.getNodeInformation()
105                                                             .getName(), body);
106
107             if (logger.isDebugEnabled()) {
108                 logger.debug("runtime = " + part);
109                 logger.debug("remoteBody = " + remoteBody);
110             }
111
112             //--------------------added lines--------------------------
113
//activityStopped();
114
//
115
long l2 = 0;
116             if (logger.isDebugEnabled()) {
117                 l2 = System.currentTimeMillis();
118                 logger.debug("Migration took " + (l2 - l1));
119             }
120             if (hasListeners()) {
121                 notifyAllListeners(new MigrationEvent(body,
122                         MigrationEvent.AFTER_MIGRATION));
123             }
124             return remoteBody;
125         } catch (ProActiveException e) {
126             MigrationException me = new MigrationException("Exception while sending the Object",
127                     e.getTargetException());
128             if (hasListeners()) {
129                 notifyAllListeners(new MigrationEvent(me));
130             }
131             throw me;
132         }
133     }
134
135     public void startingAfterMigration(Body body) {
136         if (hasListeners()) {
137             notifyAllListeners(new MigrationEvent(body,
138                     MigrationEvent.RESTARTING_AFTER_MIGRATING));
139         }
140     }
141
142     public RequestReceiver createRequestReceiver(UniversalBody remoteBody,
143         RequestReceiver currentRequestReceiver) {
144         return new RequestReceiverForwarder(remoteBody);
145     }
146
147     public ReplyReceiver createReplyReceiver(UniversalBody remoteBody,
148         ReplyReceiver currentReplyReceiver) {
149         return new ReplyReceiverForwarder(remoteBody);
150     }
151
152     public void addMigrationEventListener(MigrationEventListener listener) {
153         addListener(listener);
154     }
155
156     public void removeMigrationEventListener(MigrationEventListener listener) {
157         removeListener(listener);
158     }
159
160     //
161
// -- PROTECTED METHODS -----------------------------------------------
162
//
163
protected void notifyOneListener(ProActiveListener listener,
164         ProActiveEvent event) {
165         MigrationEvent migrationEvent = (MigrationEvent) event;
166         MigrationEventListener migrationEventListener = (MigrationEventListener) listener;
167         switch (event.getType()) {
168         case MigrationEvent.BEFORE_MIGRATION:
169             migrationEventListener.migrationAboutToStart(migrationEvent);
170             break;
171         case MigrationEvent.AFTER_MIGRATION:
172             migrationEventListener.migrationFinished(migrationEvent);
173             break;
174         case MigrationEvent.MIGRATION_EXCEPTION:
175             migrationEventListener.migrationExceptionThrown(migrationEvent);
176             break;
177         case MigrationEvent.RESTARTING_AFTER_MIGRATING:
178             migrationEventListener.migratedBodyRestarted(migrationEvent);
179             break;
180         }
181     }
182 }
183
Popular Tags