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: MultiCastSocket.java,v 1.2 2002/01/29 20:50:17 lizellaman Exp $
025: * $Log: MultiCastSocket.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:19 lizellaman
030: * initial sourceforge release
031: *
032: */
033:
034: package org.datashare;
035:
036: import java.net.MulticastSocket;
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 MultiCastSocket extends SocketAdapter {
048: MulticastSocket socket;
049: DatagramPacket sndPacket;
050: MulticastSocketServer server;
051: private TransmitDataThread tdt = null;
052: boolean running = true;
053:
054: public MultiCastSocket(MulticastSocketServer server,
055: MulticastSocket socket, DatagramPacket packet,
056: DataReceiverInterface dri, InetAddress myIpAddress) {
057: this .server = server;
058: this .socket = socket;
059: this .dri = dri;
060: this .localIP = myIpAddress;
061: // note that socket getAddress returns zeros, packet getAddress returns client's real IP, not Multicast IP
062: this .localPort = socket.getLocalPort();
063: this .remoteIP = packet.getAddress();
064: this .remotePort = packet.getPort();
065: this .keyValue = "Socket-Multicast-" + localIP.getHostAddress()
066: + ":" + localPort + "-" + remoteIP.getHostAddress()
067: + ":" + remotePort;
068: SessionUtilities.getLoggingInterface().debugMsg(
069: SessionUtilities.getLoggingInterface().DEBUG,
070: SessionUtilities.getLoggingInterface().NETWORK,
071: "New connection: " + keyValue + " with active set to "
072: + (this .getActive() ? "true" : "false"));
073:
074: // create packet for sending, will overwrite byte[] and length prior to sending
075: sndPacket = new DatagramPacket(new byte[1], 1, remoteIP,
076: remotePort);
077:
078: // create our thread for buffering and transmiting data
079: tdt = new TransmitDataThread(this );
080: tdt.setName("XmitThread for " + keyValue);
081: try {
082: tdt.setPriority(Thread.currentThread().getPriority()
083: + SessionUtilities.SocketXmtRelativePriority); // make transmitting lower priority, since it has buffering
084: } catch (Exception e) {
085: SessionUtilities.getLoggingInterface().debugMsg(
086: SessionUtilities.getLoggingInterface().ERROR,
087: SessionUtilities.getLoggingInterface().NETWORK,
088: "Problems setting Xmt Thread priority:");
089: e.printStackTrace();
090: }
091: tdt.start();
092: }
093:
094: /**
095: * call this method when data is to be sent over this connection
096: */
097: public void sendData(DataShareObject dsObject) {
098: if (running) {
099: SessionUtilities.getLoggingInterface().debugMsg(
100: SessionUtilities.getLoggingInterface().DEBUG,
101: SessionUtilities.getLoggingInterface().NETWORK,
102: "3-Adding " + dsObject.sendingClientKey
103: + "'s object to " + this .getClientKey()
104: + "'s xmt buffer");
105: tdt.addData(dsObject);
106: }
107: }
108:
109: /**
110: * this is the method that actually sends the data
111: */
112: protected void xmitData(DataShareObject dsObject) {
113: if (running) {
114: try {
115: byte[] theseBytes = SessionUtilities
116: .convertObjectToByteArray(dsObject);
117: sndPacket.setData(theseBytes);
118: sndPacket.setLength(theseBytes.length);
119: socket.send(sndPacket);
120: } catch (IOException ioe) {
121: ioe.printStackTrace();
122: // don't close the DatagramSocket because other connections use it
123: SessionUtilities.getLoggingInterface().debugMsg(
124: SessionUtilities.getLoggingInterface().DEBUG,
125: SessionUtilities.getLoggingInterface().NETWORK,
126: "Closing this connection: " + keyValue);
127: close();
128: }
129: }
130: }
131:
132: /**
133: * returns the type for this instance
134: */
135: public int getType() {
136: return ChannelDescription.MULTICAST;
137: }
138:
139: /**
140: * called by MulticastSocketServer when new data from this instance's 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.getMessage()
159: + " for connection "
160: + keyValue);
161: }
162: } catch (ClassNotFoundException cnfe) {
163: SessionUtilities.getLoggingInterface().debugMsg(
164: SessionUtilities.getLoggingInterface().ERROR,
165: SessionUtilities.getLoggingInterface().NETWORK,
166: "Problem: " + cnfe.getMessage()
167: + " for connection " + keyValue);
168: } catch (InvalidClassException ice) {
169: SessionUtilities.getLoggingInterface().debugMsg(
170: SessionUtilities.getLoggingInterface().ERROR,
171: SessionUtilities.getLoggingInterface().NETWORK,
172: "Problem: " + ice.getMessage()
173: + " for connection " + keyValue);
174: } catch (Exception e) {
175: e.printStackTrace();
176: }
177: }
178: }
179:
180: public void close() {
181: if (running) {
182: running = false;
183: server.closeSocket(this ); // doesn't really close our one socket, but removes the Consumer
184: tdt.stopThread();
185: dri.connectionLost(this);
186: }
187: }
188:
189: }
|