forked from LupusNobilis/java-org.hwo
Div. Updates
parent
c6f2f57919
commit
8dbb668a26
Binary file not shown.
Binary file not shown.
|
@ -25,5 +25,14 @@ public class StringHelper {
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String fromCharacters(Character[] chars){
|
||||||
|
char[] chs = new char[chars.length];
|
||||||
|
|
||||||
|
for (int i=0;i<chars.length;i++)
|
||||||
|
chs[i] = chars[i].charValue();
|
||||||
|
|
||||||
|
return new String(chs);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ import java.util.UUID;
|
||||||
|
|
||||||
import javax.net.SocketFactory;
|
import javax.net.SocketFactory;
|
||||||
|
|
||||||
public class Beacon extends Thread{
|
public class Beacon {
|
||||||
|
|
||||||
public class BeaconSender
|
public class BeaconSender
|
||||||
{
|
{
|
||||||
|
@ -28,6 +28,7 @@ public class Beacon extends Thread{
|
||||||
|
|
||||||
private Long lastBeacon;
|
private Long lastBeacon;
|
||||||
|
|
||||||
|
|
||||||
public BeaconSender()
|
public BeaconSender()
|
||||||
{
|
{
|
||||||
properties = new Properties();
|
properties = new Properties();
|
||||||
|
@ -58,6 +59,10 @@ public class Beacon extends Thread{
|
||||||
this.lastBeacon = lastBeacon;
|
this.lastBeacon = lastBeacon;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return String.format("%s", getProperties().getProperty("beacon.name"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,6 +101,10 @@ public class Beacon extends Thread{
|
||||||
private Integer intervall;
|
private Integer intervall;
|
||||||
private String beaconName;
|
private String beaconName;
|
||||||
private UUID uuid;
|
private UUID uuid;
|
||||||
|
private boolean serverOnly;
|
||||||
|
private boolean clientOnly;
|
||||||
|
|
||||||
|
private Thread thread;
|
||||||
|
|
||||||
private Hashtable<String, BeaconSender> beaconSenders;
|
private Hashtable<String, BeaconSender> beaconSenders;
|
||||||
|
|
||||||
|
@ -106,34 +115,87 @@ public class Beacon extends Thread{
|
||||||
this.properties = new Properties();
|
this.properties = new Properties();
|
||||||
this.intervall = 5000;
|
this.intervall = 5000;
|
||||||
this.beaconSenders = new Hashtable<String, Beacon.BeaconSender>();
|
this.beaconSenders = new Hashtable<String, Beacon.BeaconSender>();
|
||||||
this.beaconName = "DefaultBeacon";
|
|
||||||
this.uuid = UUID.randomUUID();
|
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()
|
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;
|
return exit;
|
||||||
}
|
}
|
||||||
|
public void setExit(Boolean exit){
|
||||||
|
this.exit = exit;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
||||||
Timer pingTimer = new Timer();
|
Timer pingTimer = new Timer();
|
||||||
pingTimer.schedule(new TimerTask() {
|
if (!clientOnly){
|
||||||
|
pingTimer.schedule(new TimerTask() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
ping();
|
ping();
|
||||||
}
|
}
|
||||||
}, intervall, intervall);
|
}, intervall, intervall);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
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);
|
DatagramPacket packet = new DatagramPacket(new byte[1500], 1500);
|
||||||
|
|
||||||
while (!getExit())
|
while (!getExit())
|
||||||
|
@ -163,7 +225,6 @@ public class Beacon extends Thread{
|
||||||
};
|
};
|
||||||
|
|
||||||
} catch (IOException e) {
|
} 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()
|
public void ping()
|
||||||
{
|
{
|
||||||
DatagramSocket sock = socket;
|
DatagramSocket sock = socket;
|
||||||
|
|
|
@ -17,50 +17,117 @@ import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
|
|
||||||
|
import org.hwo.StringHelper;
|
||||||
|
import org.hwo.text.LineReader;
|
||||||
|
|
||||||
import javassist.bytecode.EnclosingMethodAttribute;
|
import javassist.bytecode.EnclosingMethodAttribute;
|
||||||
|
|
||||||
public class CSV {
|
public class CSV {
|
||||||
|
|
||||||
List<String[]> records;
|
List<CSVRecord> records;
|
||||||
|
|
||||||
private char separator;
|
private char separator;
|
||||||
private char enclosedBy;
|
private char enclosedBy;
|
||||||
|
|
||||||
boolean isEnclosed;
|
|
||||||
List<String> parserRecord;
|
|
||||||
List<Character> parserField;
|
|
||||||
char lastChar;
|
|
||||||
|
|
||||||
public CSV()
|
public CSV()
|
||||||
{
|
{
|
||||||
this.records = new LinkedList<String[]>();
|
this.records = new LinkedList<CSVRecord>();
|
||||||
this.setSeparator(';');
|
this.setSeparator(';');
|
||||||
this.setEnclosedBy('\"');
|
this.setEnclosedBy('\"');
|
||||||
|
|
||||||
this.parserRecord = new ArrayList<String>();
|
|
||||||
this.parserField = new ArrayList<Character>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void nextRecord()
|
public void readFromFile(String filename)
|
||||||
{
|
{
|
||||||
nextField();
|
try {
|
||||||
|
readFromStream(new FileInputStream(filename));
|
||||||
this.records.add(parserRecord.toArray(new String[]{}));
|
} catch (FileNotFoundException e) {
|
||||||
parserRecord.clear();
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void nextField()
|
public void readFromStream(InputStream is) throws IOException
|
||||||
{
|
{
|
||||||
char fieldChars[] = new char[ parserField.size() ];
|
this.readFromStream(new InputStreamReader(is,"ISO-8859-1"));
|
||||||
|
|
||||||
for (int i=0;i<parserField.size(); i++)
|
|
||||||
fieldChars[i] = parserField.get(i);
|
|
||||||
|
|
||||||
parserRecord.add(String.valueOf(fieldChars));
|
|
||||||
parserField.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void feeder(char ch)
|
public void readFromStream(InputStreamReader isr) throws IOException
|
||||||
|
{
|
||||||
|
BufferedReader br = new BufferedReader(isr);
|
||||||
|
|
||||||
|
records.clear();
|
||||||
|
|
||||||
|
while (br.ready()) {
|
||||||
|
CSVRecord row = textToRow(br.readLine());
|
||||||
|
records.add(row);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object textToObject(String text){
|
||||||
|
|
||||||
|
if (text.indexOf('.')>=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<Character> 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<Character>();
|
||||||
|
|
||||||
|
} 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<Character>();
|
||||||
|
}
|
||||||
|
cell.add(ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return record;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* private void feeder(char ch)
|
||||||
{
|
{
|
||||||
if (ch == getEnclosedBy())
|
if (ch == getEnclosedBy())
|
||||||
{
|
{
|
||||||
|
@ -90,36 +157,7 @@ public class CSV {
|
||||||
lastChar = ch;
|
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)
|
public void saveToFile(String filename)
|
||||||
{
|
{
|
||||||
saveToFile(new File(filename));
|
saveToFile(new File(filename));
|
||||||
|
@ -154,21 +192,21 @@ public class CSV {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
for (String[] record:this.records)
|
for (CSVRecord record:this.records)
|
||||||
{
|
{
|
||||||
for (int i=0;i<record.length;i++)
|
for (int i=0;i<record.size();i++)
|
||||||
{
|
{
|
||||||
if (i>0)
|
if (i>0)
|
||||||
writer.write(separator);
|
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(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);
|
writer.write(enclosedBy);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -184,35 +222,28 @@ public class CSV {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getValue(int row,int column)
|
public Object getValue(int row,int column)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return this.records.get(row)[column];
|
return this.records.get(row).getValue(column).toString();
|
||||||
} catch (ArrayIndexOutOfBoundsException ex)
|
} catch (ArrayIndexOutOfBoundsException ex)
|
||||||
{
|
{
|
||||||
return "";
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String[]> getCells()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public List<CSVRecord> getRecords()
|
||||||
{
|
{
|
||||||
return this.records;
|
return this.records;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Object[]> getCellsAsObjects()
|
public CSVRecord getRecord(int row){
|
||||||
{
|
return records.get(row);
|
||||||
List<Object[]> ol = new ArrayList<Object[]>();
|
|
||||||
|
|
||||||
for (String[] record:records)
|
|
||||||
{
|
|
||||||
Object[] ro = new Object[record.length];
|
|
||||||
for (int i=0;i<ro.length;i++)
|
|
||||||
ro[i] = record[i];
|
|
||||||
ol.add(ro);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ol;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public char getSeparator() {
|
public char getSeparator() {
|
||||||
|
|
|
@ -0,0 +1,51 @@
|
||||||
|
package org.hwo.csv;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class CSVRecord {
|
||||||
|
|
||||||
|
ArrayList<Object> columns;
|
||||||
|
|
||||||
|
public CSVRecord(){
|
||||||
|
columns = new ArrayList<Object>();
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -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<Plot> plots;
|
||||||
|
|
||||||
|
|
||||||
|
public Diagram(){
|
||||||
|
plots = new LinkedList<Plot>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Plot newPlot(){
|
||||||
|
Plot p = new Plot(this);
|
||||||
|
this.plots.add(p);
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public BufferedImage plot(){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -11,19 +11,32 @@ import java.util.concurrent.TimeoutException;
|
||||||
import org.hwo.fifo.FiFo;
|
import org.hwo.fifo.FiFo;
|
||||||
import org.hwo.nativeloader.NativeLoader;
|
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 {
|
public class NativeSerialPort extends SerialPort {
|
||||||
|
|
||||||
class SerialPortInputStream extends InputStream
|
class SerialPortInputStream extends InputStream
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
public int read() throws IOException {
|
public int read() throws SerialPortExeption {
|
||||||
if (!isOpen())
|
if (!isOpen())
|
||||||
throw new IOException("Port nicht gešffnet!");
|
throw new SerialPortExeption(-6);
|
||||||
|
|
||||||
int ch = native_getch(nativeHandle,getTimeout());
|
int ch = native_getch(nativeHandle,getTimeout());
|
||||||
//System.err.println(String.format("RX: 0x%08x", ch));
|
|
||||||
if (ch < 0)
|
if (ch < 0)
|
||||||
throw new IOException(String.format("native_getch: returned: %d",ch));
|
throw new SerialPortExeption(ch);
|
||||||
return ch;
|
return ch;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,10 +45,9 @@ public class NativeSerialPort extends SerialPort {
|
||||||
class SerialPortOutputStream extends OutputStream
|
class SerialPortOutputStream extends OutputStream
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
public void write(int arg0) throws IOException {
|
public void write(int arg0) throws SerialPortExeption {
|
||||||
if (!isOpen())
|
if (!isOpen())
|
||||||
throw new IOException("Port nicht gešffnet!");
|
throw new SerialPortExeption(-6);
|
||||||
//System.err.println(String.format("TX: 0x%08x", arg0));
|
|
||||||
native_putch(nativeHandle, getTimeout(), arg0);
|
native_putch(nativeHandle, getTimeout(), arg0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -72,7 +84,7 @@ public class NativeSerialPort extends SerialPort {
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
nativeHandle = native_open(getPortName());
|
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)
|
if (nativeHandle < 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ public abstract class SerialPort {
|
||||||
protected SerialPort()
|
protected SerialPort()
|
||||||
{
|
{
|
||||||
this.portName = "";
|
this.portName = "";
|
||||||
this.timeout = -1;
|
this.timeout = 2500;
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract public boolean isOpen();
|
abstract public boolean isOpen();
|
||||||
|
|
|
@ -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));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -19,6 +19,7 @@ import javax.swing.event.ListSelectionListener;
|
||||||
import javax.swing.table.AbstractTableModel;
|
import javax.swing.table.AbstractTableModel;
|
||||||
|
|
||||||
import org.hwo.csv.CSV;
|
import org.hwo.csv.CSV;
|
||||||
|
import org.hwo.csv.CSVRecord;
|
||||||
import org.hwo.interactiveobjects.InteractiveObjectHelper;
|
import org.hwo.interactiveobjects.InteractiveObjectHelper;
|
||||||
import org.hwo.ui.KeyStrokeHelper;
|
import org.hwo.ui.KeyStrokeHelper;
|
||||||
import org.hwo.ui.KeyStrokeListener;
|
import org.hwo.ui.KeyStrokeListener;
|
||||||
|
@ -637,23 +638,23 @@ public class TableMapper extends AbstractTableModel
|
||||||
{
|
{
|
||||||
CSV csv = new CSV();
|
CSV csv = new CSV();
|
||||||
|
|
||||||
List<String[]> cells = csv.getCells();
|
List<CSVRecord> cells = csv.getRecords();
|
||||||
|
|
||||||
String[] header = new String[ getColumnCount() ];
|
CSVRecord header = new CSVRecord();
|
||||||
for (int i=0;i<getColumnCount();i++)
|
for (int i=0;i<getColumnCount();i++)
|
||||||
header[i] = p_columns.get(i).getLabel();
|
header.appendValue(p_columns.get(i).getLabel());
|
||||||
|
|
||||||
cells.add( header );
|
cells.add(header);
|
||||||
|
|
||||||
for (int row=0;row<getRowCount();row++)
|
for (int row=0;row<getRowCount();row++)
|
||||||
{
|
{
|
||||||
String[] record = new String[ getColumnCount() ];
|
CSVRecord record = new CSVRecord();
|
||||||
|
|
||||||
for (int column=0;column<getColumnCount();column++)
|
for (int column=0;column<getColumnCount();column++)
|
||||||
if (getValueAt(row, column)!=null)
|
if (getValueAt(row, column)!=null)
|
||||||
record[column] = getValueAt(row, column).toString();
|
record.appendValue(getValueAt(row, column).toString());
|
||||||
else
|
else
|
||||||
record[column] = "";
|
record.appendValue("");
|
||||||
|
|
||||||
cells.add(record);
|
cells.add(record);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,29 +4,26 @@ import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.io.ObjectInputStream.GetField;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
import javax.management.RuntimeErrorException;
|
import javax.management.RuntimeErrorException;
|
||||||
|
|
||||||
import org.hwo.os.OsDetect;
|
import org.hwo.os.OsDetect;
|
||||||
|
import org.hwo.os.OsDetect.Bitness;
|
||||||
|
import org.hwo.os.OsDetect.OsType;
|
||||||
|
|
||||||
public class NativeLoader {
|
public class NativeLoader {
|
||||||
|
|
||||||
public static void loadLibrary(String libName)
|
private static boolean patchedLinux = false;
|
||||||
{
|
|
||||||
try
|
private static String calculateLibraryName(String libName){
|
||||||
{
|
|
||||||
System.loadLibrary(libName);
|
|
||||||
return;
|
|
||||||
} catch (UnsatisfiedLinkError e)
|
|
||||||
{
|
|
||||||
System.err.println("Native Library not found, trying to load from JAR-File");
|
|
||||||
System.err.println(e);
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
String platformLibName;
|
String platformLibName;
|
||||||
String resourcePath;
|
|
||||||
|
if (OsDetect.getBitness()==Bitness.B32){
|
||||||
|
libName = libName + "32";
|
||||||
|
}
|
||||||
|
|
||||||
switch (OsDetect.getOperatingSystem())
|
switch (OsDetect.getOperatingSystem())
|
||||||
{
|
{
|
||||||
|
@ -40,9 +37,43 @@ public class NativeLoader {
|
||||||
platformLibName = String.format("%s.dll",libName);
|
platformLibName = String.format("%s.dll",libName);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new RuntimeException("Betriebssystem nicht unterstŸtzt!");
|
throw new RuntimeException("Betriebssystem nicht unterst<EFBFBD>tzt!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return platformLibName;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void loadLibrary(String libName)
|
||||||
|
{
|
||||||
|
if (OsDetect.getOperatingSystem()==OsType.LINUX){
|
||||||
|
System.err.println("Linux erkannt. Patche java.library.path");
|
||||||
|
System.setProperty("java.library.path",String.format(".:%s",System.getProperty("java.library.path")));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
System.loadLibrary(libName);
|
||||||
|
return;
|
||||||
|
} catch (UnsatisfiedLinkError e)
|
||||||
|
{
|
||||||
|
System.err.println("Native Library " + libName + " failed to load.");
|
||||||
|
}
|
||||||
|
|
||||||
|
String platformLibName = calculateLibraryName(libName);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
System.loadLibrary("./"+platformLibName);
|
||||||
|
return;
|
||||||
|
} catch (UnsatisfiedLinkError e)
|
||||||
|
{
|
||||||
|
System.err.println("Native Library " + platformLibName + " failed to load.");
|
||||||
|
}
|
||||||
|
|
||||||
|
String resourcePath;
|
||||||
|
|
||||||
System.err.println(String.format("NativeLoader: Platform dependent Name: %s",platformLibName));
|
System.err.println(String.format("NativeLoader: Platform dependent Name: %s",platformLibName));
|
||||||
|
|
||||||
resourcePath = "/native/" + platformLibName;
|
resourcePath = "/native/" + platformLibName;
|
||||||
|
|
|
@ -3,6 +3,7 @@ package org.hwo.os;
|
||||||
public class OsDetect {
|
public class OsDetect {
|
||||||
|
|
||||||
public enum OsType { UNKNOWN, LINUX, OSX, WINDOWS};
|
public enum OsType { UNKNOWN, LINUX, OSX, WINDOWS};
|
||||||
|
public enum Bitness { B32, B64 };
|
||||||
|
|
||||||
public static OsType getOperatingSystem()
|
public static OsType getOperatingSystem()
|
||||||
{
|
{
|
||||||
|
@ -16,4 +17,21 @@ public class OsDetect {
|
||||||
return OsType.UNKNOWN;
|
return OsType.UNKNOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Bitness getBitness(){
|
||||||
|
String arch = System.getProperty("os.arch");
|
||||||
|
System.err.println("Arch: " + arch);
|
||||||
|
if (arch == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (arch.equals("x86")){
|
||||||
|
return Bitness.B32;
|
||||||
|
} else if (arch.equals("i386")){
|
||||||
|
return Bitness.B32;
|
||||||
|
} else if (arch.equals("xmd64")){
|
||||||
|
return Bitness.B64;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
package org.hwo.rpc.json;
|
||||||
|
|
||||||
|
|
||||||
|
public class JSONRpcServer {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -1,6 +1,10 @@
|
||||||
package org.hwo.rpc.json;
|
package org.hwo.rpc.json;
|
||||||
|
|
||||||
|
import java.awt.peer.TextComponentPeer;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
@ -9,6 +13,8 @@ import java.lang.reflect.InvocationHandler;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Proxy;
|
import java.lang.reflect.Proxy;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.Socket;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -21,6 +27,9 @@ import org.json.simple.JSONValue;
|
||||||
public class JSONRpcService implements InvocationHandler{
|
public class JSONRpcService implements InvocationHandler{
|
||||||
|
|
||||||
private String url;
|
private String url;
|
||||||
|
private InetAddress inetAddress;
|
||||||
|
private int port;
|
||||||
|
|
||||||
private Integer nextid;
|
private Integer nextid;
|
||||||
|
|
||||||
public JSONRpcService(String url){
|
public JSONRpcService(String url){
|
||||||
|
@ -28,11 +37,69 @@ public class JSONRpcService implements InvocationHandler{
|
||||||
this.nextid = 0;
|
this.nextid = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public JSONRpcService(InetAddress inetAddress,int port){
|
||||||
|
this.inetAddress = inetAddress;
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
public <T> T createProxy(Class<T> iface){
|
public <T> T createProxy(Class<T> iface){
|
||||||
T proxy = (T) Proxy.newProxyInstance(iface.getClassLoader(), new Class[]{ iface }, this);
|
T proxy = (T) Proxy.newProxyInstance(iface.getClassLoader(), new Class[]{ iface }, this);
|
||||||
return proxy;
|
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
|
@Override
|
||||||
public Object invoke(Object proxy, Method method, Object[] args)
|
public Object invoke(Object proxy, Method method, Object[] args)
|
||||||
throws Throwable {
|
throws Throwable {
|
||||||
|
@ -43,29 +110,13 @@ public class JSONRpcService implements InvocationHandler{
|
||||||
req.put("params", Arrays.asList(args));
|
req.put("params", Arrays.asList(args));
|
||||||
|
|
||||||
String reqString = JSONValue.toJSONString(req);
|
String reqString = JSONValue.toJSONString(req);
|
||||||
|
|
||||||
System.err.println(reqString);
|
System.err.println(reqString);
|
||||||
|
|
||||||
URL url = new URL(this.url);
|
byte[] responseBytes = request( reqString.getBytes() );
|
||||||
URLConnection conn = url.openConnection();
|
|
||||||
conn.setDoInput(true);
|
|
||||||
conn.setDoOutput(true);
|
|
||||||
OutputStream out = conn.getOutputStream();
|
|
||||||
out.write(reqString.getBytes());
|
|
||||||
out.flush();
|
|
||||||
out.close();
|
|
||||||
|
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader( conn.getInputStream()));
|
System.err.println("Response: " + new String(responseBytes));
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
String line;
|
|
||||||
|
|
||||||
while ((line = reader.readLine())!=null) {
|
Map<String, Object> response = (Map<String, Object>)JSONValue.parse(new String(responseBytes));
|
||||||
sb.append(line);
|
|
||||||
}
|
|
||||||
|
|
||||||
System.err.println("Response: " + sb.toString());
|
|
||||||
|
|
||||||
Map<String, Object> response = (Map<String, Object>)JSONValue.parse(sb.toString());
|
|
||||||
|
|
||||||
if (response.containsKey("error") && (response.get("error") != null)) {
|
if (response.containsKey("error") && (response.get("error") != null)) {
|
||||||
throw new RuntimeException("JSON-RPC: " + response.get("error"));
|
throw new RuntimeException("JSON-RPC: " + response.get("error"));
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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<String, Object> registeredObjects;
|
||||||
|
private ServerSocket serverSocket;
|
||||||
|
|
||||||
|
private boolean exit;
|
||||||
|
|
||||||
|
public SimpleRPCServer(InetAddress listenAddress,int port) throws IOException
|
||||||
|
{
|
||||||
|
this.registeredObjects = new Hashtable<String, Object>();
|
||||||
|
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<params.length; i++){
|
||||||
|
paramTypes[i] = params[i].getClass();
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
paramTypes = new Class<?>[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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -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> T createProxy(Class<T> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Binary file not shown.
After Width: | Height: | Size: 1.1 KiB |
Loading…
Reference in New Issue