SK.gnome.dwarf.tcpip
Class UDPListener

java.lang.Object
  extended bySK.gnome.dwarf.GenericService
      extended bySK.gnome.dwarf.thread.ThreadService
          extended bySK.gnome.dwarf.tcpip.Listener
              extended bySK.gnome.dwarf.tcpip.UDPListener
All Implemented Interfaces:
java.lang.Runnable, Service
Direct Known Subclasses:
MulticastUDPListener

public class UDPListener
extends Listener

Service for receiving client datagrams.

It listenes on a specified port and address and passes the received datagrams to the TCPIPServer for further processing.


Field Summary
protected  java.net.InetAddress connectAddress
          The internet address to connect to.
protected  int connectPort
          The network port to connect to.
protected  int packetSize
          The size of the datagram packet in bytes.
protected  java.net.DatagramSocket socket
          The datagram socket.
 
Fields inherited from class SK.gnome.dwarf.tcpip.Listener
address, allowedHosts, deniedHosts, port
 
Fields inherited from class SK.gnome.dwarf.thread.ThreadService
daemon, thread
 
Fields inherited from class SK.gnome.dwarf.GenericService
initParameters, parent
 
Fields inherited from interface SK.gnome.dwarf.Service
INITIALIZED, LOG_DEBUG, LOG_ERROR, LOG_FATAL, LOG_INFO, LOG_TRACE, LOG_WARN, LOG_XFER, SHUTDOWN, STARTED, STOPPED
 
Constructor Summary
UDPListener(java.lang.String name)
          Creates a new UDPListener.
 
Method Summary
protected  boolean enableThread()
          Enables or disables the service thread.
protected  void finish()
          Waits for the thread to complete.
protected  java.net.DatagramPacket getDatagramPacket()
          Returns the datagram packet.
protected  java.net.DatagramSocket getDatagramSocket()
          Returns the datagram socket.
 void init(Server parent)
          Initializes the service.
protected  void loop()
          Performs the listening operation.
 void setConnectAddress(java.net.InetAddress address)
          Sets the internet address to connect to.
 void setConnectPort(int port)
          Sets the port to connect to.
 void setPacketSize(int size)
          Sets the datagram packet size.
 
Methods inherited from class SK.gnome.dwarf.tcpip.Listener
acceptHost, setAddress, setAllowedHosts, setDeniedHosts, setPort
 
Methods inherited from class SK.gnome.dwarf.thread.ThreadService
run, setDaemon, shutdown, start, stop
 
Methods inherited from class SK.gnome.dwarf.GenericService
getAuthenticator, getAuthFacility, getFullName, getInitParameter, getInitParameterNames, getLogFacility, getLogger, getName, getParameters, getPrincipal, getShutdownTimeout, getState, log, log, login, logout, setAuthFacility, setInitParameters, setLogFacility, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

connectAddress

protected java.net.InetAddress connectAddress
The internet address to connect to.

See Also:
setConnectAddress(InetAddress)

connectPort

protected int connectPort
The network port to connect to.

See Also:
setConnectPort(int)

packetSize

protected int packetSize
The size of the datagram packet in bytes.

Default value: TCPIPConstants.TCPIP_DEFAULT_UDP_PACKET_SIZE

See Also:
setPacketSize(int)

socket

protected java.net.DatagramSocket socket
The datagram socket.

See Also:
init(Server)
Constructor Detail

UDPListener

public UDPListener(java.lang.String name)
Creates a new UDPListener.

Method Detail

setConnectAddress

public void setConnectAddress(java.net.InetAddress address)
Sets the internet address to connect to.

Parameters:
address - the internet address
See Also:
Listener.address

setConnectPort

public void setConnectPort(int port)
Sets the port to connect to.

Parameters:
port - the port
See Also:
Listener.port

setPacketSize

public void setPacketSize(int size)
Sets the datagram packet size.

Parameters:
size - the packet size in bytes
Throws:
java.lang.IllegalArgumentException - if the size exceeds the TCPIPConstants.TCPIP_MAX_UDP_PACKET_SIZE limit
See Also:
packetSize

init

public void init(Server parent)
          throws ServiceException
Initializes the service.

Sets the socket field to value returned by the getDatagramSocket() method.

This method requires that the parent server is an instance of the TCPIPServer class.

Specified by:
init in interface Service
Overrides:
init in class GenericService
Throws:
ServiceException

finish

protected void finish()
Waits for the thread to complete.

It closes the socket, which causes an interruption of the blocked listening thread, so it can finish gracefully.

Overrides:
finish in class ThreadService

enableThread

protected boolean enableThread()
Enables or disables the service thread.

This method always returns true, so the new listening thread is always started.

Overrides:
enableThread in class ThreadService
Returns:
true if the thread is enabled

loop

protected void loop()
Performs the listening operation.

Listenes on a specified port and address via the socket. The datagram packet for receiving client data is obtained from the getDatagramPacket() method. If a new client datagram is detected, the listener first checks whether the datagram should be accepted by invoking the inherited Listener.acceptHost(InetAddress) method with the address of the received datagram. If the result is true, it passes the datagram to the parent server by calling its TCPIPServer.enqueue(Object) method. In the case the listener will not accept the datagram, it is discarded and a warning message is written to the log.

Overrides:
loop in class ThreadService

getDatagramSocket

protected java.net.DatagramSocket getDatagramSocket()
                                             throws ServiceException
Returns the datagram socket.

Returns the socket for listening. The returned socket will be bound to the Listener.port. If the Listener.address field is null, the socket will use all available addresses. If the connectAddress and connectPort is not null, the socket will be connected to the given address and port before returned from this method.

Throws:
ServiceException

getDatagramPacket

protected java.net.DatagramPacket getDatagramPacket()
Returns the datagram packet.

Returns the datagram packet for receiving client data. The packet size must not exceed the TCPIPConstants.TCPIP_MAX_UDP_PACKET_SIZE limit. This method returns a new java.net.DatagramPacket object with size equal to the packetSize. It can be overriden to implement the packet caching/reusing for a high-performance streaming servers, for example.



Copyright (c) 1999-2005, Gnome Ltd. All rights reserved.