001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.core.agent;
028:
029: import java.net.InetAddress;
030: import java.net.URI;
031: import org.cougaar.core.component.Component;
032: import org.cougaar.core.component.ServiceBroker;
033: import org.cougaar.core.component.ServiceRevokedListener;
034: import org.cougaar.core.mts.MessageAddress;
035: import org.cougaar.core.node.NodeIdentificationService;
036: import org.cougaar.core.service.AgentIdentificationService;
037: import org.cougaar.core.service.EventService;
038: import org.cougaar.core.service.LoggingService;
039: import org.cougaar.core.service.wp.AddressEntry;
040: import org.cougaar.core.service.wp.WhitePagesService;
041: import org.cougaar.util.GenericStateModelAdapter;
042:
043: /**
044: * This component generates {@link EventService} events
045: * to announce the agent's load/start/stop/<i>etc</i> and move.
046: */
047: public final class Events extends GenericStateModelAdapter implements
048: Component {
049:
050: private ServiceBroker sb;
051:
052: private LoggingService log;
053: private EventService eventService;
054: private WhitePagesService wps;
055:
056: private MobilityNotificationClient mnc;
057: private MobilityNotificationService mns;
058:
059: private MessageAddress moveTargetNode;
060:
061: private MessageAddress localAgent;
062: private long localIncarnation;
063: private MessageAddress localNode;
064: private String localHost;
065:
066: public void setServiceBroker(ServiceBroker sb) {
067: this .sb = sb;
068: }
069:
070: public void load() {
071: super .load();
072: obtainServices();
073: }
074:
075: public void start() {
076: super .start();
077: event("Started");
078: }
079:
080: public void suspend() {
081: super .suspend();
082: if (moveTargetNode != null) {
083: event("Moving");
084: }
085: }
086:
087: public void resume() {
088: super .resume();
089: if (moveTargetNode != null) {
090: event("NotMoved");
091: moveTargetNode = null;
092: }
093: }
094:
095: public void stop() {
096: super .stop();
097: if (moveTargetNode == null) {
098: event("Stopped");
099: } else {
100: event("Moved");
101: }
102: }
103:
104: public void unload() {
105: super .unload();
106: releaseServices();
107: }
108:
109: private void obtainServices() {
110: log = (LoggingService) sb.getService(this ,
111: LoggingService.class, null);
112:
113: eventService = (EventService) sb.getService(this ,
114: EventService.class, null);
115: if (eventService == null) {
116: throw new RuntimeException("Unable to obtain EventService");
117: }
118:
119: wps = (WhitePagesService) sb.getService(this ,
120: WhitePagesService.class, null);
121:
122: // local agent
123: AgentIdentificationService ais = (AgentIdentificationService) sb
124: .getService(this , AgentIdentificationService.class,
125: null);
126: if (ais != null) {
127: localAgent = ais.getMessageAddress();
128: sb.releaseService(this , AgentIdentificationService.class,
129: ais);
130: }
131:
132: // local agent's incarnation
133: TopologyService ts = (TopologyService) sb.getService(this ,
134: TopologyService.class, null);
135: if (ts != null) {
136: localIncarnation = ts.getIncarnationNumber();
137: sb.releaseService(this , TopologyService.class, ts);
138: }
139:
140: // local node
141: NodeIdentificationService nis = (NodeIdentificationService) sb
142: .getService(this , NodeIdentificationService.class, null);
143: if (nis != null) {
144: localNode = nis.getMessageAddress();
145: sb.releaseService(this , NodeIdentificationService.class,
146: nis);
147: }
148:
149: // local host
150: try {
151: InetAddress localAddr = InetAddress.getLocalHost();
152: localHost = localAddr.getHostName();
153: } catch (Exception e) {
154: localHost = "?";
155: }
156:
157: // mobility watcher
158: mnc = new MobilityNotificationClient() {
159: public void movingTo(MessageAddress destinationNode) {
160: moveTargetNode = destinationNode;
161: }
162: };
163: mns = (MobilityNotificationService) sb.getService(mnc,
164: MobilityNotificationService.class, null);
165: if (mns == null && log.isInfoEnabled()) {
166: log.info("Unable to obtain MobilityNotificationService"
167: + ", mobility is disabled");
168: }
169: }
170:
171: private void event(String cycle) {
172: if (eventService == null || !eventService.isEventEnabled()) {
173: return;
174: }
175: String tail;
176: if (moveTargetNode == null) {
177: tail = "";
178: } else {
179: String moveTargetHost = "?";
180: try {
181: AddressEntry nodeEntry = wps.get(moveTargetNode
182: .getAddress(), "topology", 10000); // wait at most 10 seconds
183: if (nodeEntry != null) {
184: moveTargetHost = nodeEntry.getURI().getHost();
185: }
186: } catch (Exception e) {
187: if (log.isInfoEnabled()) {
188: log.info("Unable to get host for destination node "
189: + moveTargetNode, e);
190: }
191: }
192: tail = " ToNode(" + moveTargetNode + ") ToHost("
193: + moveTargetHost + ")";
194: }
195: eventService.event("AgentLifecycle(" + cycle + ") Agent("
196: + localAgent + ") Node(" + localNode + ") Host("
197: + localHost + ") Incarnation(" + localIncarnation + ")"
198: + tail);
199: }
200:
201: private void releaseServices() {
202: if (mns != null) {
203: sb.releaseService(mnc, MobilityNotificationService.class,
204: mns);
205: mns = null;
206: }
207: if (wps != null) {
208: sb.releaseService(this , WhitePagesService.class, wps);
209: wps = null;
210: }
211: if (eventService != null) {
212: sb.releaseService(this , EventService.class, eventService);
213: eventService = null;
214: }
215: }
216: }
|