Added charset for communication
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
parent
5280ee98bd
commit
c2fa0f2c0b
@ -98,7 +98,7 @@ of Emmanuel Bigeon. -->
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>fr.bigeon</groupId>
|
<groupId>fr.bigeon</groupId>
|
||||||
<artifactId>ebigeon-config</artifactId>
|
<artifactId>ebigeon-config</artifactId>
|
||||||
<version>1.7.0</version>
|
<version>1.7.1</version>
|
||||||
</parent>
|
</parent>
|
||||||
<name>GCLC Socket</name>
|
<name>GCLC Socket</name>
|
||||||
<description>Socket implementation of GCLC</description>
|
<description>Socket implementation of GCLC</description>
|
||||||
|
@ -45,6 +45,7 @@ import java.io.PrintWriter;
|
|||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
@ -108,19 +109,23 @@ public class SocketConsoleApplicationShell implements Runnable {
|
|||||||
private ServerSocket serverSocket;
|
private ServerSocket serverSocket;
|
||||||
/** The application shutdown string */
|
/** The application shutdown string */
|
||||||
private final String applicationShutdown;
|
private final String applicationShutdown;
|
||||||
|
/** The charset for the communication. */
|
||||||
|
private Charset charset;
|
||||||
|
|
||||||
/** Create a socket application shell which will listen on the given port
|
/** Create a socket application shell which will listen on the given port
|
||||||
* and close session upon the provided string reception by client
|
* and close session upon the provided string reception by client
|
||||||
*
|
*
|
||||||
* @param port the port to listen to
|
* @param port the port to listen to
|
||||||
* @param close the session closing command
|
* @param close the session closing command
|
||||||
* @param applicationShutdown the appication shut down command */
|
* @param applicationShutdown the appication shut down command
|
||||||
|
* @param charset the charset for communication */
|
||||||
public SocketConsoleApplicationShell(int port, String close,
|
public SocketConsoleApplicationShell(int port, String close,
|
||||||
String applicationShutdown) {
|
String applicationShutdown, Charset charset) {
|
||||||
this.port = port;
|
this.port = port;
|
||||||
this.close = close;
|
this.close = close;
|
||||||
this.applicationShutdown = applicationShutdown;
|
this.applicationShutdown = applicationShutdown;
|
||||||
this.autoClose = false;
|
this.autoClose = false;
|
||||||
|
this.charset = charset;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Create a socket application shell which will listen on the given port
|
/** Create a socket application shell which will listen on the given port
|
||||||
@ -129,13 +134,15 @@ public class SocketConsoleApplicationShell implements Runnable {
|
|||||||
* @param port the port to listen to
|
* @param port the port to listen to
|
||||||
* @param autoClose if the session must be closed once the request has been
|
* @param autoClose if the session must be closed once the request has been
|
||||||
* sent
|
* sent
|
||||||
* @param applicationShutdown the application shutdown command */
|
* @param applicationShutdown the appication shut down command
|
||||||
|
* @param charset the charset for communication */
|
||||||
public SocketConsoleApplicationShell(int port, boolean autoClose,
|
public SocketConsoleApplicationShell(int port, boolean autoClose,
|
||||||
String applicationShutdown) {
|
String applicationShutdown, Charset charset) {
|
||||||
this.port = port;
|
this.port = port;
|
||||||
this.autoClose = autoClose;
|
this.autoClose = autoClose;
|
||||||
this.applicationShutdown = applicationShutdown;
|
this.applicationShutdown = applicationShutdown;
|
||||||
this.close = autoClose ? null : "close"; //$NON-NLS-1$
|
this.close = autoClose ? null : "close"; //$NON-NLS-1$
|
||||||
|
this.charset = charset;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
@ -149,8 +156,10 @@ public class SocketConsoleApplicationShell implements Runnable {
|
|||||||
// Create the streams
|
// Create the streams
|
||||||
try (PipedOutputStream outStream = new PipedOutputStream();
|
try (PipedOutputStream outStream = new PipedOutputStream();
|
||||||
BufferedWriter writer = new BufferedWriter(
|
BufferedWriter writer = new BufferedWriter(
|
||||||
new OutputStreamWriter(outStream));
|
new OutputStreamWriter(outStream,
|
||||||
InputStreamReader isr = new InputStreamReader(consoleInput);
|
charset));
|
||||||
|
InputStreamReader isr = new InputStreamReader(consoleInput,
|
||||||
|
charset);
|
||||||
BufferedReader inBuf = new BufferedReader(isr)) {
|
BufferedReader inBuf = new BufferedReader(isr)) {
|
||||||
consoleInput.connect(outStream);
|
consoleInput.connect(outStream);
|
||||||
consoleManager.setInput(inBuf);
|
consoleManager.setInput(inBuf);
|
||||||
@ -180,9 +189,12 @@ public class SocketConsoleApplicationShell implements Runnable {
|
|||||||
while (running) {
|
while (running) {
|
||||||
try (Socket clientSocket = serverSocket.accept();
|
try (Socket clientSocket = serverSocket.accept();
|
||||||
PrintWriter out = new PrintWriter(
|
PrintWriter out = new PrintWriter(
|
||||||
clientSocket.getOutputStream(), true);
|
new OutputStreamWriter(clientSocket.getOutputStream(),
|
||||||
|
charset),
|
||||||
|
true);
|
||||||
InputStreamReader isr = new InputStreamReader(
|
InputStreamReader isr = new InputStreamReader(
|
||||||
clientSocket.getInputStream());
|
clientSocket.getInputStream(),
|
||||||
|
charset);
|
||||||
BufferedReader in = new BufferedReader(isr);) {
|
BufferedReader in = new BufferedReader(isr);) {
|
||||||
// this is not threaded to avoid several clients at the same
|
// this is not threaded to avoid several clients at the same
|
||||||
// time
|
// time
|
||||||
|
@ -57,8 +57,10 @@ public class ThreadedServerConsoleManager implements ConsoleManager {
|
|||||||
/** The class logger */
|
/** The class logger */
|
||||||
private static final Logger LOGGER = Logger
|
private static final Logger LOGGER = Logger
|
||||||
.getLogger(ThreadedServerConsoleManager.class.getName());
|
.getLogger(ThreadedServerConsoleManager.class.getName());
|
||||||
|
/** The empty string constant */
|
||||||
|
private static final String EMPTY = ""; //$NON-NLS-1$
|
||||||
/** The prompting sequence */
|
/** The prompting sequence */
|
||||||
private String prompt = new String();
|
private String prompt = EMPTY;
|
||||||
/** The buffer of data to send to the user */
|
/** The buffer of data to send to the user */
|
||||||
private StringBuilder buffer = new StringBuilder();
|
private StringBuilder buffer = new StringBuilder();
|
||||||
/** The synchronized object */
|
/** The synchronized object */
|
||||||
@ -105,7 +107,7 @@ public class ThreadedServerConsoleManager implements ConsoleManager {
|
|||||||
@Override
|
@Override
|
||||||
public String prompt(String message) {
|
public String prompt(String message) {
|
||||||
buffer.append(message);
|
buffer.append(message);
|
||||||
String userInput = new String();
|
String userInput = EMPTY;
|
||||||
boolean prompting = true;
|
boolean prompting = true;
|
||||||
while (prompting) {
|
while (prompting) {
|
||||||
// Send buffer content
|
// Send buffer content
|
||||||
|
@ -34,6 +34,8 @@
|
|||||||
*/
|
*/
|
||||||
package fr.bigeon.gclc.socket;
|
package fr.bigeon.gclc.socket;
|
||||||
|
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
/** A test server
|
/** A test server
|
||||||
*
|
*
|
||||||
* @author Emmanuel Bigeon */
|
* @author Emmanuel Bigeon */
|
||||||
@ -63,7 +65,7 @@ public class TestServer {
|
|||||||
private static SocketConsoleApplicationShell getShell() {
|
private static SocketConsoleApplicationShell getShell() {
|
||||||
if (SHELL == null) {
|
if (SHELL == null) {
|
||||||
SHELL = new SocketConsoleApplicationShell(3300, "close",
|
SHELL = new SocketConsoleApplicationShell(3300, "close",
|
||||||
ConsoleTestApplication.EXIT);
|
ConsoleTestApplication.EXIT, Charset.forName("UTF-8"));
|
||||||
final ConsoleTestApplication app = new ConsoleTestApplication(
|
final ConsoleTestApplication app = new ConsoleTestApplication(
|
||||||
SHELL.getConsoleManager());
|
SHELL.getConsoleManager());
|
||||||
SHELL.setApplication(app);
|
SHELL.setApplication(app);
|
||||||
@ -74,7 +76,7 @@ public class TestServer {
|
|||||||
public static Thread startServer(boolean autoClose) {
|
public static Thread startServer(boolean autoClose) {
|
||||||
if (SHELL == null) {
|
if (SHELL == null) {
|
||||||
SHELL = new SocketConsoleApplicationShell(3300, autoClose,
|
SHELL = new SocketConsoleApplicationShell(3300, autoClose,
|
||||||
ConsoleTestApplication.EXIT);
|
ConsoleTestApplication.EXIT, Charset.forName("UTF-8"));
|
||||||
final ConsoleTestApplication app = new ConsoleTestApplication(
|
final ConsoleTestApplication app = new ConsoleTestApplication(
|
||||||
SHELL.getConsoleManager());
|
SHELL.getConsoleManager());
|
||||||
SHELL.setApplication(app);
|
SHELL.setApplication(app);
|
||||||
|
Loading…
Reference in New Issue
Block a user