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.mts;
028:
029: import java.io.IOException;
030: import java.io.ObjectInput;
031: import java.io.ObjectOutput;
032: import java.io.Serializable;
033:
034: /**
035: * A message is an object sent from one agent (the "originator")
036: * to another agent (the "target") through the {@link
037: * org.cougaar.core.service.MessageTransportService}.
038: */
039: public abstract class Message implements Serializable {
040:
041: private static final MessageAddress sink = MessageAddress.NULL_SYNC;
042:
043: // sender:
044: private MessageAddress theOriginator;
045: // target:
046: private MessageAddress theTarget;
047: // optional sequence number:
048: private int theSequenceNumber = 0;
049:
050: /**
051: * Default Constructor for factory.
052: */
053: public Message() {
054: this (sink, sink, 0);
055: }
056:
057: /**
058: * Standard message contructor, which specifies the source and
059: * target agent addresses.
060: * <p>
061: * @param aSource creator of this message
062: * @param aTarget target for this message
063: */
064: public Message(MessageAddress aSource, MessageAddress aTarget) {
065: this (aSource, aTarget, 0);
066: }
067:
068: /**
069: * Message constructor with sequence number.
070: *
071: * @param aSource creator of this message
072: * @param aTarget target for this message
073: * @param anId Sequence number
074: */
075: public Message(MessageAddress aSource, MessageAddress aTarget,
076: int anId) {
077: setOriginator(aSource);
078: setTarget(aTarget);
079: setContentsId(anId);
080: }
081:
082: /**
083: * Copy constructor.
084: *
085: * @param aMessage The message to use as the data source for construction.
086: */
087: public Message(Message aMessage) {
088: this (aMessage.getOriginator(), aMessage.getTarget(), aMessage
089: .getContentsId());
090: }
091:
092: /** @return the sequence number */
093: public final int getContentsId() {
094: return theSequenceNumber;
095: }
096:
097: /** @return the sender */
098: public final MessageAddress getOriginator() {
099: return theOriginator;
100: }
101:
102: /** @return the destination */
103: public final MessageAddress getTarget() {
104: return theTarget;
105: }
106:
107: /** Set the sequence number */
108: public final void setContentsId(int aContentsId) {
109: theSequenceNumber = aContentsId;
110: }
111:
112: /** Set the sender */
113: public final void setOriginator(MessageAddress aSource) {
114: theOriginator = aSource;
115: }
116:
117: /** Set the destination */
118: public final void setTarget(MessageAddress aTarget) {
119: theTarget = aTarget;
120: }
121:
122: public String toString() {
123: try {
124: return "The source: " + getOriginator().toString()
125: + " The Target: " + getTarget().toString()
126: + " The Message Id: " + getContentsId();
127: } catch (NullPointerException npe) {
128: String output = "a Malformed Message: ";
129: if (getOriginator() != null)
130: output += " The source: " + getOriginator().toString();
131: else
132: output += " The source: NULL";
133: if (getTarget() != null)
134: output += "The Target: " + getTarget().toString();
135: else
136: output += " The Target: NULL";
137:
138: return output;
139: }
140: }
141:
142: // externalizable support
143: // we don't actually implement the Externalizable interface, so it is
144: // up to subclasses to call these methods.
145: public void writeExternal(ObjectOutput out) throws IOException {
146: out.writeObject(theOriginator);
147: out.writeObject(theTarget);
148: out.writeInt(theSequenceNumber);
149: }
150:
151: public void readExternal(ObjectInput in)
152: throws ClassNotFoundException, IOException {
153: theOriginator = (MessageAddress) in.readObject();
154: theTarget = (MessageAddress) in.readObject();
155: theSequenceNumber = in.readInt();
156: }
157: }
|