Class SocketServer
- All Implemented Interfaces:
Iterable<ComponentType>
,Channel
,ComponentType
,Eligible
,Manager
,NioHandler
The server binds to the given address. If the
address is null, address and port are automatically assigned.
The port may be overwritten by a configuration event
(see onConfigurationUpdate(ConfigurationUpdate)
).
For each established connection, the server creates a new
LinkedIOSubchannel
. The servers basic operation is to
fire Input
(and Closed
) events on the
appropriate subchannel in response to data received from the
network and to handle Output
(and Close
) events
on the subchannel and forward the information to the network
connection.
The server supports limiting the number of concurrent connections
with a PermitsPool
. If such a pool is set as connection
limiter (see setConnectionLimiter(PermitsPool)
), a
permit is acquired for each new connection attempt. If no more
permits are available, the server sends a Purge
event on
each channel that is purgeable for at least the time span
set with setMinimalPurgeableTime(long)
. Purgeability
is derived from the end of record flag of Output
events
(see SocketConnectionManager.onOutput(Output, SocketChannelImpl)
. When using this feature,
make sure that connections are either short lived or the application
level components support the Purge
event. Else, it may become
impossible to establish new connections.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
The Class SocketServerInfo.static interface
The Interface of the SocketServer MXBean.static interface
An MBean interface for getting information about the socket servers and established connections.Nested classes/interfaces inherited from class org.jgrapes.net.SocketConnectionManager
SocketConnectionManager.SocketChannelImpl
Nested classes/interfaces inherited from interface org.jgrapes.core.Channel
Channel.Default
-
Field Summary
Fields inherited from class org.jgrapes.net.SocketConnectionManager
channels
-
Constructor Summary
ConstructorDescriptionCreates a new server, using itself as component channel.SocketServer
(Channel componentChannel) Creates a new server using the given channel. -
Method Summary
Modifier and TypeMethodDescriptionint
backlog()
Return the configured backlog size.Returns the connection limiter.long
Gets the minimal purgeable time.void
handleOps
(int ops) Invoked by theNioDispatcher
when operations are pending.void
Shuts down the server or one of the connections to the server.void
The component can be configured with events that include a path (see @linkConfigurationUpdate.paths()
) that matches this components path (seeManager.componentPath()
).void
Handles the successful channel registration.void
Starts the server.void
Shuts down the server by firing aClose
using the server as channel.protected boolean
Removes the channel from the set of registered channels.Returns the server address.setBacklog
(int backlog) Sets the backlog size.setBufferSize
(int size) Sets the buffer size for the send an receive buffers.setConnectionLimiter
(PermitsPool connectionLimiter) Sets a permit “pool”.setMinimalPurgeableTime
(long millis) Sets a minimal time that a connection must be purgeable (idle) before it may be purged.setServerAddress
(SocketAddress serverAddress) Sets the address to bind to.Methods inherited from class org.jgrapes.net.SocketConnectionManager
bufferSize, executorService, onOutput, setExecutorService, toString
Methods inherited from class org.jgrapes.core.Component
channel, component, defaultCriterion, isEligibleFor, setName
Methods inherited from class org.jgrapes.core.internal.ComponentVertex
activeEventPipeline, addHandler, attach, channelReplacements, children, componentPath, componentVertex, detach, fire, initComponentsHandlers, iterator, name, newEventPipeline, newEventPipeline, parent, registerAsGenerator, root, unregisterAsGenerator
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
SocketServer
public SocketServer()Creates a new server, using itself as component channel. -
SocketServer
Creates a new server using the given channel.- Parameters:
componentChannel
- the component’s channel
-
-
Method Details
-
setServerAddress
Sets the address to bind to.If none is set, the address and port are assigned automatically.
- Parameters:
serverAddress
- the address to bind to- Returns:
- the socket server for easy chaining
-
setBufferSize
Description copied from class:SocketConnectionManager
Sets the buffer size for the send an receive buffers.If no size is set, a default value of 32768 will be used.
- Overrides:
setBufferSize
in classSocketConnectionManager
- Parameters:
size
- the size to use for the send and receive buffers- Returns:
- the socket connection manager for easy chaining
-
onConfigurationUpdate
The component can be configured with events that include a path (see @linkConfigurationUpdate.paths()
) that matches this components path (seeManager.componentPath()
).The following properties are recognized:
hostname
- If given, is used as first parameter for
InetSocketAddress(String, int)
. port
- If given, is used as parameter for
InetSocketAddress(String, int)
orInetSocketAddress(int)
, depending on whether a host name is specified. Defaults to “0”. backlog
- See
setBacklog(int)
. bufferSize
- See
setBufferSize(int)
. maxConnections
- Calls
setConnectionLimiter(org.jgrapes.io.util.PermitsPool)
with aPermitsPool
of the specified size. minimalPurgeableTime
- See
setMinimalPurgeableTime(long)
.
- Parameters:
event
- the event
-
serverAddress
Returns the server address.Before starting, the address is the address set with
setServerAddress(InetSocketAddress)
. After starting the address is obtained from the created socket.- Returns:
- the serverAddress
-
setBacklog
Sets the backlog size.- Parameters:
backlog
- the backlog to set- Returns:
- the socket server for easy chaining
-
backlog
Return the configured backlog size.- Returns:
- the backlog
-
setConnectionLimiter
Sets a permit “pool”.A new connection is created only if a permit can be obtained from the pool.
A connection limiter must be set before starting the component.
- Parameters:
connectionLimiter
- the connection pool to set- Returns:
- the socket server for easy chaining
-
getConnectionLimiter
Returns the connection limiter.- Returns:
- the connection Limiter
-
setMinimalPurgeableTime
Sets a minimal time that a connection must be purgeable (idle) before it may be purged.- Parameters:
millis
- the millis- Returns:
- the socket server
-
getMinimalPurgeableTime
Gets the minimal purgeable time.- Returns:
- the minimal purgeable time
-
onStart
Starts the server.- Parameters:
event
- the start event- Throws:
IOException
- if an I/O exception occurred
-
onRegistered
@Handler(channels=Self.class) public void onRegistered(NioRegistration.Completed event) throws InterruptedException, IOException Handles the successful channel registration.- Parameters:
event
- the event- Throws:
InterruptedException
- the interrupted exceptionIOException
- Signals that an I/O exception has occurred.
-
handleOps
Description copied from interface:NioHandler
Invoked by theNioDispatcher
when operations are pending.- Specified by:
handleOps
in interfaceNioHandler
- Parameters:
ops
- the pending operations as or’ed constants defined bySelectionKey
-
removeChannel
Description copied from class:SocketConnectionManager
Removes the channel from the set of registered channels.- Overrides:
removeChannel
in classSocketConnectionManager
- Parameters:
channel
- the channel- Returns:
- true, if channel was registered
-
onClose
Shuts down the server or one of the connections to the server.- Parameters:
event
- the event- Throws:
IOException
- if an I/O exception occurredInterruptedException
- if the execution was interrupted
-
onStop
Shuts down the server by firing aClose
using the server as channel.Note that this automatically results in closing all open connections by the runtime system and thus in
Closed
events on all subchannels.- Parameters:
event
- the event- Throws:
InterruptedException
-