public class StartupSocketCommunicator extends Object
To use:
StartupSocketCommunicator(String, int, ContactHandler, Serializable)
.isServerMode()
.ContactHandler.handleContactFromOtherInstance(Serializable)
.
If you are in client mode (not first instance of the application), you have to check if the communication was successful (getException()
is null) and
then get the result that was returned from the server by calling getClientResult()
.
StartupSocketCommunicator(String, int, ContactHandler, Serializable)
for further details.
This implementation is meant to be thread safe and may be used from any thread.
Constructor and Description |
---|
StartupSocketCommunicator(String pApplicationId,
int pPort,
ContactHandler pContactHandler,
Serializable pData)
Initiates the communication for this application instance.
|
Modifier and Type | Method and Description |
---|---|
Serializable |
getClientResult() |
Throwable |
getException() |
boolean |
isServerMode() |
boolean |
isServerStopped() |
void |
stopServer()
stops the server and frees the port.
|
void |
waitServerStopped()
stops the server, frees the port and waits until that happens.
|
public StartupSocketCommunicator(String pApplicationId, int pPort, ContactHandler pContactHandler, Serializable pData)
First, we attempt to register a server socket on the given port. If the socket is successfully registered, we are the first instance of the application and act as server to other instances. Otherwise we are an additional instance of the application and act as client.
The following situations may happen:
isServerMode()
is true)ContactHandler
. The calls will happen in arbitrary threads.
If an exception in server mode occurs, you can access it with the getException()
.
If that value is null in server mode, then the server is running (or ran) without problem.
If you want to stop the server explicitly, call stopServer()
. Otherwise the server will be stopped
when the shutdown handlers are executed (see Runtime.addShutdownHook(Thread)
).
isServerMode()
is true)
If an exception in client mode occurs, you can access it with the getException()
- it will not be thrown!.
If that value is null in client mode, then the request ran without problems and we have a result
stored in getClientResult()
.
Note that the various threads started by the StartupSocketCommunicator
are all daemon threads, i.e. they
will not stop the application from exiting, even if a client is already connected.
The client will notice that by an exception in its communication attempts.
pApplicationId
- an id that is compared when two instances create contact.
If the application id does not match, we assume that a different application that uses the same technique
is configured to use the same port.pPort
- the port to use for communicationpContactHandler
- the contact handler that is called by other instances, must not be null.
This handler is only used when this application instance is the one that
gets the port and thus acts as a server. Otherwise it is ignored.pData
- the data to pass to the first application instance if we are not the first.
This may be any Serializable
but you should certain that
either both application instances are using the same code base or that the objects have
custom serialization mechanisms that allow communication across different versions.public boolean isServerMode()
StartupSocketCommunicator(String, int, ContactHandler, Serializable)
for detailspublic boolean isServerStopped()
isServerMode()
is truepublic Throwable getException()
public Serializable getClientResult()
ContactHandler.handleContactFromOtherInstance(Serializable)
with our data (only meaningfull if isServerMode()
is false).public void stopServer()
public void waitServerStopped() throws InterruptedException
InterruptedException
- if we get interrupted during sleepCopyright © 2017 Jürgen Zeller (privat). All rights reserved.