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 org.cougaar.core.agent.service.alarm.Alarm;
030: import org.cougaar.core.agent.service.alarm.ExecutionTimer;
031: import org.cougaar.core.component.Component;
032: import org.cougaar.core.component.ServiceBroker;
033: import org.cougaar.core.component.ServiceProvider;
034: import org.cougaar.core.component.ServiceRevokedListener;
035: import org.cougaar.core.mts.MessageAddress;
036: import org.cougaar.core.node.service.NaturalTimeService;
037: import org.cougaar.core.node.service.RealTimeService;
038: import org.cougaar.core.service.AlarmService;
039: import org.cougaar.util.GenericStateModelAdapter;
040:
041: /**
042: * This component adds the agent's {@link AlarmService}, based
043: * upon the node-level {@link RealTimeService} and
044: * {@link NaturalTimeService}.
045: *
046: * @see AlarmService
047: */
048: public final class AlarmComponent extends GenericStateModelAdapter
049: implements Component {
050:
051: private ServiceBroker sb;
052:
053: private RealTimeService rTimer;
054: private NaturalTimeService xTimer;
055:
056: private AlarmSP asp;
057:
058: public void setServiceBroker(ServiceBroker sb) {
059: this .sb = sb;
060: }
061:
062: public void load() {
063: super .load();
064:
065: // get execution timer
066: xTimer = (NaturalTimeService) sb.getService(this ,
067: NaturalTimeService.class, null);
068:
069: // get real timer
070: rTimer = (RealTimeService) sb.getService(this ,
071: RealTimeService.class, null);
072:
073: // add alarm service
074: asp = new AlarmSP();
075: sb.addService(AlarmService.class, asp);
076: }
077:
078: public void unload() {
079: super .unload();
080:
081: sb.revokeService(AlarmService.class, asp);
082: asp = null;
083:
084: if (rTimer != null) {
085: sb.releaseService(this , RealTimeService.class, rTimer);
086: rTimer = null;
087: }
088:
089: if (xTimer != null) {
090: sb.releaseService(this , NaturalTimeService.class, xTimer);
091: xTimer = null;
092: }
093: }
094:
095: private class AlarmSP implements ServiceProvider {
096: private final AlarmService SERVICE_INSTANCE = new AlarmSI(
097: xTimer, rTimer);
098:
099: public Object getService(ServiceBroker sb, Object requestor,
100: Class serviceClass) {
101: if (AlarmService.class.isAssignableFrom(serviceClass)) {
102: return SERVICE_INSTANCE;
103: } else {
104: return null;
105: }
106: }
107:
108: public void releaseService(ServiceBroker sb, Object requestor,
109: Class serviceClass, Object service) {
110: }
111: }
112:
113: private static final class AlarmSI implements AlarmService {
114: private final NaturalTimeService xTimer;
115: private final RealTimeService rTimer;
116:
117: public AlarmSI(NaturalTimeService xTimer, RealTimeService rTimer) {
118: this .xTimer = xTimer;
119: this .rTimer = rTimer;
120: }
121:
122: // alarm service:
123: public long currentTimeMillis() {
124: return xTimer.currentTimeMillis();
125: }
126:
127: public void addAlarm(Alarm alarm) {
128: xTimer.addAlarm(alarm);
129: }
130:
131: public void addRealTimeAlarm(Alarm alarm) {
132: rTimer.addAlarm(alarm);
133: }
134:
135: // demo service:
136: private void die() {
137: throw new UnsupportedOperationException();
138: }
139:
140: public MessageAddress getMessageAddress() {
141: die();
142: return null;
143: }
144:
145: public void setSocietyTime(long time) {
146: die();
147: }
148:
149: public void setSocietyTime(long time, boolean leaveRunning) {
150: die();
151: }
152:
153: public void setSocietyTimeRate(double newRate) {
154: die();
155: }
156:
157: public void advanceSocietyTime(long timePeriod) {
158: die();
159: }
160:
161: public void advanceSocietyTime(long timePeriod,
162: boolean leaveRunning) {
163: die();
164: }
165:
166: public void advanceSocietyTime(long timePeriod, double newRate) {
167: die();
168: }
169:
170: public void advanceSocietyTime(ExecutionTimer.Change[] changes) {
171: die();
172: }
173:
174: public void advanceNodeTime(long timePeriod, double newRate) {
175: die();
176: }
177:
178: public void setNodeTime(long time, double newRate) {
179: die();
180: }
181:
182: public void setNodeTime(long time, double newRate,
183: long changeTime) {
184: die();
185: }
186:
187: public double getExecutionRate() {
188: die();
189: return -1;
190: }
191: }
192: }
|