File: InlineJavaCallback.java

package info (click to toggle)
libinline-java-perl 0.67-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 716 kB
  • sloc: perl: 3,817; java: 2,621; makefile: 35; sh: 1
file content (158 lines) | stat: -rw-r--r-- 3,833 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package org.perl.inline.java ;

import java.util.* ;
import java.io.* ;


/*
	Callback to Perl...
*/
class InlineJavaCallback {
	private InlineJavaServer ijs = InlineJavaServer.GetInstance() ;
	private String pkg = null ;
	private InlineJavaPerlObject obj = null ;
	private String method = null ;
	private Object args[] = null ;
	private Class cast = null ;
	private Object response = null ;
	private boolean response_set = false ;


	InlineJavaCallback(String _pkg, String _method, Object _args[], Class _cast) {
		this(null, _pkg, _method, _args, _cast) ;
	}
	
	
	InlineJavaCallback(InlineJavaPerlObject _obj, String _method, Object _args[], Class _cast) {
		this(_obj, null, _method, _args, _cast) ;
		if (obj == null){
			throw new NullPointerException() ;
		}
	}
	
	
	private InlineJavaCallback(InlineJavaPerlObject _obj, String _pkg, String _method, Object _args[], Class _cast) {
		obj = _obj ;
		pkg = _pkg ;
		method = _method ;
		args = _args ;
		cast = _cast ;
				
		if (method == null){
			throw new NullPointerException() ;
		}
		if (cast == null){
			cast = java.lang.Object.class ;
		}
	}


	private String GetCommand(InlineJavaProtocol ijp) throws InlineJavaException {
		String via = null ;
		if (obj != null){
			via = "" + obj.GetId() ;
		}
		else if (pkg != null){
			via = pkg ;
		}
		StringBuffer cmdb = new StringBuffer("callback " + via + " " + method + " " + cast.getName()) ;
		if (args != null){
			for (int i = 0 ; i < args.length ; i++){
				cmdb.append(" " + ijp.SerializeObject(args[i], null)) ;
			}
		}
		return cmdb.toString() ;
	}


	void ClearResponse(){
		response = null ;
		response_set = false ;
	}


	Object GetResponse(){
		return response ;
	}


	synchronized Object WaitForResponse(Thread t){
		while (! response_set){
			try {
				InlineJavaUtils.debug(3, "waiting for callback response in " + t.getName() + "...") ;
				wait() ;
			}
			catch (InterruptedException ie){
				// Do nothing, return and wait() some more...
			}
		}
		InlineJavaUtils.debug(3, "got callback response") ;
		Object resp = response ;
		response = null ;
		response_set = false ;
		return resp ;
	}


	synchronized void NotifyOfResponse(Thread t){
		InlineJavaUtils.debug(3, "notifying that callback has completed in " + t.getName()) ;
		notify() ;
	}


	synchronized void Process() throws InlineJavaException, InlineJavaPerlException {
		Object ret = null ;
		try {
			InlineJavaProtocol ijp = new InlineJavaProtocol(ijs, null) ;
			String cmd = GetCommand(ijp) ;
			InlineJavaUtils.debug(2, "callback command: " + cmd) ;

			Thread t = Thread.currentThread() ;
			String resp = null ;
			while (true) {
				InlineJavaUtils.debug(3, "packet sent (callback) is " + cmd) ;
				if (! ijs.IsJNI()){
					// Client-server mode.
					InlineJavaServerThread ijt = (InlineJavaServerThread)t ;
					ijt.GetWriter().write(cmd + "\n") ;
					ijt.GetWriter().flush() ;

					resp = ijt.GetReader().readLine() ;
				}
				else{
					// JNI mode
					resp = ijs.jni_callback(cmd) ;
				}
				InlineJavaUtils.debug(3, "packet recv (callback) is " + resp) ;

				StringTokenizer st = new StringTokenizer(resp, " ") ;
				String c = st.nextToken() ;
				if (c.equals("callback")){
					boolean thrown = Boolean.parseBoolean(st.nextToken()) ;
					String arg = st.nextToken() ;
					InlineJavaClass ijc = new InlineJavaClass(ijs, ijp) ;
					ret = ijc.CastArgument(cast, arg) ;

					if (thrown){
						throw new InlineJavaPerlException(ret) ;
					}

					break ;
				}
				else{
					// Pass it on through the regular channel...
					InlineJavaUtils.debug(3, "packet is not callback response: " + resp) ;
					cmd = ijs.ProcessCommand(resp, false) ;

					continue ;
				}
			}
		}
		catch (IOException e){
			throw new InlineJavaException("IO error: " + e.getMessage()) ;
		}

		response = ret ;
		response_set = true ;
	}
}