File: VRwavePlugin.java

package info (click to toggle)
vrwave 0.9-4
  • links: PTS
  • area: non-free
  • in suites: slink
  • size: 5,032 kB
  • ctags: 7,153
  • sloc: java: 15,050; ansic: 8,219; sh: 458; makefile: 181
file content (205 lines) | stat: -rw-r--r-- 4,789 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
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
/*
 * VRwavePlugin.java
 * Netscape Plugin wrapper around VRwave
 *
 * created: mpichler, 19970812
 *
 * changed: mpichler, 19970924
 *
 * $Id: VRwavePlugin.java,v 1.8 1997/09/24 15:26:26 mpichler Exp $
 */


import netscape.plugin.Plugin;  // moz3_0.zip

import java.net.URL;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

import iicm.vrml.vrwave.*;  // VRwave core classes
import vrml.external.Browser;  // Java-EAI


/**
 * depending on whether Browser already extends Plugin or not,
 * VRwavePlugin has to extend Browser or Plugin. In the first case,
 * Browser location via JavaScript code will work too, otherwise only
 * the clean interface of Browser.getBrowser will do so.
 */

public class VRwavePlugin extends Browser /*Plugin*/
{
  Scene scene_;  // my scene
  SceneFrame sceneframe_;
  PipedInputStream pin_;  // to read data out of the pipe
  PipedOutputStream pout_;  // to write data into the pipe
  Thread rthread_;  // reader thread (parsing)
  VRwaveReader vrreader_;
  static final boolean DEBUG = true;

  /**
   * initialize plug-in instance: start VRwave
   */

  public void init ()
  {
    System.out.println (VRwave.STARTUP);

    if (DEBUG)
      System.err.println ("Plugin.init was called");

    try
    {
      scene_ = new Scene (null);
      // TODO: where to load properties from?
    }
    catch (Throwable t)
    {
      System.err.println ("Error: " + t);
    }
  } // init

  /**
   * create scene frame
   * @param home VRWAVE_HOME to load icons.gif (later also font files etc.)
   */

  void createSceneFrame (String home)
  {
    try
    {
      VRwave.setHome ("file:" + home);  // must read from URL instead of file
      sceneframe_ = new SceneFrame ("VRwave", scene_, true);  // shows itself
    }
    catch (Throwable t)
    {
      System.err.println ("Error: " + t);
    }
  } // createSceneFrame

  /**
   * plug-in gets unloaded: quit VRwave
   */

  public void destroy ()
  {
    if (DEBUG)
      System.err.println ("Plugin.destroy was called");

    if (rthread_ != null && rthread_.isAlive ())
    {
      if (DEBUG)
        System.err.println ("stopping reader thread");
      rthread_.stop ();
    }

    if (scene_ != null)
    {
      try
      {
        if (DEBUG)
          System.err.println ("clearScene");
        Browser.stopVRwave (scene_);
        scene_.clearScene ();  // stop all scene activity
        if (sceneframe_ != null)
        {
          if (sceneframe_.isVisible ())
            sceneframe_.hide ();
          sceneframe_.dispose ();  // free frame resources
        }
        if (DEBUG)
          System.err.println ("Frame closed");
        scene_ = null;
        sceneframe_ = null;
      }
      catch (Throwable t)
      {
        System.err.println ("Error: " + t);
      }
    }
  } // destroy

  /**
   * start a thread to read streamed data.
   */

  public void startReading (String baseurl)
  {
    try
    {
      pin_ = new PipedInputStream ();
      pout_ = new PipedOutputStream (pin_);
      rthread_ = new Thread (vrreader_ = new VRwaveReader (scene_, pin_, baseurl));
      rthread_.start ();
    }
    catch (Throwable t)
    {
      System.err.println ("Error: " + t);
    }
  }

  /**
   * called when input is complete or to abort reading.
   */

  public void dataComplete ()
  {
    try
    {
      pout_.close ();
      // data are complete sent in pipe, parsing of buffered data has not yet finished
    }
    catch (Throwable t)
    {
      System.err.println ("Error: " + t);
    }
    // reader thread is not stopped here - give parser time to complete,
    // will complain about premature EOF itself when data is incomplete
  }

  /**
   * tell how many bytes can be read by readData without blocking.
   * only valid between startReading and dataComplete calls.
   */

  public int readableData ()
  {
    // PipedIOStreams have no method to get this information.
    // following code assumes a buffer size of at least 1024
    // which the PipedInputStream can fill without blocking.
    // method currently not unused.

    try
    {
      int inbuf = pin_.available ();  // >= 0
      if (inbuf > 1024)  // oops
        return 0;
      return (1024 - inbuf);
    }
    catch (Throwable t)
    {
      System.err.println ("Error: " + t);
      return 0;
    }
  } // readableData

  /**
   * read some chunk of data (by writing it into the pipe).
   * only valid between startReading and dataComplete calls.
   * be very careful not to lock netscape in this method.
   */

  public void readData (byte[] data, int len)
  {
    try
    {
      if (scene_ != null && vrreader_ != null && vrreader_.acceptdata_)
        pout_.write (data, 0, len);
    }
    catch (Throwable t)
    {
      System.err.println ("Error: " + t);
    }
  }

} // VRwavePlugin