comparison src/luan/modules/PickleServer.java @ 167:4c0131c2b650

merge luan/lib into modules git-svn-id: https://luan-java.googlecode.com/svn/trunk@168 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 22 Jun 2014 04:28:32 +0000
parents src/luan/lib/PickleServer.java@cc3a0578edac
children
comparison
equal deleted inserted replaced
166:4eaee12f6c65 167:4c0131c2b650
1 package luan.modules;
2
3 import java.io.DataInputStream;
4 import java.io.DataOutputStream;
5 import java.io.IOException;
6 import java.io.EOFException;
7 import java.util.List;
8 import java.util.ArrayList;
9 import luan.Luan;
10 import luan.LuanState;
11 import luan.LuanTable;
12 import luan.LuanFunction;
13 import luan.LuanJavaFunction;
14 import luan.LuanException;
15
16
17 public final class PickleServer {
18
19 private final PickleCon con;
20 private boolean isRunning;
21
22 PickleServer(LuanState luan,DataInputStream in,DataOutputStream out) {
23 this(new PickleCon(luan,in,out));
24 }
25
26 PickleServer(PickleCon con) {
27 this.con = con;
28 }
29
30 void next() throws IOException {
31 try {
32 List<String> list = new ArrayList<String>();
33 try {
34 Object[] result = Luan.array(con.read());
35 list.add( "return true" );
36 for( Object obj : result ) {
37 list.add( ", " );
38 list.add( con.pickle(obj) );
39 }
40 } catch(LuanException e) {
41 // System.out.println(e);
42 //e.printStackTrace();
43 list.add( "return false, " );
44 list.add( con.pickle(e.getMessage()) );
45 list.add( ", " );
46 list.add( con.pickle(con.src) );
47 }
48 list.add( "\n" );
49 con.write( list.toArray() );
50 } catch(LuanException e2) {
51 throw new RuntimeException(e2);
52 }
53 }
54
55 public void run() {
56 LuanTable ioModule = con.ioModule;
57 Object old_reverse_pickle = ioModule.get("reverse_pickle");
58 Object old_unreverse_pickle = ioModule.get("_unreverse_pickle");
59 try {
60 try {
61 ioModule.put("reverse_pickle", new LuanJavaFunction(
62 PickleServer.class.getMethod( "reverse_pickle", LuanFunction.class ), this
63 ) );
64 ioModule.put("_unreverse_pickle", new LuanJavaFunction(
65 PickleServer.class.getMethod( "_unreverse_pickle" ), this
66 ) );
67 } catch(NoSuchMethodException e) {
68 throw new RuntimeException(e);
69 }
70 isRunning = true;
71 try {
72 while( isRunning ) {
73 next();
74 }
75 } catch(EOFException e) {
76 // done
77 } catch(IOException e) {
78 e.printStackTrace();
79 }
80 if( isRunning ) {
81 try {
82 con.close();
83 } catch(IOException e) {
84 throw new RuntimeException(e);
85 }
86 }
87 } finally {
88 ioModule.put("reverse_pickle",old_reverse_pickle);
89 ioModule.put("_unreverse_pickle",old_unreverse_pickle);
90 }
91 }
92
93 public void reverse_pickle(LuanFunction fn) throws IOException, LuanException {
94 try {
95 con.write( "return Io._reversed_pickle()\n" );
96 } catch(LuanException e) {
97 throw new RuntimeException(e);
98 }
99 PickleClient pc = new PickleClient(con);
100 try {
101 con.luan.call(fn,new Object[]{pc.table()});
102 } finally {
103 try {
104 pc.call( "Io._unreverse_pickle()\n" );
105 } catch(LuanException e) {
106 throw new RuntimeException(e);
107 }
108 }
109 }
110
111 public void _unreverse_pickle() {
112 isRunning = false;
113 }
114
115 }