diff --git a/src/native/hwoio.dll b/src/native/hwoio.dll index 7a23106..47b883d 100644 Binary files a/src/native/hwoio.dll and b/src/native/hwoio.dll differ diff --git a/src/native/libhwoio32.so b/src/native/libhwoio32.so new file mode 100755 index 0000000..21b7cac Binary files /dev/null and b/src/native/libhwoio32.so differ diff --git a/src/org/hwo/StringHelper.java b/src/org/hwo/StringHelper.java index 3d44cdd..8f387fe 100644 --- a/src/org/hwo/StringHelper.java +++ b/src/org/hwo/StringHelper.java @@ -25,5 +25,14 @@ public class StringHelper { return sb.toString(); } + public static String fromCharacters(Character[] chars){ + char[] chs = new char[chars.length]; + + for (int i=0;i beaconSenders; public Beacon(int port) @@ -106,34 +115,87 @@ public class Beacon extends Thread{ this.properties = new Properties(); this.intervall = 5000; this.beaconSenders = new Hashtable(); - this.beaconName = "DefaultBeacon"; this.uuid = UUID.randomUUID(); + + try { + this.beaconName = InetAddress.getLocalHost().getHostName(); + } catch (UnknownHostException e) { + this.beaconName = "Unkown Host"; + } + } + public void setServerOnly(boolean serverOnly) { + this.serverOnly = serverOnly; + } + public boolean isServerOnly() { + return serverOnly; + } + public void setClientOnly(boolean clientOnly) { + this.clientOnly = clientOnly; + } + public boolean isClientOnly() { + return clientOnly; + } + + public boolean isAlive(){ + return this.thread != null; + } + + public synchronized void start() { + if (thread == null) { + thread = new Thread(new Runnable() { + + @Override + public void run() { + Beacon.this.run(); + } + }); + thread.start(); + } + } + public synchronized void exit() { - exit = true; + if (thread != null){ + exit = true; + socket.close(); + try { + thread.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + thread = null; + } } - public synchronized boolean getExit() + public boolean getExit() { return exit; } + public void setExit(Boolean exit){ + this.exit = exit; + } - @Override public void run() { Timer pingTimer = new Timer(); - pingTimer.schedule(new TimerTask() { - - @Override - public void run() { - ping(); - } - }, intervall, intervall); + if (!clientOnly){ + pingTimer.schedule(new TimerTask() { + + @Override + public void run() { + ping(); + } + }, intervall, intervall); + } try { - socket = new DatagramSocket(this.port); + if (serverOnly) + socket = new DatagramSocket(); + else + socket = new DatagramSocket(this.port); + DatagramPacket packet = new DatagramPacket(new byte[1500], 1500); while (!getExit()) @@ -163,7 +225,6 @@ public class Beacon extends Thread{ }; } catch (IOException e) { - e.printStackTrace(); } } @@ -175,6 +236,24 @@ public class Beacon extends Thread{ } + public BeaconSender getSenderByUUID(UUID uuid){ + + for (BeaconSender sender: this.beaconSenders.values()){ + if (UUID.fromString(sender.getProperties().getProperty("beacon.uuid")).equals(uuid)) + return sender; + } + return null; + } + + public BeaconSender getSenderByName(String beaconName){ + + for (BeaconSender sender: this.beaconSenders.values()){ + if (sender.getProperties().getProperty("beacon.name").equals(beaconName)) + return sender; + } + return null; + } + public void ping() { DatagramSocket sock = socket; diff --git a/src/org/hwo/csv/CSV.java b/src/org/hwo/csv/CSV.java index 614299e..a0b66ad 100644 --- a/src/org/hwo/csv/CSV.java +++ b/src/org/hwo/csv/CSV.java @@ -17,50 +17,117 @@ import java.util.LinkedList; import java.util.List; import java.util.Vector; +import org.hwo.StringHelper; +import org.hwo.text.LineReader; + import javassist.bytecode.EnclosingMethodAttribute; public class CSV { - List records; + List records; private char separator; private char enclosedBy; - boolean isEnclosed; - List parserRecord; - List parserField; - char lastChar; - public CSV() { - this.records = new LinkedList(); + this.records = new LinkedList(); this.setSeparator(';'); - this.setEnclosedBy('\"'); - - this.parserRecord = new ArrayList(); - this.parserField = new ArrayList(); + this.setEnclosedBy('\"'); } - - private void nextRecord() - { - nextField(); - this.records.add(parserRecord.toArray(new String[]{})); - parserRecord.clear(); + public void readFromFile(String filename) + { + try { + readFromStream(new FileInputStream(filename)); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } } - private void nextField() + public void readFromStream(InputStream is) throws IOException { - char fieldChars[] = new char[ parserField.size() ]; - - for (int i=0;i=0){ + try { + Double d = Double.parseDouble(text); + return d; + } catch (Exception ex2){ + } + } else { + try { + Integer i = Integer.parseInt(text); + return i; + } catch (Exception ex){ + } + } + + return text; + } + + private CSVRecord textToRow(String line){ + CSVRecord record = new CSVRecord(); + LineReader reader = new LineReader(line); + + ArrayList cell = null; + boolean enclosed = false; + + while (!reader.endOfLine()){ + char ch = reader.read(); + + if ((this.separator == ch) && (!enclosed) ){ + if (cell != null){ + String celltext = StringHelper.fromCharacters(cell.toArray(new Character[0])); + record.appendValue(textToObject(celltext)); + } + cell = new ArrayList(); + + } else if (this.enclosedBy == ch){ + if (enclosed){ + if (reader.peek() == ch){ + cell.add(reader.read()); + } else { + enclosed = false; + String celltext = StringHelper.fromCharacters(cell.toArray(new Character[0])); + record.appendValue(celltext); + cell = null; + } + } else { + if (cell.size() == 0){ + enclosed = true; + } else { + cell.add(ch); + } + } + } else { + if (cell == null){ + cell = new ArrayList(); + } + cell.add(ch); + } + } + return record; + } + +/* private void feeder(char ch) { if (ch == getEnclosedBy()) { @@ -89,37 +156,8 @@ public class CSV { lastChar = ch; } - - public void readFromFile(String filename) - { - try { - readFromStream(new FileInputStream(filename)); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - public void readFromStream(InputStream is) throws IOException - { - this.readFromStream(new InputStreamReader(is,"ISO-8859-1")); - } - - public void readFromStream(InputStreamReader isr) throws IOException - { - BufferedReader br = new BufferedReader(isr); - - isEnclosed = false; - records.clear(); - parserField.clear(); - parserRecord.clear(); - - while (br.ready()) - feeder((char)br.read()); - } - - + + */ public void saveToFile(String filename) { saveToFile(new File(filename)); @@ -154,21 +192,21 @@ public class CSV { try { - for (String[] record:this.records) + for (CSVRecord record:this.records) { - for (int i=0;i0) writer.write(separator); - if (record[i] != null) + if (record.getValue(i) != null) { - if (String.class.isInstance(record[i])) + if (String.class.isInstance(record.getValue(i))) writer.write(enclosedBy); - writer.write(record[i]); + writer.write(record.getValue(i).toString()); - if (String.class.isInstance(record[i])) + if (String.class.isInstance(record.getValue(i))) writer.write(enclosedBy); } } @@ -184,37 +222,30 @@ public class CSV { } - public String getValue(int row,int column) + public Object getValue(int row,int column) { try { - return this.records.get(row)[column]; + return this.records.get(row).getValue(column).toString(); } catch (ArrayIndexOutOfBoundsException ex) { - return ""; + return null; } } + - public List getCells() + + + + public List getRecords() { return this.records; } - public List getCellsAsObjects() - { - List ol = new ArrayList(); - - for (String[] record:records) - { - Object[] ro = new Object[record.length]; - for (int i=0;i columns; + + public CSVRecord(){ + columns = new ArrayList(); + } + + public Object getValue(int column){ + return columns.get(column); + } + + public void setValue(int column,Object value){ + columns.set(column, value); + } + + public void appendValue(Object value){ + columns.add(value); + } + + public int size(){ + return columns.size(); + } + + public String getStringValue(int column){ + return getValue(column).toString(); + } + + public Integer getIntegerValue(int column){ + Object v = getValue(column); + if (Integer.class.isInstance(v)){ + return (Integer)v; + } + return null; + } + + public Double getDoubleValue(int column){ + Object v = getValue(column); + if (Integer.class.isInstance(v)){ + return (Double)v; + } + return null; + } + + + +} diff --git a/src/org/hwo/diagram/Diagram.java b/src/org/hwo/diagram/Diagram.java new file mode 100644 index 0000000..3622f82 --- /dev/null +++ b/src/org/hwo/diagram/Diagram.java @@ -0,0 +1,36 @@ +package org.hwo.diagram; + +import java.awt.image.BufferedImage; +import java.util.LinkedList; + +public class Diagram { + + private Double scale_x_min, + scale_x_max, + scale_y_min, + scale_y_max; + + + private LinkedList plots; + + + public Diagram(){ + plots = new LinkedList(); + } + + public Plot newPlot(){ + Plot p = new Plot(this); + this.plots.add(p); + return p; + } + + + + + + + public BufferedImage plot(){ + return null; + } + +} diff --git a/src/org/hwo/diagram/Plot.java b/src/org/hwo/diagram/Plot.java new file mode 100644 index 0000000..7e9053e --- /dev/null +++ b/src/org/hwo/diagram/Plot.java @@ -0,0 +1,25 @@ +package org.hwo.diagram; + +public class Plot { + + private Diagram diagram; + + private String label; + + protected Plot(Diagram diagram){ + this.diagram = diagram; + } + + public Diagram getDiagram() { + return diagram; + } + + public String getLabel() { + return label; + } + public void setLabel(String label) { + this.label = label; + } + + +} diff --git a/src/org/hwo/io/NativeSerialPort.java b/src/org/hwo/io/NativeSerialPort.java index 36d1a10..cc8d1eb 100644 --- a/src/org/hwo/io/NativeSerialPort.java +++ b/src/org/hwo/io/NativeSerialPort.java @@ -11,19 +11,32 @@ import java.util.concurrent.TimeoutException; import org.hwo.fifo.FiFo; import org.hwo.nativeloader.NativeLoader; + +/* NATIVE RETURN VALUES: + * + * 0 No error + * -1 Unknown IO Error + * -2 Timeout + * -3 Interface could not be opened + * -4 Parameter Error + * -5 More than one character returned + * -6 Port not opened + * -99 Java <-> C Interface Error + * + */ + public class NativeSerialPort extends SerialPort { class SerialPortInputStream extends InputStream { @Override - public int read() throws IOException { + public int read() throws SerialPortExeption { if (!isOpen()) - throw new IOException("Port nicht gešffnet!"); + throw new SerialPortExeption(-6); int ch = native_getch(nativeHandle,getTimeout()); - //System.err.println(String.format("RX: 0x%08x", ch)); if (ch < 0) - throw new IOException(String.format("native_getch: returned: %d",ch)); + throw new SerialPortExeption(ch); return ch; } @@ -32,10 +45,9 @@ public class NativeSerialPort extends SerialPort { class SerialPortOutputStream extends OutputStream { @Override - public void write(int arg0) throws IOException { + public void write(int arg0) throws SerialPortExeption { if (!isOpen()) - throw new IOException("Port nicht gešffnet!"); - //System.err.println(String.format("TX: 0x%08x", arg0)); + throw new SerialPortExeption(-6); native_putch(nativeHandle, getTimeout(), arg0); } } @@ -72,7 +84,7 @@ public class NativeSerialPort extends SerialPort { return false; nativeHandle = native_open(getPortName()); - System.err.println(String.format("NativeSerialPort: nativeHandle: %d",nativeHandle)); + System.err.println(String.format("NativeSerialPort: nativeHandle: %d for device %s",nativeHandle,getPortName())); if (nativeHandle < 0) return false; diff --git a/src/org/hwo/io/SerialPort.java b/src/org/hwo/io/SerialPort.java index 68ef084..bc8924e 100644 --- a/src/org/hwo/io/SerialPort.java +++ b/src/org/hwo/io/SerialPort.java @@ -42,7 +42,7 @@ public abstract class SerialPort { protected SerialPort() { this.portName = ""; - this.timeout = -1; + this.timeout = 2500; } abstract public boolean isOpen(); diff --git a/src/org/hwo/io/SerialPortExeption.java b/src/org/hwo/io/SerialPortExeption.java new file mode 100644 index 0000000..de2fe02 --- /dev/null +++ b/src/org/hwo/io/SerialPortExeption.java @@ -0,0 +1,53 @@ +package org.hwo.io; + +import java.io.IOException; +/* +* -1 Unknown IO Error +* -2 Timeout +* -3 Interface could not be opened +* -4 Parameter Error +* -5 More than one character returned +* -6 Port not opened +* +*/ + +public class SerialPortExeption extends IOException { + + private int code; + + public SerialPortExeption(){ + this.code = -99; + } + + public SerialPortExeption(int code){ + super(getMessage(code)); + this.code = code; + } + + static protected String getMessage(int code){ + switch (code){ + case -1: + return "Unknown IO Error"; + case -2: + return "Timeout occured"; + case -3: + return "Interface could not be opened"; + case -4: + return "Parameter error"; + default: + return "unknown"; + + } + } + + public int getCode() { + return code; + } + + @Override + public String toString() { + return String.format("SerialPortException: [%d] %s",code,getMessage(code)); + } + + +} diff --git a/src/org/hwo/models/TableMapper/TableMapper.java b/src/org/hwo/models/TableMapper/TableMapper.java index 40d8fbb..6f9f6e2 100644 --- a/src/org/hwo/models/TableMapper/TableMapper.java +++ b/src/org/hwo/models/TableMapper/TableMapper.java @@ -19,6 +19,7 @@ import javax.swing.event.ListSelectionListener; import javax.swing.table.AbstractTableModel; import org.hwo.csv.CSV; +import org.hwo.csv.CSVRecord; import org.hwo.interactiveobjects.InteractiveObjectHelper; import org.hwo.ui.KeyStrokeHelper; import org.hwo.ui.KeyStrokeListener; @@ -637,23 +638,23 @@ public class TableMapper extends AbstractTableModel { CSV csv = new CSV(); - List cells = csv.getCells(); + List cells = csv.getRecords(); - String[] header = new String[ getColumnCount() ]; + CSVRecord header = new CSVRecord(); for (int i=0;i T createProxy(Class iface){ T proxy = (T) Proxy.newProxyInstance(iface.getClassLoader(), new Class[]{ iface }, this); return proxy; } + + private byte[] request(byte[] request) throws Throwable{ + if (url != null) + return requestURL(request); + if (inetAddress != null) + return requestTCP(request); + return new byte[0]; + } + private byte[] requestURL(byte[] request) throws Throwable{ + URL url = new URL(this.url); + URLConnection conn = url.openConnection(); + conn.setDoInput(true); + conn.setDoOutput(true); + OutputStream out = conn.getOutputStream(); + out.write(request); + out.flush(); + out.close(); + + BufferedReader reader = new BufferedReader(new InputStreamReader( conn.getInputStream())); + StringBuilder sb = new StringBuilder(); + String line; + + while ((line = reader.readLine())!=null) { + sb.append(line); + } + + return sb.toString().getBytes(); + } + private byte[] requestTCP(byte[] request) { + + try { + Socket socket = new Socket(inetAddress,port); + + DataInputStream dis = new DataInputStream(socket.getInputStream()); + DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); + + dos.writeInt(request.length); + dos.write(request); + + int len = dis.readInt(); + byte[] response = new byte[len]; + dis.read(response,0,len); + + dis.close(); + dos.close(); + socket.close(); + + } catch (IOException e) { + e.printStackTrace(); + } + + return new byte[0]; + } @Override public Object invoke(Object proxy, Method method, Object[] args) @@ -43,29 +110,13 @@ public class JSONRpcService implements InvocationHandler{ req.put("params", Arrays.asList(args)); String reqString = JSONValue.toJSONString(req); - System.err.println(reqString); - URL url = new URL(this.url); - URLConnection conn = url.openConnection(); - conn.setDoInput(true); - conn.setDoOutput(true); - OutputStream out = conn.getOutputStream(); - out.write(reqString.getBytes()); - out.flush(); - out.close(); + byte[] responseBytes = request( reqString.getBytes() ); - BufferedReader reader = new BufferedReader(new InputStreamReader( conn.getInputStream())); - StringBuilder sb = new StringBuilder(); - String line; + System.err.println("Response: " + new String(responseBytes)); - while ((line = reader.readLine())!=null) { - sb.append(line); - } - - System.err.println("Response: " + sb.toString()); - - Map response = (Map)JSONValue.parse(sb.toString()); + Map response = (Map)JSONValue.parse(new String(responseBytes)); if (response.containsKey("error") && (response.get("error") != null)) { throw new RuntimeException("JSON-RPC: " + response.get("error")); diff --git a/src/org/hwo/rpc/json/RPCAdapter.java b/src/org/hwo/rpc/json/RPCAdapter.java new file mode 100644 index 0000000..f6aa3d6 --- /dev/null +++ b/src/org/hwo/rpc/json/RPCAdapter.java @@ -0,0 +1,54 @@ +package org.hwo.rpc.json; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; + +public abstract class RPCAdapter { +/* + * InetAddress listenAddress; + int listenPort; + + boolean exit; + + public JSONRpcServer(InetAddress listen,int port){ + this.listenAddress = listen; + this.listenPort = port; + } + + @Override + public void run() { + try { + + ServerSocket serverSocket = new ServerSocket(listenPort, 5, listenAddress); + + while (!exit){ + Socket socket = serverSocket.accept(); + + DataInputStream dis = new DataInputStream(socket.getInputStream()); + DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); + + int len = dis.readInt(); + if (len < 10240){ + byte[] requestBytes = new byte[ len ]; + dis.read(requestBytes,0,len); + } + + } + + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void exit() + { + this.exit = true; + } + */ + + +} diff --git a/src/org/hwo/rpc/simple/SimpleRPCRequest.java b/src/org/hwo/rpc/simple/SimpleRPCRequest.java new file mode 100644 index 0000000..efed733 --- /dev/null +++ b/src/org/hwo/rpc/simple/SimpleRPCRequest.java @@ -0,0 +1,27 @@ +package org.hwo.rpc.simple; + +import java.io.Serializable; + +public class SimpleRPCRequest implements Serializable{ + + private String interfaceName; + private String methodName; + private Object[] params; + + public SimpleRPCRequest(String interfaceName,String methodName,Object[] params){ + this.interfaceName = interfaceName; + this.methodName = methodName; + this.params = params; + } + + public String getInterfaceName() { + return interfaceName; + } + public String getMethodName() { + return methodName; + } + public Object[] getParams() { + return params; + } + +} diff --git a/src/org/hwo/rpc/simple/SimpleRPCResponse.java b/src/org/hwo/rpc/simple/SimpleRPCResponse.java new file mode 100644 index 0000000..81dad08 --- /dev/null +++ b/src/org/hwo/rpc/simple/SimpleRPCResponse.java @@ -0,0 +1,23 @@ +package org.hwo.rpc.simple; + +import java.io.Serializable; + +public class SimpleRPCResponse implements Serializable{ + + + private Exception exception; + private Object result; + + public SimpleRPCResponse(Object result,Exception exception){ + this.exception = exception; + this.result = result; + } + + public Exception getException() { + return exception; + } + public Object getResult() { + return result; + } + +} diff --git a/src/org/hwo/rpc/simple/SimpleRPCServer.java b/src/org/hwo/rpc/simple/SimpleRPCServer.java new file mode 100644 index 0000000..b88f3cf --- /dev/null +++ b/src/org/hwo/rpc/simple/SimpleRPCServer.java @@ -0,0 +1,144 @@ +package org.hwo.rpc.simple; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.lang.reflect.Method; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.Hashtable; + +public class SimpleRPCServer extends Thread{ + + private Hashtable registeredObjects; + private ServerSocket serverSocket; + + private boolean exit; + + public SimpleRPCServer(InetAddress listenAddress,int port) throws IOException + { + this.registeredObjects = new Hashtable(); + this.serverSocket = new ServerSocket(port, 5, listenAddress); + } + + @Override + public void run() { + while (!exit){ + try { + handleOneRequest(); + } catch (IOException e) { + e.printStackTrace(); + } + } + exit = false; + } + + public void exit(){ + this.exit = true; + try { + serverSocket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void handleOneRequest() throws IOException{ + Socket socket = serverSocket.accept(); + + DataInputStream dis = new DataInputStream(socket.getInputStream()); + int len = dis.readInt(); + byte[] request = new byte[ len ]; + dis.read(request); + + byte[] response = handleRequest(request); + + DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); + dos.writeInt(response.length); + dos.write(response); + + dos.flush(); + dos.close(); + dis.close(); + } + + + public byte[] handleRequest(byte[] request){ + try { + ByteArrayInputStream i = new ByteArrayInputStream(request); + ObjectInputStream ois = new ObjectInputStream(i); + + SimpleRPCRequest simpleRequest = (SimpleRPCRequest)ois.readObject(); + Exception exception = null; + Object result = null; + + if (registeredObjects.containsKey(simpleRequest.getInterfaceName())){ + try { + result = this.invoke(registeredObjects.get(simpleRequest.getInterfaceName()), simpleRequest.getMethodName(), simpleRequest.getParams()); + } catch (Exception e){ + exception = e; + result = null; + } + } else { + exception = new ClassNotFoundException(String.format("RPC-Interface %s is unknown.",simpleRequest.getInterfaceName())); + } + + SimpleRPCResponse simpleResponse = new SimpleRPCResponse(result, exception); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(bos); + + oos.writeObject(simpleResponse); + oos.flush(); + oos.close(); + bos.flush(); + bos.close(); + + return bos.toByteArray(); + + } catch (IOException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + + + return new byte[0]; + } + + private Object invoke(Object obj,String methodName,Object[] params) throws Exception { + + Class clazz = obj.getClass(); + Class[] paramTypes; + + if (params != null) { + paramTypes = new Class[ params.length ]; + + for (int i=0;i[0]; + } + + Method method = clazz.getDeclaredMethod(methodName, paramTypes); + + Object result = method.invoke(obj, params); + + return result; + } + + public void registerObject(Class interfaceClass,Object obj){ + this.registerObject(interfaceClass.getCanonicalName(), obj); + } + public void registerObject(String name,Object obj){ + this.registeredObjects.put(name, obj); + } + + +} diff --git a/src/org/hwo/rpc/simple/SimpleRPCService.java b/src/org/hwo/rpc/simple/SimpleRPCService.java new file mode 100644 index 0000000..d6bcc06 --- /dev/null +++ b/src/org/hwo/rpc/simple/SimpleRPCService.java @@ -0,0 +1,109 @@ +package org.hwo.rpc.simple; + +import java.awt.peer.TextComponentPeer; +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.OutputStream; +import java.lang.reflect.Array; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.net.HttpURLConnection; +import java.net.InetAddress; +import java.net.Socket; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import org.json.simple.JSONValue; + +public class SimpleRPCService { + + private class InvocationHelper implements InvocationHandler { + + private String interfaceName; + + public InvocationHelper(String interfaceName){ + this.interfaceName = interfaceName; + } + + @Override + public Object invoke(Object proxy, Method method, Object[] args) + throws Throwable { + + SimpleRPCRequest simpleRequest = new SimpleRPCRequest(interfaceName, method.getName(), args); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(bos); + + oos.writeObject(simpleRequest); + oos.flush(); + oos.close(); + bos.close(); + + byte[] request = bos.toByteArray(); + + Socket socket = new Socket(inetAddress,port); + socket.setSoTimeout(soTimeout); + DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); + dos.writeInt(request.length); + dos.write(request); + dos.flush(); + + DataInputStream dis = new DataInputStream(socket.getInputStream()); + int len = dis.readInt(); + byte[] response = new byte[ len ]; + dis.read(response); + dis.close(); + dos.close(); + socket.close(); + + ByteArrayInputStream bis = new ByteArrayInputStream(response); + ObjectInputStream ois = new ObjectInputStream(bis); + + SimpleRPCResponse simpleResponse = (SimpleRPCResponse)ois.readObject(); + + if (simpleResponse.getException() != null) + throw simpleResponse.getException(); + + return simpleResponse.getResult(); + } + } + + private InetAddress inetAddress; + private int port; + private int soTimeout; + + public SimpleRPCService(InetAddress inetAddress,int port){ + this.inetAddress = inetAddress; + this.port = port; + this.soTimeout = 30000; + } + + public int getSoTimeout() { + return soTimeout; + } + public void setSoTimeout(int soTimeout) { + this.soTimeout = soTimeout; + } + + public T createProxy(Class iface){ + T proxy = (T) Proxy.newProxyInstance(iface.getClassLoader(), new Class[]{ iface }, new InvocationHelper(iface.getCanonicalName())); + System.err.println("SimpleRPCService: Created Proxy for Interface: " + iface.getCanonicalName() + " at IP: " + inetAddress.toString()); + return proxy; + } + + + +} diff --git a/src/org/hwo/rpc/simple/SimpleRPCTest.java b/src/org/hwo/rpc/simple/SimpleRPCTest.java new file mode 100644 index 0000000..f10edec --- /dev/null +++ b/src/org/hwo/rpc/simple/SimpleRPCTest.java @@ -0,0 +1,60 @@ +package org.hwo.rpc.simple; + +import java.net.InetAddress; +import java.net.UnknownHostException; + +public class SimpleRPCTest { + + interface TestInterface { + + public int add(Integer a,Integer b); + + } + + static class TestClass implements TestInterface{ + + public TestClass(){ + } + + @Override + public int add(Integer a, Integer b) { + return a + b; + } + } + + + public static void main(String[] args) { + + + + try { + + SimpleRPCServer server = new SimpleRPCServer(InetAddress.getLocalHost(),44352); + SimpleRPCService service = new SimpleRPCService(InetAddress.getLocalHost(), 44352); + + TestClass tc = new TestClass(); + server.registerObject(TestInterface.class, tc); + + server.start(); + + TestInterface ti = service.createProxy(TestInterface.class); + + System.err.println("ti.add(): " + ti.add(4, 5)); + + + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + + + + + + } + + +} diff --git a/src/org/hwo/text/LineReader.java b/src/org/hwo/text/LineReader.java new file mode 100644 index 0000000..77aacc1 --- /dev/null +++ b/src/org/hwo/text/LineReader.java @@ -0,0 +1,48 @@ +package org.hwo.text; + + +public class LineReader { + + private char[] buffer; + private int position; + + + public LineReader() + { + position = 0; + } + + public LineReader(String line) + { + position = 0; + setText(line); + } + + public void setText(String text){ + position = 0; + buffer = (text == null) ? new char[0] : text.toCharArray(); + } + public String getText(){ + return new String(buffer); + } + + public char peek(){ + return buffer[position]; + } + public char peek(int offset){ + return buffer[position + offset]; + } + + public char read(){ + return buffer[position++]; + } + + public void seek(int pos){ + position = pos; + } + + public boolean endOfLine(){ + return (position >= buffer.length); + } + +} diff --git a/src/org/hwo/ui/JObjectSelector.java b/src/org/hwo/ui/JObjectSelector.java new file mode 100644 index 0000000..95ee05c --- /dev/null +++ b/src/org/hwo/ui/JObjectSelector.java @@ -0,0 +1,134 @@ +package org.hwo.ui; + +import java.awt.BorderLayout; +import java.awt.FlowLayout; + +import javax.swing.DefaultListModel; +import javax.swing.JButton; +import javax.swing.JDialog; +import javax.swing.JPanel; +import javax.swing.border.EmptyBorder; + +import java.awt.GridBagLayout; + +import javax.swing.JLabel; + +import java.awt.GridBagConstraints; + +import javax.swing.JList; + +import java.awt.Insets; +import java.awt.event.ActionListener; +import java.awt.event.ActionEvent; +import java.awt.Dialog.ModalExclusionType; +import java.awt.Dialog.ModalityType; + +public class JObjectSelector extends JDialog { + + public static Object execute(Object[] items){ + + JObjectSelector os = new JObjectSelector(); + os.setItems(items); + os.setVisible(true); + return os.selectedItem; + } + + + private final JPanel contentPanel = new JPanel(); + private JList lItems; + + private Object[] items; + private Object selectedItem; + private JLabel lText; + + /** + * Create the dialog. + */ + public JObjectSelector() { + setModalityType(ModalityType.APPLICATION_MODAL); + setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE); + setTitle("Auswahl\u2026"); + setBounds(100, 100, 450, 300); + getContentPane().setLayout(new BorderLayout()); + contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); + getContentPane().add(contentPanel, BorderLayout.CENTER); + GridBagLayout gbl_contentPanel = new GridBagLayout(); + gbl_contentPanel.columnWidths = new int[]{0, 0}; + gbl_contentPanel.rowHeights = new int[]{0, 0, 0}; + gbl_contentPanel.columnWeights = new double[]{1.0, Double.MIN_VALUE}; + gbl_contentPanel.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE}; + contentPanel.setLayout(gbl_contentPanel); + { + lText = new JLabel("-"); + GridBagConstraints gbc_lText = new GridBagConstraints(); + gbc_lText.insets = new Insets(0, 0, 5, 0); + gbc_lText.gridx = 0; + gbc_lText.gridy = 0; + contentPanel.add(lText, gbc_lText); + } + { + lItems = new JList(); + GridBagConstraints gbc_lItems = new GridBagConstraints(); + gbc_lItems.fill = GridBagConstraints.BOTH; + gbc_lItems.gridx = 0; + gbc_lItems.gridy = 1; + contentPanel.add(lItems, gbc_lItems); + } + { + JPanel buttonPane = new JPanel(); + buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); + getContentPane().add(buttonPane, BorderLayout.SOUTH); + { + JButton okButton = new JButton("OK"); + okButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + accept(); + } + }); + okButton.setActionCommand("OK"); + buttonPane.add(okButton); + getRootPane().setDefaultButton(okButton); + } + { + JButton cancelButton = new JButton("Cancel"); + cancelButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + cancel(); + } + }); + cancelButton.setActionCommand("Cancel"); + buttonPane.add(cancelButton); + } + } + } + + public void accept() + { + selectedItem = lItems.getSelectedValue(); + setVisible(false); + } + + public void cancel(){ + setVisible(false); + } + + public void setItems(Object[] items) { + this.items = items; + DefaultListModel dlm = new DefaultListModel(); + for (Object o:items){ + dlm.addElement(o); + } + lItems.setModel(dlm); + } + public Object[] getItems() { + return items; + } + + public void setText(String text){ + this.lText.setText(text); + } + public String getText(){ + return this.lText.getText(); + } + +} diff --git a/src/org/hwo/ui/dropdown.png b/src/org/hwo/ui/dropdown.png new file mode 100644 index 0000000..d4816e9 Binary files /dev/null and b/src/org/hwo/ui/dropdown.png differ