6 import org.web3d.x3d.sai.*;
8 import org.w3c.dom.Node;
13 import vrml.external.exception.*;
15 import java.lang.System;
16 import javax.swing.event.*;
24 static Thread FreeWRLThread;
33 PrintWriter EAIinThreadtoBrowser;
34 PipedWriter EAIinThreadtoBrowserPipe = null;
35 static BufferedReader BrowserfromEAI = null;
36 PipedReader BrowserfromEAIPipe = null;
42 static PrintWriter EAIout;
49 private String reply =
"";
52 static int queryno = 1;
55 static Object FreeWRLToken =
new Object();
58 EventListenerList listenerList =
new EventListenerList();
61 String description =
"";
64 boolean disposed =
false;
67 public int get_Browser_EVtype (
int event)
69 return BrowserGlobals.EVtype[event];
74 return BrowserGlobals.EVObserver[eventno];
77 public void Browser_RL_Async_send (String EVentreply,
int eventno)
80 for (EVcounter=0; EVcounter<BrowserGlobals.EVno; EVcounter++) {
85 RL_Async.send(EVentreply, EVcounter);
95 while (EAISocket == null) {
97 EAISocket =
new Socket(
"localhost",portnum);
98 }
catch (IOException e) {
100 counter = counter + 1;
102 System.out.println (
"SAI: Java code timed out finding FreeWRL");
107 }
catch (InterruptedException f) {
119 EAIinThreadtoBrowserPipe =
new PipedWriter();
120 BrowserfromEAIPipe =
new PipedReader(EAIinThreadtoBrowserPipe);
121 }
catch (IOException ie) {
122 System.out.println (
"SAI: caught error in new PipedReader: " + ie);
125 EAIinThreadtoBrowser =
new PrintWriter(EAIinThreadtoBrowserPipe);
126 BrowserfromEAI =
new BufferedReader (BrowserfromEAIPipe);
134 EAIinThreadtoBrowserPipe =
new PipedWriter();
135 BrowserfromEAIPipe =
new PipedReader(EAIinThreadtoBrowserPipe);
136 }
catch (IOException ie) {
137 System.out.println (
"SAI: caught error in new PipedReader: " + ie);
140 EAIinThreadtoBrowser =
new PrintWriter(EAIinThreadtoBrowserPipe);
141 BrowserfromEAI =
new BufferedReader (BrowserfromEAIPipe);
144 FreeWRLThread =
new Thread (
new EAIinThread(sock, pApplet, EAIinThreadtoBrowser,
this));
145 FreeWRLThread.start();
158 EAIout =
new PrintWriter (sock.getOutputStream());
159 }
catch (IOException e) {
160 System.out.print (
"SAI: Problem in handshaking with Browser");
165 EAIoutSender.start();
182 while (EAISocket == null) {
184 EAISocket =
new Socket(
"localhost",9877);
185 }
catch (IOException e) {
187 counter = counter + 1;
189 System.out.println (
"SAI: Java code timed out finding FreeWRL");
194 }
catch (InterruptedException f) {
205 EAIinThreadtoBrowserPipe =
new PipedWriter();
206 BrowserfromEAIPipe =
new PipedReader(EAIinThreadtoBrowserPipe);
207 }
catch (IOException ie) {
208 System.out.println (
"SAI: caught error in new PipedReader: " + ie);
211 EAIinThreadtoBrowser =
new PrintWriter(EAIinThreadtoBrowserPipe);
212 BrowserfromEAI =
new BufferedReader (BrowserfromEAIPipe);
215 FreeWRLThread =
new Thread (
new EAIinThread(sock, pApplet, EAIinThreadtoBrowser,
this));
216 FreeWRLThread.start();
227 EAIout =
new PrintWriter (sock.getOutputStream());
228 }
catch (IOException e) {
229 System.out.print (
"SAI: Problem in handshaking with Browser");
234 EAIoutSender.start();
243 public void checkValid() {
255 synchronized (FreeWRLToken) {
256 EAIoutSender.send (
"" + queryno +
"K\n");
257 retval = getVRMLreply(queryno);
264 private void getRendProp() {
266 StringTokenizer tokens;
270 synchronized (FreeWRLToken) {
271 EAIoutSender.send(
"" + queryno +
"X\n");
272 retval = getVRMLreply(queryno);
276 tokens =
new StringTokenizer (retval);
278 value = tokens.nextToken();
279 FreeWRLRendererInfo.setRenderingProperty(key, value);
280 key =
"MaxTextureSize";
281 value = tokens.nextToken();
282 FreeWRLRendererInfo.setRenderingProperty(key, value);
283 key =
"TextureUnits";
284 value = tokens.nextToken();
285 FreeWRLRendererInfo.setRenderingProperty(key,
new Integer(value));
287 value = tokens.nextToken();
288 FreeWRLRendererInfo.setRenderingProperty(key,
new Boolean(value));
290 value = tokens.nextToken();
291 FreeWRLRendererInfo.setRenderingProperty(key,
new Integer(value));
292 key =
"TextureMemory";
293 value = tokens.nextToken();
294 FreeWRLRendererInfo.setRenderingProperty(key,
new Float(value));
302 synchronized (FreeWRLToken) {
303 EAIoutSender.send (
"" + queryno +
"L\n");
304 retval = getVRMLreply(queryno);
317 synchronized (FreeWRLToken) {
318 EAIoutSender.send (
"" + queryno +
"M\n");
319 retval = getVRMLreply(queryno);
323 return Float.valueOf(retval).floatValue();
332 synchronized (FreeWRLToken) {
333 EAIoutSender.send (
"" + queryno +
"N\n");
334 retval = getVRMLreply(queryno);
338 return Float.valueOf(retval).floatValue();
342 String SysString =
"";
349 if (passedscene != null) {
350 nodes = (
FreeWRLNode[]) passedscene.getRootNodes();
352 for (count=0; count<nodes.length; count++) {
353 SysString = SysString +
" " + nodes[count].getPointer();
357 synchronized (FreeWRLToken) {
358 EAIoutSender.send (
"" + queryno +
"P" + SysString);
359 retval = getVRMLreply(queryno);
379 StringTokenizer tokens;
386 synchronized (FreeWRLToken) {
387 EAIoutSender.send (
"" +queryno +
"S "+ str +
"\nEOT\n");
388 retval = getVRMLreply(queryno);
390 if (retval.equals(
"")) {
394 tokens =
new StringTokenizer (retval);
401 while (tokens.hasMoreTokens()) {
403 x[count].setPerlPtr(tokens.nextToken());
404 x[count].setPointer(tokens.nextToken());
405 x[count].setType(FreeWRLFieldTypes.getIntType(
"h"));
410 System.out.println (
"SAI: createVrmlFromString - warning, tied to 100 nodes");
418 public X3DNode createNodeFromString(String str) {
420 StringTokenizer tokens;
424 synchronized(FreeWRLToken) {
425 EAIoutSender.send(
"" + queryno +
"S " + str +
"\nEOT\n");
426 retval = getVRMLreply(queryno);
428 tokens =
new StringTokenizer(retval);
429 while(tokens.hasMoreTokens()) {
430 x.setPerlPtr(tokens.nextToken());
431 x.setPointer(tokens.nextToken());
432 x.setType(FreeWRLFieldTypes.getIntType(
"h"));
441 StringTokenizer tokens;
450 InputStreamReader isr =
new InputStreamReader(is);
453 while ((c = isr.read()) != -1) {
456 }
catch (Exception e) {
457 throw new IOException(e +
" Error reading from stream ");
460 synchronized (FreeWRLToken) {
461 EAIoutSender.send (
"" +queryno +
"S "+ str +
"\nEOT\n");
462 retval = getVRMLreply(queryno);
464 if (retval.equals(
"")) {
468 tokens =
new StringTokenizer (retval);
475 while (tokens.hasMoreTokens()) {
477 x[count].setPerlPtr(tokens.nextToken());
478 x[count].setPointer(tokens.nextToken());
479 x[count].setType(FreeWRLFieldTypes.getIntType(
"h"));
484 System.out.println (
"SAI: createVrmlFromString - warning, tied to 100 nodes");
495 StringTokenizer tokens;
502 nodeList =
new ArrayList(1);
508 for (i = 0; i < url.length; i++) {
509 synchronized (FreeWRLToken) {
510 EAIoutSender.send (
"" + queryno +
"T " + url[i] +
"\n");
512 retval = getVRMLreply(queryno);
514 tokens =
new StringTokenizer (retval);
515 System.out.println(
"retval is: " + retval);
517 if (!retval.equals(
"")) {
518 while (tokens.hasMoreTokens()) {
520 temp.setPerlPtr(tokens.nextToken());
521 temp.setPointer(tokens.nextToken());
522 temp.setType(FreeWRLFieldTypes.getIntType(
"h"));
542 return FreeWRLRendererInfo.getRenderingProperties();
547 return FreeWRLBrowserInfo.getBrowserProperties();
553 synchronized (FreeWRLToken) {
554 EAIoutSender.send(
"" + queryno +
"R NEXT\n");
555 retval = getVRMLreply(queryno);
562 synchronized (FreeWRLToken) {
563 EAIoutSender.send(
"" + queryno +
"R PREV\n");
564 retval = getVRMLreply(queryno);
571 synchronized (FreeWRLToken) {
572 EAIoutSender.send(
"" + queryno +
"R FIRST\n");
573 retval = getVRMLreply(queryno);
580 synchronized (FreeWRLToken) {
581 EAIoutSender.send(
"" + queryno +
"R LAST\n");
582 retval = getVRMLreply(queryno);
588 System.out.print(obj);
592 System.out.println(obj);
597 public String addRoute(
FreeWRLNode fromNode, String fromEventOut,
599 IllegalArgumentException {
602 synchronized (FreeWRLToken) {
603 EAIoutSender.send (
"" + queryno +
"H " + fromNode.getPointer() +
" " + fromEventOut +
604 " " + toNode.getPointer() +
" " + toEventIn +
"\n");
605 retval = getVRMLreply(queryno);
612 public String deleteRoute(
FreeWRLNode fromNode, String fromEventOut,
614 throws IllegalArgumentException {
617 synchronized (FreeWRLToken) {
618 EAIoutSender.send (
"" + queryno +
"J " + fromNode.getPointer() +
" " + fromEventOut +
619 " " + toNode.getPointer() +
" " + toEventIn +
"\n");
620 retval = getVRMLreply(queryno);
627 public void beginUpdate() {}
628 public void endUpdate() {}
631 public void initialize() {
632 System.out.println (
"SAI: Initialize Not Implemented");
638 public void shutdown() {
639 System.out.println (
"SAI: Shutdown Not Implemented");
654 StringTokenizer tokens;
656 if (NodeName == null) {
660 synchronized (FreeWRLToken) {
661 EAIoutSender.send (
"" + queryno +
"A " + NodeName +
"\n");
662 System.out.println(
"SENT: " + NodeName +
" " + queryno);
663 retval = getVRMLreply(queryno);
664 System.out.println(
"GOT RETVAL: " + retval);
665 tokens =
new StringTokenizer(retval);
666 temp.setPerlPtr(tokens.nextToken());
667 temp.setPointer(tokens.nextToken());
670 if (((temp.getPerlPtr()).equals(NodeName)) || ((temp.getPerlPtr()).equals(
"-1"))) {
679 public static void SendChildEvent (String parent, String offset, String FieldName, String Child) {
685 synchronized (FreeWRLToken) {
686 EAIoutSender.send (
"" + queryno +
"C " + parent +
" " + offset +
" " +
687 FieldName +
" "+ Child +
"\n");
688 retval = getVRMLreply(queryno);
695 public static void newSendEvent (
FreeWRLField field, String Value) {
697 synchronized (FreeWRLToken) {
698 EAIoutSender.send (
"" + queryno +
"D" + field.getDataType()+
" " +
699 field.getNodePtr()+
" " + field.getOffset() +
" " +field.getScriptType() +
" " + Value +
"\n");
705 public static String sendGlobalCommand (String
command) {
708 synchronized (FreeWRLToken) {
709 EAIoutSender.send(
"" + queryno + command);
710 retval = getVRMLreply(queryno);
718 protected static String SendEventType (String NodeName, String ptr, String FieldName, String direction) {
724 synchronized (FreeWRLToken) {
725 EAIoutSender.send (
"" + queryno +
"F " + NodeName +
" " + ptr +
" " + FieldName +
" " + direction +
"\n");
726 retval = getVRMLreply(queryno);
732 public static String SendEventOut (String nodeptr, String offset, String datasize, String datatype,
736 synchronized (FreeWRLToken) {
737 EAIoutSender.send (
"" + queryno +
"E " + nodeptr +
" " + offset +
" " + datatype +
738 " " + datasize +
"\n");
740 retval = getVRMLreply(queryno);
747 String nodeptr, String offset, String datatype, String datasize,
int EventType)
749 synchronized (FreeWRLToken) {
750 EAIoutSender.send (
"" + queryno +
"G " + nodeptr +
" " + offset +
" " + datatype +
" " + datasize +
"\n");
752 BrowserGlobals.EVarray [BrowserGlobals.EVno] = queryno;
753 BrowserGlobals.EVtype [BrowserGlobals.EVno] = EventType;
754 BrowserGlobals.EVObject[BrowserGlobals.EVno] = userData;
755 BrowserGlobals.EVObserver[BrowserGlobals.EVno] = f;
757 BrowserGlobals.EVno += 1;
759 getVRMLreply(queryno);
764 public static void unRegisterListener (
X3DFieldEventListener f, String nodeptr, String offset, String datatype, String datasize,
int EventType)
766 synchronized (FreeWRLToken) {
767 EAIoutSender.send (
"" + queryno +
"W " + nodeptr +
" " + offset +
" " + datatype +
" " + datasize +
"\n");
769 getVRMLreply(queryno);
774 protected synchronized static String getVRMLreply (
int queryno) {
782 while (queryno != Integer.parseInt(req)) {
784 req = BrowserfromEAI.readLine();
785 System.out.println(
"req " + req);
786 }
catch (IOException ie) {
787 System.out.println (
"SAI: caught " + ie);
791 if (queryno != Integer.parseInt(req)) {
792 System.out.println (
"SAI: getVRMLreply - REPLIES MIXED UP!!! Expecting " + queryno +
" got " + req);
796 rep = BrowserfromEAI.readLine();
797 System.out.println(
"rep is " + rep);
798 }
catch (IOException ie) { System.out.println (
"SAI: getVRMLreply failed");
return null; }
803 public void close() {
804 System.out.println(
"SAI: closing socket");
815 public void dispose() {
821 listenerList.add(BrowserListener.class, listener);
825 listenerList.remove(BrowserListener.class, listener);
828 public void browserEvent(
int type) {
833 Object[] listeners = listenerList.getListenerList();
835 for (
int i = listeners.length-2; i >=0; i-=2) {
869 if ((profile == null) && (components == null)) {
873 if (profile != null) {
874 ProfileInfo prof = FWProfInfo.getProfile(profile.getName());
877 if (components != null) {
878 for (
int i = 0; i < components.length; i++) {
879 comp = FWProfInfo.getComponent(components[i].getName(), components[i].getLevel());
890 StringTokenizer tokens;
899 if (parameters == null) {
903 rtemp = (Boolean) parameters.get(
"replace");
907 replace = rtemp.booleanValue();
911 nodeList =
new ArrayList(1);
913 for (i = 0; i < url.length; i++) {
914 synchronized (FreeWRLToken) {
915 EAIoutSender.send (
"" + queryno +
"T " + url[i] +
"\n");
917 retval = getVRMLreply(queryno);
919 tokens =
new StringTokenizer (retval);
920 System.out.println(
"retval is: *" + retval +
"*");
922 if (!retval.equals(
"")) {
923 while (tokens.hasMoreTokens()) {
925 temp.setPerlPtr(tokens.nextToken());
926 temp.setPointer(tokens.nextToken());
927 temp.setType(FreeWRLFieldTypes.getIntType(
"h"));
940 if (!(nodeList.size() == 0)) {
943 replaceWorld(newscene);
948 for (i = 0; i < nodes.length; i++) {
949 scene.addRootNode(nodes[i]);
959 public void stopRender() {
961 public void pauseRender() {