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: UdpSocket.java,v 1.2 2002/01/29 20:50:17 lizellaman Exp $
025: * $Log: UdpSocket.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 java.net.DatagramSocket;
037: import java.net.DatagramPacket;
038: import java.net.InetAddress;
039:
040: import java.io.EOFException;
041: import java.io.InvalidClassException;
042: import java.io.IOException;
043:
044: import org.datashare.objects.DataShareObject;
045: import org.datashare.objects.ChannelDescription;
046:
047: public class UdpSocket extends SocketAdapter {
048: DatagramSocket socket;
049: DatagramPacket sndPacket;
050: UdpSocketServer server;
051: private TransmitDataThread tdt = null;
052: boolean running = true;
053:
054: public UdpSocket(UdpSocketServer server, DatagramSocket socket,
055: DatagramPacket packet, DataReceiverInterface dri,
056: InetAddress myIpAddress) {
057: this .server = server;
058: this .socket = socket;
059: this .dri = dri;
060: this .localIP = myIpAddress; // socket.getLocalAddress() does not seem to work, even for bound sockets
061: this .localPort = socket.getLocalPort();
062: this .remoteIP = packet.getAddress();
063: this .remotePort = packet.getPort();
064: this .keyValue = "Socket-UDP-" + localIP.getHostAddress() + ":"
065: + localPort + "-" + remoteIP.getHostAddress() + ":"
066: + remotePort;
067: // create packet for sending, will overwrite byte[] and length prior to sending
068: sndPacket = new DatagramPacket(new byte[1], 1, remoteIP,
069: remotePort);
070: SessionUtilities.getLoggingInterface().debugMsg(
071: SessionUtilities.getLoggingInterface().DEBUG,
072: SessionUtilities.getLoggingInterface().NETWORK,
073: "New connection: " + keyValue + " with active set to "
074: + (this .getActive() ? "true" : "false"));
075:
076: // create our thread for buffering and transmiting data
077: tdt = new TransmitDataThread(this );
078: tdt.setName("XmitThread for " + keyValue);
079: try {
080: tdt.setPriority(Thread.currentThread().getPriority()
081: + SessionUtilities.SocketXmtRelativePriority); // make transmitting lower priority, since it has buffering
082: } catch (Exception e) {
083: SessionUtilities.getLoggingInterface().debugMsg(
084: SessionUtilities.getLoggingInterface().ERROR,
085: SessionUtilities.getLoggingInterface().NETWORK,
086: "Problems setting Xmt Thread priority:");
087: e.printStackTrace();
088: }
089: tdt.start();
090: }
091:
092: /**
093: * call this method when data is to be sent over this connection
094: */
095: public void sendData(DataShareObject dsObject) {
096: if (running) {
097: SessionUtilities.getLoggingInterface().debugMsg(
098: SessionUtilities.getLoggingInterface().DEBUG,
099: SessionUtilities.getLoggingInterface().NETWORK,
100: "3-Adding " + dsObject.sendingClientKey
101: + "'s object to " + this .getClientKey()
102: + "'s xmt buffer");
103: tdt.addData(dsObject);
104: }
105: }
106:
107: /**
108: * this is the method that actually sends the data
109: */
110: protected void xmitData(DataShareObject dsObject) {
111: if (running) {
112: try {
113: byte[] theseBytes = SessionUtilities
114: .convertObjectToByteArray(dsObject);
115: sndPacket.setData(theseBytes);
116: sndPacket.setLength(theseBytes.length);
117: socket.send(sndPacket);
118: } catch (IOException ioe) {
119: // don't close the DatagramSocket because other connections use it
120: SessionUtilities.getLoggingInterface().debugMsg(
121: SessionUtilities.getLoggingInterface().ERROR,
122: SessionUtilities.getLoggingInterface().NETWORK,
123: "Problems (" + ioe
124: + "), closing this connection: "
125: + keyValue);
126: //ioe.printStackTrace();
127: close();
128: }
129: }
130: }
131:
132: /**
133: * returns the type for this instance
134: */
135: public int getType() {
136: return ChannelDescription.UDP;
137: }
138:
139: /**
140: * called by UdpServer when new data from this client has been received
141: */
142: public void newData(DatagramPacket packet) {
143: if (running) {
144: try {
145: Object object = SessionUtilities.retrieveObject(packet
146: .getData());
147: try {
148: DataShareObject dso = (DataShareObject) object;
149: dri.clientDataReceived(dso, this );
150: } catch (ClassCastException cce) {
151: SessionUtilities
152: .getLoggingInterface()
153: .debugMsg(
154: SessionUtilities
155: .getLoggingInterface().ERROR,
156: SessionUtilities
157: .getLoggingInterface().NETWORK,
158: "Problem: " + cce
159: + " for connection "
160: + keyValue);
161: }
162: } catch (InvalidClassException ice) {
163: SessionUtilities.getLoggingInterface().debugMsg(
164: SessionUtilities.getLoggingInterface().ERROR,
165: SessionUtilities.getLoggingInterface().NETWORK,
166: "Problem: " + ice + " for connection "
167: + keyValue);
168: } catch (EOFException eofe) {
169: SessionUtilities.getLoggingInterface().debugMsg(
170: SessionUtilities.getLoggingInterface().ERROR,
171: SessionUtilities.getLoggingInterface().NETWORK,
172: "Problem: " + eofe + " for connection "
173: + keyValue);
174: } catch (Exception e) {
175: SessionUtilities
176: .getLoggingInterface()
177: .debugMsg(
178: SessionUtilities.getLoggingInterface().ERROR,
179: SessionUtilities.getLoggingInterface().NETWORK,
180: "Problem: " + e + " for connection "
181: + keyValue);
182: //e.printStackTrace();
183: }
184: }
185: }
186:
187: public void close() {
188: if (running) {
189: running = false;
190: server.closeSocket(this);
191: tdt.stopThread();
192: dri.connectionLost(this);
193: }
194: }
195:
196: }
|