Mercurial Hosting > luan
comparison src/luan/lib/PickleClient.java @ 129:486a0641bca4
add pickle client/server;
fix parser bugs;
git-svn-id: https://luan-java.googlecode.com/svn/trunk@130 21e917c8-12df-6dd8-5cb6-c86387c605b9
| author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
|---|---|
| date | Mon, 09 Jun 2014 09:16:16 +0000 |
| parents | |
| children | 0594c132888b |
comparison
equal
deleted
inserted
replaced
| 128:f0a4abe58593 | 129:486a0641bca4 |
|---|---|
| 1 package luan.lib; | |
| 2 | |
| 3 import java.io.DataInputStream; | |
| 4 import java.io.DataOutputStream; | |
| 5 import java.io.IOException; | |
| 6 import luan.Luan; | |
| 7 import luan.LuanState; | |
| 8 import luan.LuanException; | |
| 9 import luan.LuanTable; | |
| 10 import luan.LuanJavaFunction; | |
| 11 | |
| 12 | |
| 13 public final class PickleClient { | |
| 14 | |
| 15 private final PickleCon con; | |
| 16 | |
| 17 PickleClient(LuanState luan,DataInputStream in,DataOutputStream out) { | |
| 18 con = new PickleCon(luan,in,out); | |
| 19 } | |
| 20 | |
| 21 public Object call(Object... args) throws LuanException, IOException { | |
| 22 con.write(args); | |
| 23 Object[] result = Luan.array(con.read()); | |
| 24 boolean ok = (boolean)result[0]; | |
| 25 if( ok ) { | |
| 26 Object[] rtn = new Object[result.length-1]; | |
| 27 System.arraycopy(result,1,rtn,0,rtn.length); | |
| 28 return rtn; | |
| 29 } else { | |
| 30 String msg = (String)result[1]; | |
| 31 String src = (String)result[2]; | |
| 32 throw con.luan.JAVA.exception( | |
| 33 msg + "\n" | |
| 34 + "in:\n" | |
| 35 + "------------------\n" | |
| 36 + src + "\n" | |
| 37 + "------------------\n" | |
| 38 ); | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 LuanTable table() { | |
| 43 LuanTable tbl = new LuanTable(); | |
| 44 try { | |
| 45 tbl.put( "pickle", new LuanJavaFunction( | |
| 46 PickleCon.class.getMethod( "pickle", Object.class ), con | |
| 47 ) ); | |
| 48 tbl.put( "call", new LuanJavaFunction( | |
| 49 PickleClient.class.getMethod( "call", new Object[0].getClass() ), this | |
| 50 ) ); | |
| 51 tbl.put( "close", new LuanJavaFunction( | |
| 52 PickleCon.class.getMethod( "close" ), con | |
| 53 ) ); | |
| 54 } catch(NoSuchMethodException e) { | |
| 55 throw new RuntimeException(e); | |
| 56 } | |
| 57 return tbl; | |
| 58 } | |
| 59 | |
| 60 } |
