|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectedu.nps.moves.disutil.UdpServer
public class UdpServer
Used in support of PduMulticastReceiver
.
A robust class for establishing a UDP server and manipulating
its listening port and optionally a multicast groups to join.
The UdpServer.Event
s and property change events make
it an appropriate tool in a threaded, GUI application.
It is almost identical in design to the TcpServer class that
should have accompanied this class when you downloaded it.
To start a UDP server, create a new UdpServer and call start():
UdpServer server = new UdpServer(); server.start();
Of course it won't be much help unless you know which port it's listening on and you register as a listener so you'll know when a java.net.DatagramPacket has come in:
server.setPort(1234); server.addUdpServerListener( new UdpServer.Adapter(){ public void udpServerPacketReceived( UdpServer.Event evt ){ DatagramPacket packet = evt.getPacket(); ... } // end packet received });
The server runs on one thread, and all events are fired on that thread.
If you have to offload heavy processing to another thread, be sure to
make a copy of the datagram data array since it will be reused the next
time around. You may use the UdpServer.Event.getPacketAsBytes()
command as a convenient way to make a copy of the byte array.
The full 64KB allowed by the UDP standard is set aside to receive the datagrams, but it's possible that your host platform may truncate that.
The public methods are all synchronized on this, and great care has been taken to avoid deadlocks and race conditions. That being said, there may still be bugs (please contact the author if you find any), and you certainly still have the power to introduce these problems yourself.
It's often handy to have your own class extend this one rather than making an instance field to hold a UdpServer where you'd have to pass along all the setPort(...) methods and so forth.
The supporting UdpServer.Event
, UdpServer.Listener
, and UdpServer.Adapter
classes are static inner classes in this file so that you have only one
file to copy to your project. You're welcome.
This code is released into the Public Domain. Since this is Public Domain, you don't need to worry about licensing, and you can simply copy this UdpServer.java file to your own package and use it as you like. Enjoy. Please consider leaving the following statement here in this code:
This UdpServer class was copied to this project from its source as found at iHarder.net.
UdpServer
,
UdpServer.Adapter
,
UdpServer.Event
,
UdpServer.Listener
Nested Class Summary | |
---|---|
class |
UdpServer.Adapter
A helper class that implements all methods of the UdpServer.Listener interface with empty methods. |
static class |
UdpServer.Event
An event representing activity by a UdpServer . |
static interface |
UdpServer.Listener
An interface for listening to events from a UdpServer . |
static class |
UdpServer.State
One of four possible states for the server to be in: |
Field Summary | |
---|---|
static java.lang.String |
GROUPS_PROP
The multicast groups property groups used with the property change listeners and the preferences, if a preferences object is given. |
static java.lang.String |
PORT_PROP
The port property port used with the property change listeners and the preferences, if a preferences object is given. |
Constructor Summary | |
---|---|
UdpServer()
Constructs a new UdpServer that will listen on the default port 8000 (but not until start() is called). |
|
UdpServer(int port)
Constructs a new UdpServer that will listen on the given port (but not until start() is called). |
|
UdpServer(int port,
java.util.concurrent.ThreadFactory factory)
Constructs a new UdpServer that will listen on the given port (but not until start() is called). |
Method Summary | |
---|---|
void |
addPropertyChangeListener(java.beans.PropertyChangeListener listener)
Add a property listener. |
void |
addPropertyChangeListener(java.lang.String property,
java.beans.PropertyChangeListener listener)
Add a property listener for the named property. |
void |
addUdpServerListener(UdpServer.Listener l)
Adds a UdpServer.Listener . |
void |
fireProperties()
Fires property chagne events for all current values setting the old value to null and new value to the current. |
protected void |
firePropertyChange(java.lang.String prop,
java.lang.Object oldVal,
java.lang.Object newVal)
Fire a property change event on the current thread. |
void |
fireState()
Fires an event declaring the current state of the server. |
protected void |
fireUdpServerPacketReceived()
Fires event on calling thread. |
protected void |
fireUdpServerStateChanged()
Fires event on calling thread. |
java.lang.String |
getGroups()
Returns the multicast groups to which the server has joined. |
static java.util.logging.Level |
getLoggingLevel()
Static method returning the logging level using Java's java.util.logging package. |
java.net.DatagramPacket |
getPacket()
Returns the last DatagramPacket received. |
int |
getPort()
Returns the port on which the server is or will be listening. |
int |
getReceiveBufferSize()
Returns the receive buffer for the underlying MulticastSocket if the server is currently running (otherwise there is no MulticastSocket to query). |
UdpServer.State |
getState()
Returns the current state of the server, one of STOPPED, STARTING, or STARTED. |
protected void |
recordState(UdpServer.State state)
Records (sets) the state and fires an event. |
void |
removePropertyChangeListener(java.beans.PropertyChangeListener listener)
Remove a property listener. |
void |
removePropertyChangeListener(java.lang.String property,
java.beans.PropertyChangeListener listener)
Remove a property listener for the named property. |
void |
removeUdpServerListener(UdpServer.Listener l)
Removes a UdpServer.Listener . |
void |
reset()
Resets the server, if it is running, otherwise does nothing. |
protected void |
runServer()
This method starts up and listens indefinitely for UDP packets. |
void |
setGroups(java.lang.String group)
Sets the new multicast groups to which the server will join. |
static void |
setLoggingLevel(java.util.logging.Level level)
Static method to set the logging level using Java's java.util.logging package. |
void |
setPort(int port)
Sets the new port on which the server will attempt to listen. |
void |
setReceiveBufferSize(int size)
Recommends a receive buffer size for the underlying MulticastSocket. |
void |
start()
Attempts to start the server listening and returns immediately. |
void |
stop()
Attempts to stop the server, if the server is in the STARTED state, and returns immediately. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final java.lang.String PORT_PROP
public static final java.lang.String GROUPS_PROP
Constructor Detail |
---|
public UdpServer()
start()
is called).
The I/O thread will not be in daemon mode.
public UdpServer(int port)
start()
is called).
The I/O thread will not be in daemon mode.
port
- The initial port on which to listenpublic UdpServer(int port, java.util.concurrent.ThreadFactory factory)
start()
is called). The provided
ThreadFactory will be used when starting and running the server.
port
- The initial port on which to listenfactory
- The thread factory used to generate a thread to run the serverMethod Detail |
---|
public void start()
UdpServer.Listener
public void stop()
UdpServer.Listener
public UdpServer.State getState()
protected void recordState(UdpServer.State state)
state
- The new state of the serverpublic void fireState()
public void reset()
protected void runServer()
public java.net.DatagramPacket getPacket()
public int getReceiveBufferSize() throws java.net.SocketException
java.net.SocketException
public void setReceiveBufferSize(int size) throws java.net.SocketException
size
-
java.net.SocketException
public int getPort()
public void setPort(int port)
port
- the new port for listening
java.lang.IllegalArgumentException
- if port is outside 0..65535public java.lang.String getGroups()
public void setGroups(java.lang.String group)
Sets the new multicast groups to which the server will join. If the server is already listening, then it will attempt to restart, generating start and stop events.
The list of groups may be whitespace- and/or comma-separated. When the server starts up (or restarts), the list will be parsed, and only legitimate groups will actually be joined.
May be null.
group
- the new groups to joinpublic void addUdpServerListener(UdpServer.Listener l)
UdpServer.Listener
.
l
- the UdpServer.Listenerpublic void removeUdpServerListener(UdpServer.Listener l)
UdpServer.Listener
.
l
- the UdpServer.Listenerprotected void fireUdpServerPacketReceived()
protected void fireUdpServerStateChanged()
public void fireProperties()
protected void firePropertyChange(java.lang.String prop, java.lang.Object oldVal, java.lang.Object newVal)
prop
- name of propertyoldVal
- old valuenewVal
- new valuepublic void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
listener
- the property change listenerpublic void addPropertyChangeListener(java.lang.String property, java.beans.PropertyChangeListener listener)
property
- the sole property name for which to registerlistener
- the property change listenerpublic void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
listener
- the property change listenerpublic void removePropertyChangeListener(java.lang.String property, java.beans.PropertyChangeListener listener)
property
- the sole property name for which to stop receiving eventslistener
- the property change listenerpublic static void setLoggingLevel(java.util.logging.Level level)
UdpServer.setLoggingLevel(Level.OFF);
.
level
- the new logging levelpublic static java.util.logging.Level getLoggingLevel()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |