Source Code Cross Referenced for UdpSocket.java in  » Groupware » Data-share » org » datashare » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Groupware » Data share » org.datashare 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.