001: /*
002: * <copyright>
003: *
004: * Copyright 2003-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.lib.aggagent.test;
028:
029: import java.util.Iterator;
030:
031: import org.cougaar.core.agent.service.alarm.Alarm;
032: import org.cougaar.core.blackboard.IncrementalSubscription;
033: import org.cougaar.core.mts.MessageAddress;
034: import org.cougaar.core.plugin.ComponentPlugin;
035:
036: /**
037: * This is an example demonstrating the use of the Relay mechanism.
038: * It complements the ReceiverPlugin by sending messages to the address under
039: * which the ReceiverPlugin registers itself.
040: * <br><br>
041: * Incidentally, this class also demonstrates the usage of the AlarmService
042: * interface, which is registered in superclass ComponentPlugin. Note the use
043: * of an Alarm implementation.
044: */
045: public class SenderPlugin extends ComponentPlugin {
046:
047: /** Holds value of property UIDService. */
048: private org.cougaar.core.service.UIDService UIDService;
049: MessageAddress source;
050: MessageAddress target;
051:
052: private class SendAfterDelay implements Alarm {
053: private long detonate = -1;
054: private boolean expired = false;
055:
056: public SendAfterDelay() {
057: detonate = 3000l + System.currentTimeMillis();
058: }
059:
060: public long getExpirationTime() {
061: return detonate;
062: }
063:
064: public void expire() {
065: if (!expired)
066: sendMessage();
067: expired = true;
068: }
069:
070: public boolean hasExpired() {
071: return expired;
072: }
073:
074: public boolean cancel() {
075: if (!expired)
076: return expired = true;
077: return false;
078: }
079: }
080:
081: private void delayedSend() {
082: alarmService.addRealTimeAlarm(new SendAfterDelay());
083: }
084:
085: private void sendMessage() {
086: System.out.println("SenderPlugin::sendMessage ...");
087:
088: TestRelay tr = new TestRelay(getUIDService().nextUID(), source,
089: target, "Foo", null);
090: getBlackboardService().openTransaction();
091: getBlackboardService().publishAdd(tr);
092: getBlackboardService().closeTransaction();
093:
094: System.out.println("SenderPlugin::sendMessage: done");
095: delayedSend();
096: }
097:
098: IncrementalSubscription sub;
099:
100: public void setupSubscriptions() {
101: source = getAgentIdentifier();
102: Iterator iter = this .getParameters().iterator();
103: while (iter.hasNext()) {
104: MessageAddress addr = MessageAddress
105: .getMessageAddress((String) iter.next());
106: target = addr;
107: System.out.println("Sending to : " + addr);
108: }
109: sub = (IncrementalSubscription) getBlackboardService()
110: .subscribe(new GetTestRelayPredicate());
111: delayedSend();
112: }
113:
114: /**
115: * Called every time this component is scheduled to run.
116: */
117: protected void execute() {
118: System.out.println("SenderPlugin: execute");
119: Iterator iter = sub.getAddedCollection().iterator();
120: while (iter.hasNext()) {
121: System.out.println(" --- Added: " + iter.next());
122: }
123: iter = sub.getChangedCollection().iterator();
124: while (iter.hasNext()) {
125: TestRelay tr = (TestRelay) iter.next();
126: System.out.println(" --- Changed: " + tr);
127: if (tr.getContent().equals("Foo")) {
128: tr.updateContent("Heard you", null);
129: getBlackboardService().publishChange(tr);
130: }
131: }
132:
133: }
134:
135: /** Getter for property UIDService.
136: * @return Value of property UIDService.
137: */
138: public org.cougaar.core.service.UIDService getUIDService() {
139: return this .UIDService;
140: }
141:
142: /** Setter for property UIDService.
143: * @param UIDService New value of property UIDService.
144: */
145: public void setUIDService(
146: org.cougaar.core.service.UIDService UIDService) {
147: this.UIDService = UIDService;
148: }
149:
150: }
|