comparison src/luan/lib/PickleServer.java @ 146:0517a4a7fcc5

add Io.reverse_pickle git-svn-id: https://luan-java.googlecode.com/svn/trunk@147 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 15 Jun 2014 13:35:33 +0000
parents 90f38a5d0e0a
children cc3a0578edac
comparison
equal deleted inserted replaced
145:90f38a5d0e0a 146:0517a4a7fcc5
6 import java.io.EOFException; 6 import java.io.EOFException;
7 import java.util.List; 7 import java.util.List;
8 import java.util.ArrayList; 8 import java.util.ArrayList;
9 import luan.Luan; 9 import luan.Luan;
10 import luan.LuanState; 10 import luan.LuanState;
11 import luan.LuanTable;
12 import luan.LuanFunction;
13 import luan.LuanJavaFunction;
11 import luan.LuanException; 14 import luan.LuanException;
12 15
13 16
14 final class PickleServer { 17 public final class PickleServer {
15 18
16 private final PickleCon con; 19 private final PickleCon con;
20 private boolean isRunning;
17 21
18 PickleServer(LuanState luan,DataInputStream in,DataOutputStream out) { 22 PickleServer(LuanState luan,DataInputStream in,DataOutputStream out) {
19 con = new PickleCon(luan,in,out); 23 this(new PickleCon(luan,in,out));
24 }
25
26 PickleServer(PickleCon con) {
27 this.con = con;
20 } 28 }
21 29
22 void next() throws IOException { 30 void next() throws IOException {
23 try { 31 try {
24 List<String> list = new ArrayList<String>(); 32 List<String> list = new ArrayList<String>();
42 throw new RuntimeException(e2); 50 throw new RuntimeException(e2);
43 } 51 }
44 } 52 }
45 53
46 public void run() { 54 public void run() {
55 LuanTable ioModule = con.ioModule;
56 Object old_reverse_pickle = ioModule.get("reverse_pickle");
57 Object old_close_pickle = ioModule.get("unreverse_pickle");
47 try { 58 try {
48 while( true ) { 59 try {
49 next(); 60 ioModule.put("reverse_pickle", new LuanJavaFunction(
61 PickleServer.class.getMethod( "reverse_pickle" ), this
62 ) );
63 ioModule.put("unreverse_pickle", new LuanJavaFunction(
64 PickleServer.class.getMethod( "unreverse_pickle" ), this
65 ) );
66 } catch(NoSuchMethodException e) {
67 throw new RuntimeException(e);
50 } 68 }
51 } catch(EOFException e) { 69 isRunning = true;
52 // done 70 try {
53 } catch(IOException e) { 71 while( isRunning ) {
54 e.printStackTrace(); 72 next();
55 } 73 }
56 try { 74 } catch(EOFException e) {
57 con.close(); 75 // done
58 } catch(IOException e) { 76 } catch(IOException e) {
59 throw new RuntimeException(e); 77 e.printStackTrace();
78 }
79 if( isRunning ) {
80 try {
81 con.close();
82 } catch(IOException e) {
83 throw new RuntimeException(e);
84 }
85 }
86 } finally {
87 ioModule.put("reverse_pickle",old_reverse_pickle);
88 ioModule.put("unreverse_pickle",old_close_pickle);
60 } 89 }
61 } 90 }
62 91
92 public LuanTable reverse_pickle() throws IOException {
93 try {
94 con.write( "return Io._reversed_pickle()\n" );
95 } catch(LuanException e) {
96 throw new RuntimeException(e);
97 }
98 return new PickleClient(con).table();
99 }
100
101 public void unreverse_pickle() {
102 isRunning = false;
103 }
104
63 } 105 }