KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > datashare > TransmitDataThread


1 /* ----- BEGIN LICENSE BLOCK -----
2  * Version: MPL 1.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is the DataShare server.
15  *
16  * The Initial Developer of the Original Code is
17  * Ball Aerospace & Technologies Corp, Fairborn, Ohio
18  * Portions created by the Initial Developer are Copyright (C) 2001
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s): Charles Wood <cwood@ball.com>
22  *
23  * ----- END LICENSE BLOCK ----- */

24 /* RCS $Id: TransmitDataThread.java,v 1.2 2002/01/29 20:50:17 lizellaman Exp $
25  * $Log: TransmitDataThread.java,v $
26  * Revision 1.2 2002/01/29 20:50:17 lizellaman
27  * Added LoggingInterface, modified the PropertiesInterface implementation
28  *
29  * Revision 1.1.1.1 2001/10/23 13:37:16 lizellaman
30  * initial sourceforge release
31  *
32  */

33
34 package org.datashare;
35
36 import org.datashare.objects.DataShareObject;
37
38 /**
39  * this class was created in an attempt to move the re-transmission of data from the receiving SocketAdapter's
40  * Thread, to a sending Socket's transmission Thread, as provided by this class. The receiving
41  * Socket now uses this class to have the data to be re-transmitted buffered and transmiitted by a Thread
42  * other than its own (this one!).
43  * @date March 01, 2001
44  */

45 class TransmitDataThread extends Thread JavaDoc
46    {
47    private boolean running = true;
48    private SocketAdapter sa;
49    private DataShareObject dso;
50    private FifoQueue pipe = new FifoQueue();
51
52    /**
53     * constructor, this thread must be started before addData() should be called.
54     * @param sa our SocketAdapter which is used to transmit any DataShareObjects passed into
55     * the addData method.
56     */

57    public TransmitDataThread(SocketAdapter sa)
58       {
59       this.sa = sa;
60       }
61
62    /**
63     * used to put the DataShareObjects to be transmited by our SocketAdapter into a buffer
64     * prior to transmitting them, should not be called from this thread.
65     * @param dso the object to be transmitted over our SocketAdapater's connection
66     */

67    public void addData(DataShareObject dso)
68       {
69       pipe.write(dso);
70       }
71
72    /**
73     * call this method when it is desired to stop this Thread.
74     */

75    public void stopThread()
76       {
77       SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
78          SessionUtilities.getLoggingInterface().NETWORK,
79          "stopThread() called for " + getName());
80       running = false;
81       pipe.close();
82       }
83
84    /**
85     * tests to see if there is any data to transmit, transmits it using our SocketAdapter,
86     * then waits for more data.
87     */

88    public void run()
89       {
90       while(running)
91          {
92          if(running)
93             {
94             dso = (DataShareObject)pipe.read(); // block if nothing available
95
if(dso != null)
96                {
97                if(SessionUtilities.getVerbose())
98                   {
99                   int count = pipe.size();
100                   SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
101                      SessionUtilities.getLoggingInterface().NETWORK,
102                      "4-Xmitting " + dso.sendingClientKey + " object to " + sa.getKeyValue() + ((count>1)?(", " + count + " objects backlogged"):""));
103                   }
104                sa.xmitData(dso);
105                yield();
106                }
107             else
108                {
109                SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
110                   SessionUtilities.getLoggingInterface().NETWORK,
111                   "TransmitDataThread pipe returned null for " + this.getName() + ", closing xmitThread...");
112                this.stopThread();
113                }
114             }
115          } // end of while
116
SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
117          SessionUtilities.getLoggingInterface().NETWORK,
118          "Thread " + getName() + " has stopped");
119       }
120
121    }
122
Popular Tags