SocketManager

open class SocketManager

The SocketManager class is useful to dynamically manage communication by sockets

Author

N7ghtm4r3 - Tecknobit

Since

2.0.2

Inheritors

Constructors

Link copied to clipboard
constructor(host: String, serverPort: Int)
Constructor to init SocketManager
constructor(allowMultipleListeners: Boolean)
Constructor to init SocketManager

Types

Link copied to clipboard
open class Listener
The Listener class is useful to manage dynamically the listeners when the allowMultipleListeners is set to "true", allowing the communication and, eventually, the interruption of the routine of the listener
Link copied to clipboard
StandardResponseCode list of available response status code

Properties

Link copied to clipboard
allowMultipleListeners whether accept multiple listeners at the same time
Link copied to clipboard
protected open var continueSingleRoutine: Boolean
continueSingleRoutine whether continue the routine for the single listener if allowMultipleListeners is set to "false"
Link copied to clipboard
protected val currentHost: String
currentHost current server host used in the communication
Link copied to clipboard
protected open var currentServerPort: Int
currentServerPort current server port used in the communication
Link copied to clipboard
protected open var defaultErrorResponse: String
defaultErrorResponse default error response
Link copied to clipboard
protected open var defaultSuccessResponse: String
defaultSuccessResponse default successful response
Link copied to clipboard
protected val executor: ExecutorService
"executor" of the routines
Link copied to clipboard
protected open var lastContentRed: String
lastContentRed last content red with readContent or readContent
Link copied to clipboard
listeners list of listeners to use if the allowMultipleListeners flag is set to "true"
Link copied to clipboard
protected val NEW_LINE_REPLACER: String = "@-/-/-@"
NEW_LINE_REPLACER is the constants which indicates the replacer to use when the content message contains multiple lines
Link copied to clipboard
protected open var publicHostAddress: String
publicHostAddress public host address
Link copied to clipboard
protected open var serverSocket: ServerSocket
serverSocket socket managed by the server
Link copied to clipboard
protected val serverUse: Boolean
serverUse whether the runtime use of this class is server side or not
Link copied to clipboard
protected open var socket: Socket
socket socket used in the communication

Functions

Link copied to clipboard
open fun acceptRequest(): Socket
Method to accept a new socket request No-any params required
Link copied to clipboard
open fun acceptRequestOn(port: Int): Socket
Method to accept a new socket request
Link copied to clipboard
Method to get allowMultipleListeners instance No-any params required
Link copied to clipboard
Method to stop the communication No-any params required
Link copied to clipboard
Method to get if the listener's workflow needs to continue or not Any param required
Link copied to clipboard
Method to get if the listener's workflow needs to continue or not
Link copied to clipboard
protected open fun exit(errorMessage: String)
Method to print an error occurred and exit with status code 1
Link copied to clipboard
Method to get serverSocket instance No-any params required
Link copied to clipboard
Method to get socket instance No-any params required
Link copied to clipboard
Method to get currentServerPort instance No-any params required
Link copied to clipboard
Method to get executor instance No-any params required
Link copied to clipboard
open fun getHost(publicAddress: Boolean): String
Method to get host where the server is running
Link copied to clipboard
open fun getIpAddress(socket: Socket): String
This method is used to get an ip address from a Socket
Link copied to clipboard
Method to get listeners instance No-any params required
Link copied to clipboard
open fun isServerUse(): Boolean
Method to get serverUse instance No-any params required
Link copied to clipboard
open fun pingHost(timeout: Int): Boolean
open fun pingHost(host: String, port: Int, timeout: Int): Boolean
This method is used to ping a host on a specific port and get whether is reachable
Link copied to clipboard
open fun readContent(): String
Method to read a content message received with the socket request No-any params required
open fun readContent(targetSocket: Socket): String
Method to read a content message received with the socket request
Link copied to clipboard
Method to read the last content message red in the stream with the socket request No-any params required
Link copied to clipboard
Method to send the defaultErrorResponse No-any params required
Link copied to clipboard
open fun sendDefaultErrorResponseTo(host: String, port: Int)
Method to send the defaultErrorResponse
Link copied to clipboard
Method to send the defaultSuccessResponse No-any params required
Link copied to clipboard
open fun sendSuccessResponseTo(port: Int)
open fun sendSuccessResponseTo(host: String, port: Int)
Method to send the defaultSuccessResponse
Link copied to clipboard
open fun <T> setDefaultErrorResponse(defaultErrorResponse: T)
Method to set the default error response to send as error
Link copied to clipboard
open fun <T> setDefaultSuccessResponse(defaultSuccessResponse: T)
Method to set the default successful response to send as success
Link copied to clipboard
open fun startListener(port: Int, routine: Runnable)
Method to start a new listener
Link copied to clipboard
open fun stopAllListeners()
Method to stop the listener's workflow No-any params required
Link copied to clipboard
open fun stopListener()
Method to stop the listener's workflow Any param required

         SocketManager socketManager = new SocketManager(false);
         socketManager.startListener(3218, new Runnable() {
            
            public void run() {
                while (socketManager.continueListening()) {
                    // do listener's workflow
                }
                // listener's workflow when has been stopped
            }
        });

        // your code's workflow

        socketManager.stopListener(); // --> the single listener will be stopped
    
Link copied to clipboard
open fun stopListenerOn(port: Int)
Method to stop the listener's workflow
Link copied to clipboard
open fun toString(): String
Returns a string representation of the object No-any params required
Link copied to clipboard
open fun <T> writeContent(content: T)
Method to write a content message to send with the socket request
Link copied to clipboard
open fun <T> writeContentTo(port: Int, content: T)
open fun <T> writeContentTo(host: String, content: T)
open fun <T> writeContentTo(targetSocket: Socket, content: T)
open fun <T> writeContentTo(host: String, port: Int, content: T)
Method to write a content message to send with the socket request
Link copied to clipboard
open fun <T> writePlainContent(content: T)
Method to write a plain content message to send with the socket request
Link copied to clipboard
open fun <T> writePlainContentTo(port: Int, content: T)
open fun <T> writePlainContentTo(host: String, content: T)
open fun <T> writePlainContentTo(targetSocket: Socket, content: T)
open fun <T> writePlainContentTo(host: String, port: Int, content: T)
Method to write a plain content message to send with the socket request