Package com.tailf.conf
Class SocketFactory
Object
com.tailf.conf.SocketFactory
Class for creation and control of sockets, towards NCS/Confd
This singleton factory class control all socket connections.
It is possible to register a socket factory callback. If such a factory is
registered all socket connect calls will be dispatched to this callback.
The callback is registered with the SocketFactory.registerCallback(...)
method. For Confd this is the preferred way.
In NCS which connects its control sockets before any package is instantiated
there exist instead a system property TAILF_SOCKET_FACTORY_CB which should
be set to the callback class e.g like:
java -DTAILF_SOCKET_FACTORY_CB=com.example.myfactory ...
This class must exist in the classpath so that it can be found by NcsMain.
-
Method Summary
Modifier and TypeMethodDescriptionstatic SocketRetrieve a socket which is connected to the default host/port.static SocketRetrieve a connected socket to a specified destination If the default host/port is not set, it will be set to the same as the given socket.static SocketgetSocket(Object caller, InetAddress iaddr, int port) Retrieve a connected socket to a specified destination If the default host/port is not set, it will be set to the same as the given socket.static SocketRetrieve a connected socket using a connected socket as basis.static SocketFactoryCallbackRetrieve the SocketFactoryCallback.static SocketgetUnconnectedSocket(Object caller) Retrieve an unconnected socket.static voidRegister a SocketFactoryCallback that will be responsible for all socket connectionstatic voidsetHostname(String hostname) Set the default hostname for socket connections.static voidsetPort(int port) Set the default port number for socket connections
-
Method Details
-
setHostname
Set the default hostname for socket connections.- Parameters:
hostname-
-
setPort
public static void setPort(int port) Set the default port number for socket connections- Parameters:
port-
-
getUnconnectedSocket
Retrieve an unconnected socket. Such socket can be used when e.g a bind() call is necessary before the connect() is performed. The connect() call is performed by the requestor.- Parameters:
caller- The requestor object instance- Returns:
- An unconnected socket
- Throws:
IOExceptionConfException
-
getSocket
Retrieve a socket which is connected to the default host/port.- Parameters:
caller- The requestor object instance- Returns:
- a socket connected to the default host/port.
- Throws:
IOExceptionConfException
-
getSocket
Retrieve a connected socket using a connected socket as basis. The default implementation of this method will just return the given socket in the argument. If the default host/port is not set, it will be set to the same as the given socket.- Parameters:
caller- The requestor object instances-- Returns:
- the resulting socket
-
getSocket
public static Socket getSocket(Object caller, String hostname, int port) throws IOException, ConfException Retrieve a connected socket to a specified destination If the default host/port is not set, it will be set to the same as the given socket.- Parameters:
caller- The requestor object instancehostname- The preferred hostport- The preferred port- Returns:
- the socket connected to hostname, port
- Throws:
IOExceptionConfException
-
getSocket
public static Socket getSocket(Object caller, InetAddress iaddr, int port) throws IOException, ConfException Retrieve a connected socket to a specified destination If the default host/port is not set, it will be set to the same as the given socket.- Parameters:
caller- The requestor object instanceiaddr- The preferred addressport- The preferred port- Returns:
- the socket connection to iaddr, port
- Throws:
IOExceptionConfException
-
registerCallback
Register a SocketFactoryCallback that will be responsible for all socket connection- Parameters:
cb- SocketFactoryCallback instance
-
getSocketFactoryCb
Retrieve the SocketFactoryCallback. This always exists, if the user have not registered a callback this method will return a DefaultSocketFactoryCb instance- Returns:
- the current SocketFactoryCallback
-