Skip to content
Snippets Groups Projects
Commit 401c154c authored by Ludovic Apvrille's avatar Ludovic Apvrille
Browse files

No commit message

No commit message
parent b911fcd9
No related branches found
No related tags found
No related merge requests found
......@@ -176,7 +176,8 @@ public class RemoteSimulationControl extends Thread {
try {
while(true) {
s = rc.readOneLine();
System.out.println("From server: " + s);
System.out.print("From server: " + s + "\n>");
System.out.flush();
}
} catch (RemoteConnectionException rce) {
if (mygo) {
......@@ -203,8 +204,10 @@ public class RemoteSimulationControl extends Thread {
System.out.println("Ready to get commands:");
try {
dataIn = new BufferedReader(new InputStreamReader(System.in));
while(go) {
dataIn = new BufferedReader(new InputStreamReader(System.in));
System.out.print(">");
System.out.flush();
input = dataIn.readLine();
if (cp.isQuitCommand(input)) {
mygo = false;
......
......@@ -49,11 +49,15 @@ package remotesimulation;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.util.*;
public class CommandParser {
ArrayList<SimulationCommand> commandList;
public CommandParser() {
commandList = new ArrayList<SimulationCommand>();
fillCommandList();
}
public boolean isCommand(String cmd, String id) {
......@@ -85,6 +89,15 @@ public class CommandParser {
}
private void fillCommandList() {
SimulationCommand sc;
// kill-simulator
sc = new SimulationCommand("kill-simulator", "0", 0, 0, 0, 0, "Terminates the remote simulator");
commandList.add(sc);
}
......
......@@ -152,404 +152,5 @@ public class RemoteConnection {
return s;
}
/*public void stopFillJTA() throws RemoteConnectionException {
sendKillProcessRequest();
go = false;
closeConnect();
}
public int getId() throws LauncherException {
connect(port);
send("00");
int id = readId();
closeConnect();
return id;
}
public int freeId(int id) throws LauncherException {
connect(port);
send("0" + id);
int idret = readId();
if (idret != id) {
throw new LauncherException(ID_FAILED);
}
return idret;
}
public void sendProcessRequest() throws LauncherException {
connect(port);
send("1" + cmd);
readPortString();
closeConnect();
}
public void sendProcessRequest(String cmd1, String cmd2) throws LauncherException {
connect(port);
send("3" + cmd1);
int id1 = readPortString();
closeConnect();
connect(port);
send("3" + cmd2);
int id2 = readPortString();
closeConnect();
connect(port);
send("2" + id1 + " " + id2);
readReturnPipedProcesses();
closeConnect();
connect(port);
send("4" + id1);
readReturnPipedProcesses();
closeConnect();
connect(port);
send("4" + id2);
readReturnPipedProcesses();
closeConnect();
portString = id2;
portString2 = id1;
}
public void sendFileData(String fileName, String data) throws LauncherException {
connect(port);
send("7" + fileName);
sendFileData(data);
send("7" + fileName);
readReturn();
closeConnect();
}
public String getFileData(String fileName) throws LauncherException {
connect(port);
send("8" + fileName);
String s = readDataUntilCompletion();
closeConnect();
return s;
}
public void deleteFile(String fileName) throws LauncherException {
connect(port);
send("9" + fileName);
readReturn();
closeConnect();
}
public void sendKillProcessRequest() throws LauncherException {
connect(port);
send("6" + portString);
closeConnect();
if(portString2 != -1) {
connect(port);
send("6" + portString2);
closeConnect();
}
}
public void sendKillAllProcessRequest() throws LauncherException {
connect(port);
send("5");
closeConnect();
}
public String getDataFromProcess() throws LauncherException {
go = true;
StringBuffer bf = new StringBuffer();
//System.out.println("Connect");
connect(portString);
String s;
//System.out.println("Waiting for data");
while (((s = readProcessData()) != null) && (go == true)) {
bf.append(s + "\n");
}
//System.out.println("no more data : stopped");
closeConnect();
//System.out.println("Closed");
return new String(bf);
}
public void fillJTA(JTextArea jta) throws LauncherException {
go = true;
//System.out.println("Connect");
connect(portString);
String s;
//System.out.println("Waiting for data");
while (((s = readProcessData()) != null) && (go == true)) {
jta.append(s + "\n");
}
//System.out.println("no more data : stopped");
closeConnect();
//System.out.println("Closed");
}
public void closeConnect() throws LauncherException {
try {
clientSocket.close();
} catch (IOException io) {
throw new LauncherException(SERV_NOT_RESP+host);
}
}
private void send(String s) throws LauncherException {
//System.out.println("Sending: " + s);
try {
out.println(s);
out.flush();
} catch (Exception e) {
throw new LauncherException(IO_ERROR);
}
}
private void sendFileData(String data) throws LauncherException {
//System.out.println("Sending data");
StringReader sr = new StringReader(data);
BufferedReader br = new BufferedReader(sr);
String s;
try {
while((s = br.readLine()) != null) {
send("8" + s);
}
} catch (Exception e) {
throw new LauncherException(FILE_FAILED);
}
}
private String readProcessData() throws LauncherException {
int nb;
String s = null;
try {
s = in.readLine();
nb = Integer.decode(s.substring(0,1)).intValue();
} catch(IOException io) {
throw new LauncherException(IO_ERROR);
}
if (nb == 5) {
return null;
}
s = s.substring(1, s.length());
if (s == null) {
s = "";
}
return s;
}
private String readDataUntilCompletion() throws LauncherException {
int nb=0, nbTotal, cpt = 0;
String s = null;
StringBuffer ret = new StringBuffer();
char []c = new char[BUFSIZE+1];
int read;
try {
//System.out.println("Reading first data ");
s = in.readLine();
nb = Integer.decode(s.substring(0,1)).intValue();
if (nb == 8) {
nbTotal = Integer.decode(s.substring(1,s.length())).intValue();
//System.out.println("Total= " + nbTotal);
while (((cpt < nbTotal) && (read = in.read(c, 0, Math.min(BUFSIZE, nbTotal - cpt))) > -1)) {
//s = new String(c, 0, read);
//System.out.println("Nb read: " + read + " size of s =" + s.length());
//nb = Integer.decode(s.substring(0,1)).intValue();
//ret.append(s.substring(0, s.length()));
ret.append(c, 0, read);
cpt += read;
}
// Read last info
//System.out.println("Reading last info");
nb = readReturn();
//System.out.println("Return = " + nb);
//read = in.read(c, 0, 1);
//s = new String(c, 0, read);
//System.out.println("Last s=" + s + " read=" + read);
//nb = Integer.decode(s.substring(0,1)).intValue();
//System.out.println("Last info=" + nb);
//nb= 3;
}
} catch(IOException io) {
throw new LauncherException(IO_ERROR);
}
if (nb != 3) {
throw new LauncherException(FILE_FAILED);
}
return new String(ret);
}
private int readReturn() throws LauncherException {
int nb;
String s = null;
try {
//System.out.println("Reading line");
s = in.readLine();
//System.out.println("Line read");
//System.out.println("Converting nb s=>" + s + "<");
nb = Integer.decode(s.substring(0,1)).intValue();
//System.out.println("Nb = " + nb);
} catch(IOException io) {
System.out.println("Exception 0");
throw new LauncherException(IO_ERROR);
}
if (nb != 3) {
System.out.println("Exception 1");
throw new LauncherException(FILE_FAILED);
}
return nb;
}
private int readId() throws LauncherException {
int nb;
String s = null;
try {
s = in.readLine();
nb = Integer.decode(s.substring(0,1)).intValue();
} catch(IOException io) {
throw new LauncherException(IO_ERROR);
}
if (nb == 0) {
throw new LauncherException(ID_FAILED);
}
return nb;
}
private void readReturnPipedProcesses() throws LauncherException {
int nb;
String s = null;
try {
s = in.readLine();
nb = Integer.decode(s.substring(0,1)).intValue();
} catch(IOException io) {
throw new LauncherException(IO_ERROR);
}
if (nb != 3) {
throw new LauncherException(PROC_FAILED);
}
}
private int readPortString() throws LauncherException {
int nb;
String s = null;
try {
s = in.readLine();
nb = Integer.decode(s.substring(0,1)).intValue();
} catch(IOException io) {
throw new LauncherException(IO_ERROR);
}
if (nb == 2) {
throw new LauncherException(PROC_FAILED);
}
portString = -1;
portString2 = -1;
try {
portString = Integer.decode(s.substring(1, s.length())).intValue();
} catch (Exception e) {
throw new LauncherException(IO_ERROR);
}
if (portString <1) {
throw new LauncherException(PROC_FAILED);
}
return portString;
}
private void connect(int portNet) throws LauncherException {
InetAddress ina = null;
//System.out.println("Connecting on port " + portNet);
if (host == null) {
throw new LauncherException(NO_HOST);
}
try {
ina = InetAddress.getByName(host);
} catch (UnknownHostException e) {
throw new LauncherException(INET + host);
}
try {
clientSocket = new Socket(ina, portNet);
} catch (IOException io) {
throw new LauncherException(SERV_NOT_RESP+host);
}
try {
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//in2 = new DataInputStream(clientSocket.getInputStream());
out = new PrintStream(clientSocket.getOutputStream());
//System.out.println("Connected on port " + portNet);
} catch (Exception e) {
throw new LauncherException(SERV_NOT_RESP+host);
}
}
/*private void basicConnect() throws LauncherException {
InetAddress ina = null;
if (host == null) {
throw new LauncherException(NO_HOST);
}
try {
ina = InetAddress.getByName(host);
} catch (UnknownHostException e) {
throw new LauncherException(INET + host);
}
try {
clientSocket = new Socket(ina, port);
} catch (IOException io) {
throw new LauncherException(SERV_NOT_RESP+host);
}
try {
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//in2 = new DataInputStream(clientSocket.getInputStream());
out = new PrintStream(clientSocket.getOutputStream());
} catch (Exception e) {
throw new LauncherException(SERV_NOT_RESP+host);
}
}*/
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment