Hello everybody,
I have a xml socket server which get data from xml sockets out of actionscript3. Now I'm having a lot of if statements. Can I use another way to make my code more clean? Ivibot is a piece of hardware my school provided. LedBlink is a runnable class that let's a led blinking on a specific port. Engine1 and Engine2 are also Runnable classes that let the engine run for like 30 sec. By the way I'm dutch so that's why you see dutch words. Here some translation Kleur is Color and kast is Closet. Thanks in advance for your help :)
Here below you see the server class:
package gc;
import HW.*;
import java.io.*;
import java.net.*;
import javax.swing.JTextArea;
import usb.ivi.DeviceNotFoundException;
import usb.ivi.*;
class SimpleServer {
private static SimpleServer server;
private static GUI gui;
private static Ivi ivi = new Ivi();
private int counter = 0;
private int counter2 = 0;
private static final byte EOF = (byte) 0x00;
private ServerSocket serverSocket; //New ServerSocket
private Socket childSocket; //New Socket
private BufferedReader readerIn;
private OutputStream socketOut;
public static void main(String[] args) {
int port = 8090;
try {
port = Integer.parseInt(args[0]);
}
catch(ArrayIndexOutOfBoundsException e) {
// Catch exception and keep going.
}
gui = new GUI();
gui.main(args);
server = new SimpleServer(port);
}
private SimpleServer(int port) {
System.out.println(" << Starting XML OwlServer on Port: " + port + " >>");
gui.setMsgText(" << Starting XML OwlServer on Port: " + port + " >>");
try {
serverSocket = new ServerSocket(port);
childSocket = serverSocket.accept();
System.out.println("The connection is accepted.");
gui.setMsgText("The connection is accepted.");
InputStream in = childSocket.getInputStream();
socketOut = childSocket.getOutputStream();
out("<msg>Send <exit /> to do an exit.</msg>");
while (true) {
String line = "";
int c;
while( (c = in.read()) != 0 )
line += (char)c;
if( line.length() < 1 )
continue;
//readerIn.read(); // read extra EOF
System.out.println("Received input string: \"" + line + "\"");
gui.setMsgText("Received input string: \"" + line + "\"");
//System.out.println("UTF-8 bytes: " + asHex(line));
line = line.trim();
if(line.equalsIgnoreCase("<exit/>")) {
out("Bye, your connection will be closed.");
gui.setMsgText("Bye, your connection will be closed.");
System.exit(0);
break;
}
if(line.equalsIgnoreCase("Kast 4")) {
gui.setMsgText("Boekenkast 4 doe iets!");
new Engine2(50);
}
if(line.equalsIgnoreCase("Kleur 3")){
System.out.println("rood");
new LedBlink(0);
}
if(line.equalsIgnoreCase("Kleur 4")){
new LedBlink(1);
}
if(line.equalsIgnoreCase("Kleur 5")){
new LedBlink(2);
}
if(line.equalsIgnoreCase("Kast 6")) {
new Engine1(50);
}
if(line.equalsIgnoreCase("Kleur 0")){
System.out.println("rood");
new LedBlink(0);
}
if(line.equalsIgnoreCase("Kleur 1")){
new LedBlink(1);
}
if(line.equalsIgnoreCase("Kleur 2")){
new LedBlink(2);
}
if(line.equalsIgnoreCase("<motorstop/>")){
System.out.println("Stop engine!");
gui.setMsgText("Stop engine!");
ivi.stopBeweging();
}
if(line.equalsIgnoreCase("<motor1stop/>")){
System.out.println("Stop engine1!");
gui.setMsgText("Stop engine1!");
ivi.stopMotor1();
}
if(line.equalsIgnoreCase("<motor2stop/>")){
System.out.println("Stop engine2!");
gui.setMsgText("Stop engine2!");
ivi.stopMotor2();
}
if(line.equalsIgnoreCase("<lichtuit/>")){
System.out.println("Put out the lights!");
gui.setMsgText("Put out the lights!");
ivi.dLeds_off();
ivi.bLeds_off();
}
}
childSocket.close();
socketOut.close();
serverSocket.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
private void out(String str) {
System.out.println(">> " + str);
System.out.println("UTF-8 bytes: " + asHex(str) + " 00");
try {
byte[] strBytes = str.getBytes("UTF-8");
socketOut.write(strBytes);
socketOut.write(EOF);
socketOut.flush();
}
catch(UnsupportedEncodingException e) {
// won't happen
}
catch(IOException e){
throw new RuntimeException(e);
}
}
protected String asHex(String str) {
StringBuffer sb = new StringBuffer();
try {
byte[] bytes = str.getBytes("UTF-8");
for(int i = 0; i < bytes.length - 1; i++) {
sb.append(hexByte(bytes[i]));
sb.append(" ");
}
sb.append(hexByte(bytes[bytes.length - 1]));
}
catch(UnsupportedEncodingException e) {
// won't happen, UTF-8 is supported
}
return sb.toString();
}
protected String hexByte(byte b) {
int i = b & 0xFF;
String hex = Integer.toHexString(i).toUpperCase();
if(i < 0x10) hex = "0" + hex;
return hex;
}
}