Adding swt and socket frontends

This commit is contained in:
2016-05-24 14:55:45 -04:00
parent 6f21c4fe0d
commit 87a668d308
15 changed files with 1320 additions and 18 deletions

View File

@@ -0,0 +1,278 @@
/*
* Copyright E. Bigeon (2014)
*
* emmanuel@bigeon.fr
*
* This software is a computer program whose purpose is to
* Socket implementation of GCLC.
*
* This software is governed by the CeCILL license under French law and
* abiding by the rules of distribution of free software. You can use,
* modify and/or redistribute the software under the terms of the CeCILL
* license as circulated by CEA, CNRS and INRIA at the following URL
* "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided only
* with a limited warranty and the software's author, the holder of the
* economic rights, and the successive licensors have only limited
* liability.
*
* In this respect, the user's attention is drawn to the risks associated
* with loading, using, modifying and/or developing or reproducing the
* software by the user in light of its specific status of free software,
* that may mean that it is complicated to manipulate, and that also
* therefore means that it is reserved for developers and experienced
* professionals having in-depth computer knowledge. Users are therefore
* encouraged to load and test the software's suitability as regards their
* requirements in conditions enabling the security of their systems and/or
* data to be ensured and, more generally, to use and operate it in the
* same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL license and that you accept its terms.
*/
package fr.bigeon.gclc.socket;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
import fr.bigeon.gclc.ConsoleApplication;
import fr.bigeon.gclc.ConsoleManager;
import fr.bigeon.smu.StringEncoder;
/** This is a socket communicating console consoleManager
* <p>
* To use this application, the following flow should be used:
*
* <pre>
* SocketConsoleApplicationShell shell = new SocketConsoleApplicationShell();
* ConsoleApplication myApplication = new MyConsoleApplication(shell.getConsoleManager(), ...);
* shell.setApplication(myApplication);
* Thread th = new Thread(shell);
* th.start();
* </pre>
* <p>
* This will start the application in a separate thread. The application will be
* listening on the given socket and writing back on it. If this is all your
* application, you should then {@link Thread#join()} the thread to wait for the
* end of the execution.
*
* @author Emmanuel Bigeon */
public class SocketConsoleApplicationShell implements Runnable {
/** The end of line character */
protected static final String EOL = "\n"; //$NON-NLS-1$
/** The encoder */
private static final StringEncoder ENCODER = new StringEncoder("%", Arrays.asList(EOL)); //$NON-NLS-1$
/** The listening port */
private final int port;
/** The output */
private PrintWriter output;
/** The input reader */
private BufferedReader input;
/** The input */
private final PipedInputStream consoleInput = new PipedInputStream();
/** The application */
private ConsoleApplication app;
/** The session closing command */
private final String close;
/** The running status */
private boolean running;
/** If the prompt should be next activity */
private boolean doPrompt = false;
/** An object to lock on for prompt */
private final Object promptingLock = new Object();
/** The console manager implementation */
private final ConsoleManager consoleManager = new ConsoleManager() {
private String prompt = new String();
private StringBuffer buffer = new StringBuffer();
@Override
public void setPrompt(String prompt) {
this.prompt = prompt;
}
@SuppressWarnings("synthetic-access")
@Override
public String prompt(String message) {
buffer.append(message);
String userInput = new String();
boolean prompting = true;
while (prompting) {
output.println(ENCODER.encode(buffer.toString()));
try {
synchronized (promptingLock) {
doPrompt = true;
promptingLock.notify();
}
userInput = input.readLine();
doPrompt = false;
prompting = false;
} catch (final IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
buffer = new StringBuffer();
return userInput;
}
@Override
public String prompt() {
return prompt(prompt);
}
@Override
public void println(String message) {
buffer.append(message + EOL);
}
@Override
public void println() {
buffer.append(EOL);
}
@Override
public void print(String text) {
buffer.append(text);
}
@Override
public String getPrompt() {
return prompt;
}
};
/** The auto close flag. if this is true, every request closes the session
* after its call */
private final boolean autoClose;
/** The runnable class that will actually have the application running.
*
* @author Emmanuel Bigeon */
private class ConsoleRunnable implements Runnable {
/**
*
*/
public ConsoleRunnable() {
// TODO Auto-generated constructor stub
}
/* (non-Javadoc)
* @see java.lang.Runnable#run() */
@SuppressWarnings("synthetic-access")
@Override
public void run() {
running = true;
app.start();
running = false;
synchronized (promptingLock) {
promptingLock.notifyAll();
}
}
}
/** TODO Describe SocketConsoleApplicationShell.java Constructor
*
* @param port the port to listen to
* @param close the session closing command */
public SocketConsoleApplicationShell(int port, String close) {
this.port = port;
this.close = close;
this.autoClose = false;
}
/** TODO Describe SocketConsoleApplicationShell.java Constructor
*
* @param port the port to listen to
* @param autoClose if the session must be closed once the request has been
* sent */
public SocketConsoleApplicationShell(int port, boolean autoClose) {
this.port = port;
this.close = null;
this.autoClose = autoClose;
}
/* (non-Javadoc)
* @see java.lang.Runnable#run() */
@Override
public void run() {
try (ServerSocket serverSocket = new ServerSocket(port)) {
final ConsoleRunnable runnable = new ConsoleRunnable();
final Thread appTh = new Thread(runnable);
running = true;
try (PipedOutputStream outStream = new PipedOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outStream))) {
consoleInput.connect(outStream);
try (BufferedReader inBuf = new BufferedReader(new InputStreamReader(consoleInput))) {
input = inBuf;
while (running) {
try (Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));) {
output = out;
// Initiate application
if (!appTh.isAlive()) {
appTh.start();
} else {
output.println("Reconnected"); //$NON-NLS-1$
}
synchronized (promptingLock) {
String ln;
if (!doPrompt) try {
promptingLock.wait();
} catch (final InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (running && (ln = in.readLine()) != null) {
if (ln.equals(close)) {
break;
}
writer.write(ln + EOL);
writer.flush();
try {
promptingLock.wait();
} catch (final InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (autoClose) running = false;
}
}
}
}
}
}
} catch (final IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/** @return the consoleManager */
public synchronized ConsoleManager getConsoleManager() {
return consoleManager;
}
/** @param app the application to set */
public synchronized void setApplication(ConsoleApplication app) {
this.app = app;
}
}

View File

@@ -0,0 +1,74 @@
/*
* Copyright E. Bigeon (2014)
*
* emmanuel@bigeon.fr
*
* This software is a computer program whose purpose is to
* Socket implementation of GCLC.
*
* This software is governed by the CeCILL license under French law and
* abiding by the rules of distribution of free software. You can use,
* modify and/or redistribute the software under the terms of the CeCILL
* license as circulated by CEA, CNRS and INRIA at the following URL
* "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided only
* with a limited warranty and the software's author, the holder of the
* economic rights, and the successive licensors have only limited
* liability.
*
* In this respect, the user's attention is drawn to the risks associated
* with loading, using, modifying and/or developing or reproducing the
* software by the user in light of its specific status of free software,
* that may mean that it is complicated to manipulate, and that also
* therefore means that it is reserved for developers and experienced
* professionals having in-depth computer knowledge. Users are therefore
* encouraged to load and test the software's suitability as regards their
* requirements in conditions enabling the security of their systems and/or
* data to be ensured and, more generally, to use and operate it in the
* same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL license and that you accept its terms.
*/
package fr.bigeon.gclc.socket;
import fr.bigeon.gclc.ConsoleApplication;
import fr.bigeon.gclc.ConsoleManager;
import fr.bigeon.gclc.command.Command;
import fr.bigeon.gclc.exception.InvalidCommandName;
/** TODO Describe ConsoleTestApplication.java
* @author Emmanuel Bigeon
*
*/
public class ConsoleTestApplication extends ConsoleApplication {
/** @param manager the manager */
@SuppressWarnings("nls")
public ConsoleTestApplication(final ConsoleManager manager) {
super(manager, "exit",
"Welcome to the test application. Type help or test.",
"See you");
addHelpCommand("help");
try {
add(new Command("test") {
@Override
public String tip() {
return "A test command";
}
@Override
public void execute(String... args) {
manager.println("Test command ran fine");
}
});
} catch (final InvalidCommandName e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,95 @@
/*
* Copyright E. Bigeon (2014)
*
* emmanuel@bigeon.fr
*
* This software is a computer program whose purpose is to
* Socket implementation of GCLC.
*
* This software is governed by the CeCILL license under French law and
* abiding by the rules of distribution of free software. You can use,
* modify and/or redistribute the software under the terms of the CeCILL
* license as circulated by CEA, CNRS and INRIA at the following URL
* "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided only
* with a limited warranty and the software's author, the holder of the
* economic rights, and the successive licensors have only limited
* liability.
*
* In this respect, the user's attention is drawn to the risks associated
* with loading, using, modifying and/or developing or reproducing the
* software by the user in light of its specific status of free software,
* that may mean that it is complicated to manipulate, and that also
* therefore means that it is reserved for developers and experienced
* professionals having in-depth computer knowledge. Users are therefore
* encouraged to load and test the software's suitability as regards their
* requirements in conditions enabling the security of their systems and/or
* data to be ensured and, more generally, to use and operate it in the
* same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL license and that you accept its terms.
*/
package fr.bigeon.gclc.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collection;
import fr.bigeon.smu.StringEncoder;
/** TODO Describe TestConsoleClient.java
* @author Emmanuel Bigeon
*
*/
@SuppressWarnings("nls")
public class TestConsoleClient {
@SuppressWarnings("javadoc")
private static final Collection<String> TO_ENCODE = new ArrayList<>();
static {
TO_ENCODE.add("\n");
}
@SuppressWarnings("javadoc")
private static final StringEncoder ENCODER = new StringEncoder("%",
TO_ENCODE);
@SuppressWarnings("javadoc")
public static void main(String[] args) {
final String hostName = "127.0.0.1";
final int portNumber = 3300;
try (Socket kkSocket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(kkSocket.getOutputStream(),
true);
BufferedReader in = new BufferedReader(new InputStreamReader(
kkSocket.getInputStream()));) {
String fromServer;
while ((fromServer = in.readLine()) != null) {
System.out.println("Server: \n" + ENCODER.decode(fromServer));
if (fromServer.equals("Bye.")) {
break;
}
final BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
final String fromUser = stdIn.readLine();
if (fromUser != null) {
System.out.println("Client: " + fromUser);
out.println(fromUser);
}
}
} catch (final IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright E. Bigeon (2014)
*
* emmanuel@bigeon.fr
*
* This software is a computer program whose purpose is to
* Socket implementation of GCLC.
*
* This software is governed by the CeCILL license under French law and
* abiding by the rules of distribution of free software. You can use,
* modify and/or redistribute the software under the terms of the CeCILL
* license as circulated by CEA, CNRS and INRIA at the following URL
* "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided only
* with a limited warranty and the software's author, the holder of the
* economic rights, and the successive licensors have only limited
* liability.
*
* In this respect, the user's attention is drawn to the risks associated
* with loading, using, modifying and/or developing or reproducing the
* software by the user in light of its specific status of free software,
* that may mean that it is complicated to manipulate, and that also
* therefore means that it is reserved for developers and experienced
* professionals having in-depth computer knowledge. Users are therefore
* encouraged to load and test the software's suitability as regards their
* requirements in conditions enabling the security of their systems and/or
* data to be ensured and, more generally, to use and operate it in the
* same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL license and that you accept its terms.
*/
package fr.bigeon.gclc.socket;
/** TODO Describe TestServer.java
* @author Emmanuel Bigeon
*
*/
public class TestServer {
/** @param args no argument */
@SuppressWarnings("nls")
public static void main(String[] args) {
final SocketConsoleApplicationShell shell = new SocketConsoleApplicationShell(
3300, "close");
final ConsoleTestApplication app = new ConsoleTestApplication(
shell.getConsoleManager());
shell.setApplication(app);
final Thread serverTh = new Thread(shell, "gclcServer");
serverTh.start();
try {
serverTh.join();
} catch (final InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}