001: /* ----- BEGIN LICENSE BLOCK -----
002: * Version: MPL 1.1
003: *
004: * The contents of this file are subject to the Mozilla Public License Version
005: * 1.1 (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS" basis,
010: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
011: * for the specific language governing rights and limitations under the
012: * License.
013: *
014: * The Original Code is the DataShare server.
015: *
016: * The Initial Developer of the Original Code is
017: * Ball Aerospace & Technologies Corp, Fairborn, Ohio
018: * Portions created by the Initial Developer are Copyright (C) 2001
019: * the Initial Developer. All Rights Reserved.
020: *
021: * Contributor(s): Charles Wood <cwood@ball.com>
022: *
023: * ----- END LICENSE BLOCK ----- */
024: /* RCS $Id: TransmitDataThread.java,v 1.2 2002/01/29 20:50:17 lizellaman Exp $
025: * $Log: TransmitDataThread.java,v $
026: * Revision 1.2 2002/01/29 20:50:17 lizellaman
027: * Added LoggingInterface, modified the PropertiesInterface implementation
028: *
029: * Revision 1.1.1.1 2001/10/23 13:37:16 lizellaman
030: * initial sourceforge release
031: *
032: */
033:
034: package org.datashare;
035:
036: import org.datashare.objects.DataShareObject;
037:
038: /**
039: * this class was created in an attempt to move the re-transmission of data from the receiving SocketAdapter's
040: * Thread, to a sending Socket's transmission Thread, as provided by this class. The receiving
041: * Socket now uses this class to have the data to be re-transmitted buffered and transmiitted by a Thread
042: * other than its own (this one!).
043: * @date March 01, 2001
044: */
045: class TransmitDataThread extends Thread {
046: private boolean running = true;
047: private SocketAdapter sa;
048: private DataShareObject dso;
049: private FifoQueue pipe = new FifoQueue();
050:
051: /**
052: * constructor, this thread must be started before addData() should be called.
053: * @param sa our SocketAdapter which is used to transmit any DataShareObjects passed into
054: * the addData method.
055: */
056: public TransmitDataThread(SocketAdapter sa) {
057: this .sa = sa;
058: }
059:
060: /**
061: * used to put the DataShareObjects to be transmited by our SocketAdapter into a buffer
062: * prior to transmitting them, should not be called from this thread.
063: * @param dso the object to be transmitted over our SocketAdapater's connection
064: */
065: public void addData(DataShareObject dso) {
066: pipe.write(dso);
067: }
068:
069: /**
070: * call this method when it is desired to stop this Thread.
071: */
072: public void stopThread() {
073: SessionUtilities.getLoggingInterface().debugMsg(
074: SessionUtilities.getLoggingInterface().DEBUG,
075: SessionUtilities.getLoggingInterface().NETWORK,
076: "stopThread() called for " + getName());
077: running = false;
078: pipe.close();
079: }
080:
081: /**
082: * tests to see if there is any data to transmit, transmits it using our SocketAdapter,
083: * then waits for more data.
084: */
085: public void run() {
086: while (running) {
087: if (running) {
088: dso = (DataShareObject) pipe.read(); // block if nothing available
089: if (dso != null) {
090: if (SessionUtilities.getVerbose()) {
091: int count = pipe.size();
092: SessionUtilities
093: .getLoggingInterface()
094: .debugMsg(
095: SessionUtilities
096: .getLoggingInterface().DEBUG,
097: SessionUtilities
098: .getLoggingInterface().NETWORK,
099: "4-Xmitting "
100: + dso.sendingClientKey
101: + " object to "
102: + sa.getKeyValue()
103: + ((count > 1) ? (", "
104: + count + " objects backlogged")
105: : ""));
106: }
107: sa.xmitData(dso);
108: yield();
109: } else {
110: SessionUtilities
111: .getLoggingInterface()
112: .debugMsg(
113: SessionUtilities
114: .getLoggingInterface().DEBUG,
115: SessionUtilities
116: .getLoggingInterface().NETWORK,
117: "TransmitDataThread pipe returned null for "
118: + this .getName()
119: + ", closing xmitThread...");
120: this .stopThread();
121: }
122: }
123: } // end of while
124: SessionUtilities.getLoggingInterface().debugMsg(
125: SessionUtilities.getLoggingInterface().DEBUG,
126: SessionUtilities.getLoggingInterface().NETWORK,
127: "Thread " + getName() + " has stopped");
128: }
129:
130: }
|