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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
package org.perl.inline.java ;
import java.net.* ;
import java.io.* ;
import java.util.* ;
/*
This is the server that will answer all the requests for and on Java
objects.
*/
public class InlineJavaServer {
private static InlineJavaServer instance = null ;
private String host = null ;
private int port = 0 ;
private boolean shared_jvm = false ;
private boolean priv = false ;
private boolean native_doubles = false ;
private boolean finished = false ;
private ServerSocket server_socket = null ;
private InlineJavaUserClassLoader ijucl = null ;
private HashMap<Thread, HashMap<Integer, Object>> thread_objects = new HashMap<>();
private int objid = 1 ;
private boolean jni = false ;
private Thread creator = null ;
private int thread_count = 0 ;
// This constructor is used in JNI mode
private InlineJavaServer(int debug, boolean _native_doubles){
init(debug, _native_doubles) ;
jni = true ;
AddThread(creator) ;
}
// This constructor is used in server mode
// Normally one would then call RunMainLoop()
/* Note: Consider http://groups.google.com/group/perl.inline/tree/browse_frm/thread/aa7f5ce236f6d576/3db48a308a8175fb?rnum=1&hl=en&q=Congratulations+with+Inline%3A%3AJava+0.51&_done=%2Fgroup%2Fperl.inline%2Fbrowse_frm%2Fthread%2Faa7f5ce236f6d576%2Fd2de9cf38429c09c%3Flnk%3Dst%26q%3DCongratulations+with+Inline%3A%3AJava+0.51%26rnum%3D1%26hl%3Den%26#doc_3db48a308a8175fb before changing this prototype */
public InlineJavaServer(int debug, String _host, int _port, boolean _shared_jvm, boolean _priv, boolean _native_doubles){
init(debug, _native_doubles) ;
jni = false ;
host = _host ;
port = _port ;
shared_jvm = _shared_jvm ;
priv = _priv ;
try {
if ((host == null)||(host.equals(""))||(host.equals("ANY"))){
server_socket = new ServerSocket(port) ;
}
else {
server_socket = new ServerSocket(port, 0, InetAddress.getByName(host)) ;
}
}
catch (IOException e){
InlineJavaUtils.Fatal("Can't open server socket on port " + String.valueOf(port) +
": " + e.getMessage()) ;
}
}
public void RunMainLoop(){
while (! finished){
try {
String name = "IJST-#" + thread_count++ ;
InlineJavaServerThread ijt = new InlineJavaServerThread(name, this, server_socket.accept(),
(priv ? new InlineJavaUserClassLoader() : ijucl)) ;
ijt.start() ;
if (! shared_jvm){
try {
ijt.join() ;
}
catch (InterruptedException e){
}
break ;
}
}
catch (IOException e){
if (! finished){
System.err.println("Main Loop IO Error: " + e.getMessage()) ;
System.err.flush() ;
}
}
}
}
private synchronized void init(int debug, boolean _native_doubles){
instance = this ;
creator = Thread.currentThread() ;
InlineJavaUtils.set_debug(debug) ;
native_doubles = _native_doubles ;
ijucl = new InlineJavaUserClassLoader() ;
}
static InlineJavaServer GetInstance(){
if (instance == null){
InlineJavaUtils.Fatal("No instance of InlineJavaServer has been created!") ;
}
return instance ;
}
InlineJavaUserClassLoader GetUserClassLoader(){
Thread t = Thread.currentThread() ;
if (t instanceof InlineJavaServerThread){
return ((InlineJavaServerThread)t).GetUserClassLoader() ;
}
else {
return ijucl ;
}
}
String GetType(){
return (shared_jvm ? "shared" : "private") ;
}
boolean GetNativeDoubles(){
return native_doubles ;
}
boolean IsJNI(){
return jni ;
}
/*
Since this function is also called from the JNI XS extension,
it's best if it doesn't throw any exceptions.
*/
String ProcessCommand(String cmd) {
return ProcessCommand(cmd, true) ;
}
String ProcessCommand(String cmd, boolean addlf) {
InlineJavaUtils.debug(3, "packet recv is " + cmd) ;
String resp = null ;
if (cmd != null){
InlineJavaProtocol ijp = new InlineJavaProtocol(this, cmd) ;
try {
ijp.Do() ;
InlineJavaUtils.debug(3, "packet sent is " + ijp.GetResponse()) ;
resp = ijp.GetResponse() ;
}
catch (InlineJavaException e){
// Encode the error in default encoding since we don't want any
// Exceptions thrown here...
String err = "error scalar:" + ijp.EncodeFromByteArray(e.getMessage().getBytes()) ;
InlineJavaUtils.debug(3, "packet sent is " + err) ;
resp = err ;
}
}
else{
if (! shared_jvm){
// Probably connection dropped...
InlineJavaUtils.debug(1, "lost connection with client in single client mode. Exiting.") ;
System.exit(1) ;
}
else{
InlineJavaUtils.debug(1, "lost connection with client in shared JVM mode.") ;
return null ;
}
}
if (addlf){
resp = resp + "\n" ;
}
return resp ;
}
/*
This method really has no business here, but for historical reasons
it will remain here.
*/
native String jni_callback(String cmd) ;
boolean IsThreadPerlContact(Thread t){
if (((jni)&&(t == creator))||
((! jni)&&(t instanceof InlineJavaServerThread))){
return true ;
}
return false ;
}
synchronized Object GetObject(int id) throws InlineJavaException {
Object o = null ;
HashMap h = (HashMap)thread_objects.get(Thread.currentThread()) ;
if (h == null){
throw new InlineJavaException("Can't find thread " + Thread.currentThread().getName() + "!") ;
}
else{
o = h.get(Integer.valueOf(id)) ;
if (o == null){
throw new InlineJavaException("Can't find object " + id + " for thread " +Thread.currentThread().getName()) ;
}
}
return o ;
}
synchronized int PutObject(Object o) throws InlineJavaException {
HashMap<Integer, Object> h = (HashMap<Integer, Object>)thread_objects.get(Thread.currentThread()) ;
int id = objid ;
if (h == null){
throw new InlineJavaException("Can't find thread " + Thread.currentThread().getName() + "!") ;
}
else{
h.put(Integer.valueOf(objid), o) ;
objid++ ;
}
return id ;
}
synchronized Object DeleteObject(int id) throws InlineJavaException {
Object o = null ;
HashMap h = (HashMap)thread_objects.get(Thread.currentThread()) ;
if (h == null){
throw new InlineJavaException("Can't find thread " + Thread.currentThread().getName() + "!") ;
}
else{
o = h.remove(Integer.valueOf(id)) ;
if (o == null){
throw new InlineJavaException("Can't find object " + id + " for thread " + Thread.currentThread().getName()) ;
}
}
return o ;
}
synchronized int ObjectCount() throws InlineJavaException {
int i = -1 ;
HashMap h = (HashMap)thread_objects.get(Thread.currentThread()) ;
if (h == null){
throw new InlineJavaException("Can't find thread " + Thread.currentThread().getName() + "!") ;
}
else{
i = h.values().size() ;
}
return i ;
}
public synchronized void StopMainLoop(){
if (! jni){
try {
finished = true ;
server_socket.close() ;
}
catch (IOException e){
System.err.println("Shutdown IO Error: " + e.getMessage()) ;
System.err.flush() ;
}
}
}
synchronized void Shutdown(){
StopMainLoop() ;
System.exit(0) ;
}
/*
Here the prototype accepts Threads because the JNI thread
calls this method also.
*/
synchronized void AddThread(Thread t){
thread_objects.put(t, new HashMap<Integer, Object>()) ;
InlineJavaPerlCaller.AddThread(t) ;
}
synchronized void RemoveThread(InlineJavaServerThread t){
thread_objects.remove(t) ;
InlineJavaPerlCaller.RemoveThread(t) ;
}
/*
Startup
*/
public static void main(String[] argv){
int debug = Integer.parseInt(argv[0]) ;
String host = argv[1] ;
int port = Integer.parseInt(argv[2]) ;
boolean shared_jvm = Boolean.parseBoolean(argv[3]) ;
boolean priv = Boolean.parseBoolean(argv[4]) ;
boolean native_doubles = Boolean.parseBoolean(argv[5]) ;
InlineJavaServer ijs = new InlineJavaServer(debug, host, port, shared_jvm, priv, native_doubles) ;
ijs.RunMainLoop() ;
System.exit(0) ;
}
/*
With PerlInterpreter this is called twice, but we don't want to create
a new object the second time.
*/
public static InlineJavaServer jni_main(int debug, boolean native_doubles){
if (instance != null){
InlineJavaUtils.set_debug(debug) ;
InlineJavaUtils.debug(1, "recycling InlineJavaServer created by PerlInterpreter") ;
return instance ;
}
else {
return new InlineJavaServer(debug, native_doubles) ;
}
}
}
|