FreeWRL/FreeX3D  3.0.0
FWJavaScript.java
1 package vrml;
2 
3 import java.net.*;
4 import java.lang.System;
5 import java.lang.reflect.*;
6 import java.io.*;
7 import java.util.Hashtable;
8 //JAS import java.util.Vector;
9 import java.util.Enumeration;
10 //JAS import vrml.*;
11 import vrml.node.*;
12 
13 public final class FWJavaScript {
14  static Hashtable touched = new Hashtable();
15  static String reqid;
16  static Browser theBrowser;
17 
18  static Socket sock; // communication socket with FreeWRL
19  static BufferedReader EAIin;
20  static PrintWriter EAIout;
21 
22 
23 
24 
25 
26  public static void add_touched(Field f) {
27  touched.put(f, Boolean.TRUE);
28  }
29 
30  public static void send_touched(String reqid) throws IOException {
31  // System.out.println("send_touched\n");
32  Enumeration e = touched.keys();
33  while(e.hasMoreElements()) {
34  // System.out.println("send_touched one\n");
35  Field val = (Field) e.nextElement();
36  FWJavaScriptBinding b = val.__binding;
37  BaseNode n = b.node();
38  String f = b.field() + " " + val.getOffset();
39  //System.out.println ("java, send_touched, offset of " +
40  // b.field() + " is " + val.getOffset());
41  String nodeid = n._get_nodeid();
42  EAIout.println("JSENDEV");
43  EAIout.println(nodeid);
44  EAIout.println(f);
45  val.__toPerl(EAIout);
46  EAIout.println();
47  }
48  touched.clear();
49  EAIout.println("FINISHED "+reqid);
50  EAIout.flush();
51  }
52 
53  public static void main (String argv[])
54  throws ClassNotFoundException,
55  NoSuchMethodException,
56  InstantiationException,
57  IllegalAccessException,
58  InvocationTargetException,
59  Exception,
60  Throwable
61  {
62  int counter;
63  String reply;
64  String nodeid = "";
65  String seqno;
66 
67  // Create a socket here for an EAI/CLASS server on localhost
68  sock = null;
69 
70  counter = 1;
71  while (sock == null) {
72  try {
73  //System.out.println (" ....FWJavaScript trying socket " + argv[0]);
74  sock = new Socket("localhost",Integer.parseInt(argv[0]));
75  } catch (IOException e) {
76  // wait up to 30 seconds for FreeWRL to answer.
77  counter = counter + 1;
78  if (counter == 10) {
79  System.out.println (" ....FWJavaScript: Java code timed out finding FreeWRL");
80  System.exit(1);
81  }
82  try {Thread.sleep (500);} catch (InterruptedException f) { }
83  }
84  }
85 
86  EAIout = new PrintWriter (sock.getOutputStream());
87  EAIin = new BufferedReader( new InputStreamReader(sock.getInputStream()));
88 
89  /* Install security */
90  System.out.println ("Security manager commented out");
91  //System.setSecurityManager(new SecurityManager());
92 
93  /* And Go... */
94  theBrowser = new Browser();
95 
96  Hashtable scripts = new Hashtable();
97  EAIout.println("JavaClass version 1.0 - www.crc.ca");
98  EAIout.flush();
99 
100  while(true) {
101  String cmd = EAIin.readLine();
102 
103  // did FreeWRL leave us?
104  if (cmd == null) {
105  //System.out.println ("have null string exiting...\n");
106  System.exit(1);
107  }
108 
109  //System.out.println("FWJ got ");
110  //System.out.println("--- "+cmd);
111 
112  nodeid =EAIin.readLine();
113  //System.out.println (" ....FWJ, got nodeID " + nodeid);
114 
115  if(cmd.equals("NEWSCRIPT")) {
116  String url = EAIin.readLine();
117  reqid = EAIin.readLine();
118  //System.out.println("NEWSCRIPT: "+url);
119  FWJavaScriptClassLoader classloader =
120  new FWJavaScriptClassLoader(url);
121  String classname
122  = url.substring(url.lastIndexOf('/')+1);
123  if (classname.endsWith(".class"))
124  classname = classname
125  .substring(0, classname.length() - 6);
126  Script s;
127  try {
128  s = (Script) classloader
129  .loadClass(classname).newInstance();
130  } catch (Exception ex) {
131  System.out.println("Can't load script: "
132  + url);
133  throw ex;
134  }
135  s._set_nodeid(nodeid);
136  //System.out.println ("setting nodeid to " + nodeid);
137  scripts.put(nodeid,s);
138  } else if(cmd.equals("SETFIELD")) {
139  System.out.println ("SETFIELD NOT HANDLED YET\n");
140 
141  } else if(cmd.equals("INITIALIZE")) {
142  Script s = (Script)scripts.get(nodeid);
143  reqid = EAIin.readLine();
144  s.initialize();
145  send_touched(reqid);
146  } else if(cmd.equals("EVENTSPROCESSED")) {
147  Script s = (Script)scripts.get(nodeid);
148  reqid = EAIin.readLine();
149  s.eventsProcessed();
150  send_touched(reqid);
151  } else if(cmd.equals("SENDEVENT")) {
152  Script s = (Script)scripts.get(nodeid);
153  String fname = EAIin.readLine();
154  String ftype = EAIin.readLine();
155  reqid = EAIin.readLine(); // note reqid position, different than
156  // others, but we are using EAI functions.
157  // position does not matter...
158  //System.out.println (" ....FWJ, got SENDEVENT, NodeID " + nodeid
159  // + " field " + fname + " type " + ftype + " reqid "
160  // + reqid);
161 
162  ConstField fval =
163  FWCreateField.createConstField(ftype);
164  fval.__fromPerl(EAIin);
165  double timestamp =
166  Double.parseDouble(EAIin.readLine());
167  Event ev = new Event(
168  fname,
169  timestamp,
170  fval
171  );
172  s.processEvent(ev);
173  send_touched(reqid);
174  } else {
175  throw new Exception("Invalid command '"
176  + cmd + "'");
177  }
178  EAIout.flush();
179  }
180  }
181 
182  public static String getFieldType(BaseNode node, String fieldname,
183  String kind)
184  {
185  String str;
186  try {
187  EAIout.println("GETFIELD " + node._get_nodeid() + " " + fieldname + " " + kind);
188  EAIout.flush();
189  return EAIin.readLine();
190  } catch (IOException e) {
191  throw new InternalError("Communication error: "+e);
192  }
193  }
194 
195  public static void readField(BaseNode node, String fieldName, Field fld) {
196  try {
197  FWJavaScript.EAIout.println("READFIELD " + node._get_nodeid() + " " + fieldName);
198  FWJavaScript.EAIout.flush();
199  fld.__fromPerl(EAIin);
200  } catch (IOException e) {
201  throw new InternalError("Communication error: "+e);
202  }
203  }
204 
205  public static String getNodeType(BaseNode node)
206  {
207  try {
208  FWJavaScript.EAIout.println("GETTYPE "+ node._get_nodeid());
209  FWJavaScript.EAIout.flush();
210  return EAIin.readLine();
211  } catch (IOException e) {
212  throw new InternalError("Communication error: "+e);
213  }
214  }
215 
216  public static Browser getBrowser()
217  {
218  return theBrowser;
219  }
220 
221 
222  public static BaseNode[] createVrmlFromString(String vrmlSyntax)
224  {
225  try {
226  FWJavaScript.EAIout.println("CREATEVRML");
227  FWJavaScript.EAIout.println(vrmlSyntax);
228  FWJavaScript.EAIout.println("EOT");
229  FWJavaScript.EAIout.flush();
230  String intstring = FWJavaScript.EAIin.readLine();
231  int number = Integer.parseInt(intstring);
232  if (number == -1)
233  throw new InvalidVRMLSyntaxException(EAIin.readLine());
234 
235  if (number == 0)
236  return null;
237 
238  Node[] nodes = new Node[number];
239 
240  // remember, nodes have a frontend:backend; one is known in Perl, the
241  //System.out.println ("Java: Create, reading in " + number + " nodes");
242  // other is the C pointer to memory.
243  for (int i = 0; i < number; i++)
244  nodes[i] = new Node(""+EAIin.readLine()+":"+EAIin.readLine());
245  //System.out.println ("returning from Java Create");
246  return nodes;
247  } catch (IOException e) {
248  throw new InternalError("Communication error: "+e);
249  }
250  }
251 
252 
253  public static BaseNode[] createX3DFromString(String vrmlSyntax)
255  {
256  try {
257  FWJavaScript.EAIout.println("CREATEX3D");
258  FWJavaScript.EAIout.println(vrmlSyntax);
259  FWJavaScript.EAIout.println("EOT");
260  FWJavaScript.EAIout.flush();
261  String intstring = FWJavaScript.EAIin.readLine();
262  int number = Integer.parseInt(intstring);
263  if (number == -1)
264  throw new InvalidX3DSyntaxException(EAIin.readLine());
265 
266  if (number == 0)
267  return null;
268 
269  Node[] nodes = new Node[number];
270 
271  // remember, nodes have a frontend:backend; one is known in Perl, the
272  //System.out.println ("Java: Create, reading in " + number + " nodes");
273  // other is the C pointer to memory.
274  for (int i = 0; i < number; i++)
275  nodes[i] = new Node(""+EAIin.readLine()+":"+EAIin.readLine());
276  //System.out.println ("returning from Java Create");
277  return nodes;
278  } catch (IOException e) {
279  throw new InternalError("Communication error: "+e);
280  }
281  }
282 
283 }