Mercurial Hosting > luan
changeset 611:8bd98da6991a 0.14
remove pickle code
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 11 Dec 2015 12:24:24 -0700 |
parents | b4f3dbe1c6e3 |
children | bb9a790d8401 |
files | core/src/luan/modules/IoLuan.java core/src/luan/modules/PickleClient.java core/src/luan/modules/PickleCon.java core/src/luan/modules/PickleServer.java |
diffstat | 4 files changed, 0 insertions(+), 419 deletions(-) [+] |
line wrap: on
line diff
diff -r b4f3dbe1c6e3 -r 8bd98da6991a core/src/luan/modules/IoLuan.java --- a/core/src/luan/modules/IoLuan.java Fri Dec 11 00:13:13 2015 -0700 +++ b/core/src/luan/modules/IoLuan.java Fri Dec 11 12:24:24 2015 -0700 @@ -647,33 +647,6 @@ @Override public String to_string() { return socket.toString(); } - - public LuanTable pickle_client(LuanState luan) throws IOException { - InputStream in = new BufferedInputStream(inputStream()); - OutputStream out = new BufferedOutputStream(outputStream()); - return new PickleClient(luan,in,out).table(); - } - - public void run_pickle_server(LuanState luan) throws IOException, LuanException { - InputStream in = new BufferedInputStream(inputStream()); - OutputStream out = new BufferedOutputStream(outputStream()); - new PickleServer(luan,in,out).run(); - } - - @Override public LuanTable table() { - LuanTable tbl = super.table(); - try { - tbl.rawPut( "pickle_client", new LuanJavaFunction( - LuanSocket.class.getMethod( "pickle_client", LuanState.class ), this - ) ); - tbl.rawPut( "run_pickle_server", new LuanJavaFunction( - LuanSocket.class.getMethod( "run_pickle_server", LuanState.class ), this - ) ); - } catch(NoSuchMethodException e) { - throw new RuntimeException(e); - } - return tbl; - } } public static LuanTable socket(LuanState luan,String name) throws LuanException, IOException {
diff -r b4f3dbe1c6e3 -r 8bd98da6991a core/src/luan/modules/PickleClient.java --- a/core/src/luan/modules/PickleClient.java Fri Dec 11 00:13:13 2015 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,106 +0,0 @@ -package luan.modules; - -import java.io.InputStream; -import java.io.OutputStream; -import java.io.IOException; -import luan.Luan; -import luan.LuanState; -import luan.LuanException; -import luan.LuanTable; -import luan.LuanJavaFunction; -import luan.LuanFunction; - - -public final class PickleClient { - - private final PickleCon con; - private final LuanFunction _reversed_pickle; - - PickleClient(LuanState luan,InputStream in,OutputStream out) { - this(new PickleCon(luan,in,out)); - } - - PickleClient(PickleCon con) { - this.con = con; - try { - this._reversed_pickle = new LuanJavaFunction( - PickleClient.class.getMethod( "_reversed_pickle" ), this - ); - } catch(NoSuchMethodException e) { - throw new RuntimeException(e); - } - } - - public Object _reversed_pickle() throws LuanException, IOException { - new PickleServer(con).run(); - return con.read(); - } - - public Object call(Object... args) throws LuanException, IOException { - con.write(args); - Object[] result; - con.env.rawPut("_reversed_pickle",_reversed_pickle); - try { - result = Luan.array(con.read()); - } finally { - con.env.rawPut("_reversed_pickle",null); - } - boolean ok = (boolean)result[0]; - if( ok ) { - Object[] rtn = new Object[result.length-1]; - System.arraycopy(result,1,rtn,0,rtn.length); - return rtn; - } else { - String msg = (String)result[1]; - String src = (String)result[2]; - throw new LuanException(con.luan, - msg + "\n" - + "in:\n" - + "------------------\n" - + formatCode(src) + "\n" - + "------------------\n" - ); - } - } - - LuanTable table() { - LuanTable tbl = new LuanTable(); - try { - tbl.rawPut( "pickle", new LuanJavaFunction( - PickleCon.class.getMethod( "pickle", Object.class ), con - ) ); - tbl.rawPut( "call", new LuanJavaFunction( - PickleClient.class.getMethod( "call", new Object[0].getClass() ), this - ) ); - tbl.rawPut( "close", new LuanJavaFunction( - PickleCon.class.getMethod( "close" ), con - ) ); - } catch(NoSuchMethodException e) { - throw new RuntimeException(e); - } - return tbl; - } - - - public static String formatCode(String s) { - StringBuilder buf = new StringBuilder(); - int line = 1; - int i = 0; - int i2 = 0; - while( i2 != -1 ) { - buf.append( line++ ); - buf.append( '\t' ); - i2 = s.indexOf('\n',i); - String lineStr = i2 == -1 ? s.substring(i) : s.substring(i,i2+1); - int j; - for( j=0; j<lineStr.length() && lineStr.charAt(j)=='\t'; j++ ) { - buf.append( " " ); - } - buf.append( lineStr.substring(j) ); - i = i2 + 1; - } - return buf.toString(); - } - - -}
diff -r b4f3dbe1c6e3 -r 8bd98da6991a core/src/luan/modules/PickleCon.java --- a/core/src/luan/modules/PickleCon.java Fri Dec 11 00:13:13 2015 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,165 +0,0 @@ -package luan.modules; - -import java.io.InputStream; -import java.io.OutputStream; -import java.io.IOException; -import java.io.EOFException; -import java.nio.charset.StandardCharsets; -import java.util.Set; -import java.util.IdentityHashMap; -import java.util.Collections; -import java.util.Map; -import java.util.List; -import java.util.ArrayList; -import luan.Luan; -import luan.LuanTable; -import luan.LuanState; -import luan.LuanFunction; -import luan.LuanJavaFunction; -import luan.LuanException; - - -public final class PickleCon { - final LuanState luan; - private final InputStream in; - private final LuanFunction _read_binary; - private final OutputStream out; - private final List<byte[]> binaries = new ArrayList<byte[]>(); - String src; - final LuanTable env = new LuanTable(); - - PickleCon(LuanState luan,InputStream in,OutputStream out) { - this.in = in; - this.luan = luan; - try { - this._read_binary = new LuanJavaFunction( - PickleCon.class.getMethod( "_read_binary", Integer.TYPE ), this - ); - } catch(NoSuchMethodException e) { - throw new RuntimeException(e); - } - - this.out = out; - } - - public byte[] _read_binary(int size) throws IOException/*, LuanException*/ { - byte[] a = new byte[size]; - int i = 0; - while( i < size ) { - int n = in.read(a,i,size-i); - if( n == -1 ) -// throw new LuanException(luan, "end of stream" ); - throw new EOFException(); - i += n; - } - return a; - } - - public Object read() throws IOException, LuanException { - env.rawPut("_read_binary",_read_binary); - try { - src = readString(); - LuanFunction fn = BasicLuan.load(luan,src,"pickle-reader",env,false); - return fn.call(luan); - } finally { - env.rawPut("_binaries",null); - env.rawPut("_read_binary",null); - } - } - - public String pickle(Object obj) throws LuanException { - if( obj == null ) - return "nil"; - if( obj instanceof Boolean ) - return obj.toString(); - if( obj instanceof Number ) - return Luan.toString((Number)obj); - if( obj instanceof String ) - return "\"" + Luan.stringEncode((String)obj) + "\""; - if( obj instanceof LuanTable ) - return pickle( (LuanTable)obj, Collections.newSetFromMap(new IdentityHashMap<LuanTable,Boolean>()) ); - if( obj instanceof byte[] ) { - byte[] a = (byte[])obj; - binaries.add(a); - return "_binaries[" + binaries.size() + "]"; - } - throw new LuanException(luan, "invalid type: " + obj.getClass() ); - } - - private String pickle(Object obj,Set<LuanTable> set) throws LuanException { - return obj instanceof LuanTable ? pickle((LuanTable)obj,set) : pickle(obj); - } - - private String pickle(LuanTable tbl,Set<LuanTable> set) throws LuanException { - if( !set.add(tbl) ) { - throw new LuanException(luan, "circular reference in table" ); - } - StringBuilder sb = new StringBuilder(); - sb.append( "{" ); - for( Map.Entry<Object,Object> entry : tbl.iterable(luan) ) { - sb.append( "[" ); - sb.append( pickle(entry.getKey(),set) ); - sb.append( "]=" ); - sb.append( pickle(entry.getValue(),set) ); - sb.append( ", " ); - } - sb.append( "}" ); - return sb.toString(); - } - - public void write(Object... args) throws LuanException, IOException { - StringBuilder sb = new StringBuilder(); - if( !binaries.isEmpty() ) { - sb.append( "_binaries = {}\n" ); - for( byte[] a : binaries ) { - sb.append( "_binaries[#_binaries+1] = _read_binary(" + a.length + ")\n" ); - } - } - for( Object obj : args ) { - sb.append( luan.toString(obj) ); - } - writeString( sb.toString() ); -//System.out.println("aaaaaaaaaaaaaaaaaaaaaaaa"); -//System.out.println(sb); -//System.out.println("zzzzzzzzzzzzzzzzzzzzzzzz"); - for( byte[] a : binaries ) { - out.write(a); - } - out.flush(); - binaries.clear(); - } - - public void close() throws IOException { - in.close(); - out.close(); - } - - String readString() throws IOException { - int len = readInt(); - byte[] a = _read_binary(len); - return new String(a,StandardCharsets.UTF_8); - } - - int readInt() throws IOException { - int ch1 = in.read(); - int ch2 = in.read(); - int ch3 = in.read(); - int ch4 = in.read(); - if ((ch1 | ch2 | ch3 | ch4) < 0) - throw new EOFException(); - return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); - } - - void writeString(String s) throws IOException { - byte[] a = s.getBytes(StandardCharsets.UTF_8); - writeInt(a.length); - out.write(a); - } - - void writeInt(int v) throws IOException { - out.write((v >>> 24) & 0xFF); - out.write((v >>> 16) & 0xFF); - out.write((v >>> 8) & 0xFF); - out.write((v >>> 0) & 0xFF); - } -}
diff -r b4f3dbe1c6e3 -r 8bd98da6991a core/src/luan/modules/PickleServer.java --- a/core/src/luan/modules/PickleServer.java Fri Dec 11 00:13:13 2015 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,121 +0,0 @@ -package luan.modules; - -import java.io.InputStream; -import java.io.OutputStream; -import java.io.StringWriter; -import java.io.PrintWriter; -import java.io.IOException; -import java.io.EOFException; -import java.util.List; -import java.util.ArrayList; -import luan.Luan; -import luan.LuanState; -import luan.LuanTable; -import luan.LuanFunction; -import luan.LuanJavaFunction; -import luan.LuanException; - - -public final class PickleServer { - - private final PickleCon con; - private boolean isRunning; - - PickleServer(LuanState luan,InputStream in,OutputStream out) { - this(new PickleCon(luan,in,out)); - } - - PickleServer(PickleCon con) { - this.con = con; - } - - void next() throws IOException { - try { - try { - Object[] result = Luan.array(con.read()); - StringBuilder sb = new StringBuilder(); - sb.append( "return true" ); - for( Object obj : result ) { - sb.append( ", " ); - sb.append( con.pickle(obj) ); - } - sb.append( '\n' ); - con.write( sb.toString() ); - } catch(LuanException e) { -// System.out.println(e); -//e.printStackTrace(); - StringBuilder sb = new StringBuilder(); - sb.append( "return false, " ); - sb.append( con.pickle(e.getFullMessage()) ); - sb.append( ", " ); - sb.append( con.pickle(con.src) ); - sb.append( '\n' ); - con.write( sb.toString() ); - } - } catch(LuanException e2) { - throw new RuntimeException(e2); - } - } - - public void run() throws LuanException { - LuanTable io = (LuanTable)PackageLuan.require(con.luan,"luan:Io"); - LuanTable env = con.env; - Object old_reverse_pickle = io.rawGet("reverse_pickle"); - Object old_unreverse_pickle = env.rawGet("_unreverse_pickle"); - try { - try { - io.rawPut("reverse_pickle", new LuanJavaFunction( - PickleServer.class.getMethod( "reverse_pickle", LuanFunction.class ), this - ) ); - env.rawPut("_unreverse_pickle", new LuanJavaFunction( - PickleServer.class.getMethod( "_unreverse_pickle" ), this - ) ); - } catch(NoSuchMethodException e) { - throw new RuntimeException(e); - } - isRunning = true; - try { - while( isRunning ) { - next(); - } - } catch(EOFException e) { - // done - } catch(IOException e) { - e.printStackTrace(); - } - if( isRunning ) { - try { - con.close(); - } catch(IOException e) { - throw new RuntimeException(e); - } - } - } finally { - io.rawPut("reverse_pickle",old_reverse_pickle); - env.rawPut("_unreverse_pickle",old_unreverse_pickle); - } - } - - public void reverse_pickle(LuanFunction fn) throws IOException, LuanException { - try { - con.write( "return _reversed_pickle()\n" ); - } catch(LuanException e) { - throw new RuntimeException(e); - } - PickleClient pc = new PickleClient(con); - try { - fn.call(con.luan,new Object[]{pc.table()}); - } finally { - try { - pc.call( "_unreverse_pickle()\n" ); - } catch(LuanException e) { - throw new RuntimeException(e); - } - } - } - - public void _unreverse_pickle() { - isRunning = false; - } - -}