Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Nofify Node of listening port once server initialised.
- Loading branch information
Showing
7 changed files
with
184 additions
and
51 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/uk/co/pekim/nodejdbc/notifynode/NotifyInitialised.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* | ||
*/ | ||
package uk.co.pekim.nodejdbc.notifynode; | ||
|
||
/** | ||
* Message to Node ifnormaing that we've initialised. | ||
* | ||
* @author Mike D Pilsbury | ||
*/ | ||
public class NotifyInitialised { | ||
private int port; | ||
|
||
/** | ||
* Create an initialisation message. | ||
* | ||
* @param port | ||
* the port. | ||
*/ | ||
public NotifyInitialised(final int port) { | ||
this.port = port; | ||
} | ||
|
||
/** | ||
* The port. | ||
* | ||
* @return the port | ||
*/ | ||
public int getPort() { | ||
return port; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/uk/co/pekim/nodejdbc/notifynode/NotifyNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* | ||
*/ | ||
package uk.co.pekim.nodejdbc.notifynode; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStreamWriter; | ||
import java.net.InetAddress; | ||
import java.net.Socket; | ||
import java.net.UnknownHostException; | ||
|
||
import org.codehaus.jackson.JsonGenerationException; | ||
import org.codehaus.jackson.map.JsonMappingException; | ||
import org.codehaus.jackson.map.ObjectMapper; | ||
|
||
import uk.co.pekim.nodejdbc.NodeJdbcException; | ||
import uk.co.pekim.nodejdbc.netstring.Netstring; | ||
|
||
/** | ||
* Notify the parent Node process of various events. | ||
* | ||
* @author Mike D Pilsbury | ||
*/ | ||
public class NotifyNode { | ||
private static final ObjectMapper JSON_MAPPER = new ObjectMapper(); | ||
|
||
private OutputStreamWriter output; | ||
|
||
/** | ||
* Create a Node notifier. | ||
* | ||
* @param port | ||
* the port that a Node instance is listening on. | ||
*/ | ||
public NotifyNode(final int port) { | ||
try { | ||
final Socket socket = new Socket(InetAddress.getLocalHost(), port); | ||
output = new OutputStreamWriter(socket.getOutputStream()); | ||
} catch (UnknownHostException exception) { | ||
throw new NodeJdbcException("Failed to connect to node", exception); | ||
} catch (IOException exception) { | ||
throw new NodeJdbcException("Failed to connect to node", exception); | ||
} | ||
} | ||
|
||
/** | ||
* Send a message to the Node instance. The message is send as a JSON | ||
* representation of the message, wrapped in a netstring. | ||
* | ||
* @param message | ||
* the message to send. | ||
*/ | ||
public void send(final Object message) { | ||
try { | ||
final String jsonString = JSON_MAPPER.writeValueAsString(message); | ||
final String netString = Netstring.build(jsonString); | ||
|
||
output.write(netString); | ||
output.flush(); | ||
} catch (JsonGenerationException exception) { | ||
throw new NodeJdbcException("Failed to create JSON from " + message, exception); | ||
} catch (JsonMappingException exception) { | ||
throw new NodeJdbcException("Failed to create JSON from " + message, exception); | ||
} catch (IOException exception) { | ||
throw new NodeJdbcException("Failed to send JSON for " + message, exception); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters