Source code: com/prolifics/servlet/Comm.java
1 /*************************************************/
2 /* Copyright (c) 1999 */
3 /* by */
4 /* JYACC, Inc., New York NY USA */
5 /* and contributors. */
6 /* Use of this program is governed by the */
7 /* JYACC Public License Version 1.0, a copy of */
8 /* which can be obtained at */
9 /* http://www.possl.org/jyacc-license.html */
10 /*************************************************/
11
12 /* @(#)Comm.java 77.1 99/04/22 15:03:03 */
13
14 package com.prolifics.servlet;
15 import java.io.*;
16 import java.net.*;
17 import java.util.*;
18
19
20 /**
21 * test servlet. This servlet simply echos back the request line and
22 * headers that were sent by the client, plus any HTTPS information
23 * which is accessible.
24 *
25 * @version @(#)Comm.java 77.1 99/04/22 15:03:03
26 * @author Prolifics
27 */
28 class Comm
29 {
30 private static String sccsid = "@(#)Comm.java 77.1 99/04/22 15:03:03";
31 private InputStream in;
32 private OutputStream out;
33 private int fd = -1;
34 private Socket sock = null;
35
36 public static void main(String args[])
37 throws IOException
38 {
39 System.out.println("0 = " + args[0]);
40 System.out.println("1 = " + args[1]);
41 new Comm().oldopen(args[0], args[1]);
42 }
43
44 static
45 {
46 System.loadLibrary("prowebjni");
47 /*
48 try
49 {
50 System.load("/u/home/eric/servlets/libprowebjni.so");
51 }
52 catch (Throwable ignored)
53 {
54 System.load("h:\\servlets\\prowebjni.dll");
55 }
56 */
57 ninit();
58 }
59
60 private native int nopen(byte name[], int fd)
61 throws IOException;
62 private native void nclose(int fd) throws IOException;
63 private static native void ninit();
64 private static native synchronized int ngetindex();
65 private static native void nfreeindex(int index);
66
67 public Comm()
68 {
69 fd = ngetindex();
70 if (fd < 0)
71 {
72 throw new IndexOutOfBoundsException();
73 }
74 }
75
76 public void destroy()
77 {
78 if (fd >= 0)
79 {
80 nfreeindex(fd);
81 fd = -1;
82 }
83 }
84
85 protected void finalize() throws Throwable
86 {
87 close();
88 destroy();
89 super.finalize();
90 }
91
92
93 public static byte[] stringToBytes(String src)
94 {
95 return src.getBytes(/* encoding */);
96 }
97
98 public static String bytesToString(byte src[], int offset, int len)
99 {
100 return new String(src, offset, len /* , encoding */);
101 }
102
103 public void open(String name, String URL)
104 throws IOException
105 {
106 if (ngetArchitecture() == 0)
107 {
108 oldopen(name, URL);
109 return;
110 }
111
112 App WebApp = App.getApp(name);
113
114 synchronized(WebApp)
115 {
116 int tim = 100;
117
118 for (int count = 0; count < 20; count++)
119 {
120 if (count > 0)
121 {
122 try
123 {
124 Thread.sleep(tim);
125 }
126 catch (InterruptedException ignored)
127 {
128 }
129 }
130
131 if (nopen(stringToBytes(name), fd) == 0)
132 {
133 in = new CommInputStream(fd);
134 out = new CommOutputStream(fd);
135
136 in = new BufferedInputStream(in);
137 out = new BufferedOutputStream(out);
138
139 return;
140 }
141 /* tim *= 2; */
142 }
143 }
144
145 throw new IOException();
146 }
147
148 private static native int ngetArchitecture();
149 private native int ngetDispatcherPort(byte name[]) throws IOException;
150 private native int ngetInitialReadSize() throws IOException;
151 private native int nprepareRequest(byte buf[], byte name[], byte URL[], int fd);
152 private native int ngetLengthToRead(byte buf[]) throws IOException;
153 private native int ngetServerPort(byte buf[]) throws IOException;
154
155 public void oldopen(String name, String URL)
156 throws IOException
157 {
158 int dispatchport = 0;
159 int port = 0;
160 int lengthToRead = ngetInitialReadSize();
161 int lengthRead = 0;
162 int lengthToWrite = 0;
163 byte buf[] = new byte[1152];
164 Socket dispatchsock = null;
165 try
166 {
167 dispatchport = ngetDispatcherPort(stringToBytes(name));
168 }
169 catch (Exception ignored)
170 {
171 dispatchport = ngetDispatcherPort(stringToBytes("Bronze"));
172 }
173
174 dispatchsock = new Socket(InetAddress.getLocalHost(), dispatchport);
175 dispatchsock.setTcpNoDelay(true);
176 InputStream dispatchin = dispatchsock.getInputStream();
177 OutputStream dispatchout = dispatchsock.getOutputStream();
178 lengthToWrite = nprepareRequest(buf,
179 stringToBytes(name), stringToBytes(URL), fd);
180 dispatchout.write(buf, 0, lengthToWrite);
181 dispatchout.flush();
182 lengthRead = dispatchin.read(buf, 0, lengthToRead);
183 if (lengthToRead != lengthRead)
184 {
185 throw new IOException();
186 }
187 lengthToRead = ngetLengthToRead(buf);
188 lengthToRead -= lengthRead;
189 lengthRead = dispatchin.read(buf, lengthRead, lengthToRead);
190 if (lengthToRead != lengthRead)
191 {
192 throw new IOException();
193 }
194 port = ngetServerPort(buf);
195 System.out.println(port);
196 if (port > 0)
197 {
198 dispatchin.close();
199 dispatchout.close();
200 dispatchsock.close();
201 sock = new Socket(InetAddress.getLocalHost(), port);
202 sock.setTcpNoDelay(true);
203 in = sock.getInputStream();
204 out = sock.getOutputStream();
205 }
206 else
207 {
208 in = dispatchin;
209 out = dispatchout;
210 sock = dispatchsock;
211 }
212 }
213
214 public void close()
215 throws IOException
216 {
217 if (in != null)
218 {
219 try
220 {
221 in.close();
222 }
223 catch (IOException ignored)
224 {
225 }
226 }
227
228 if (out != null)
229 {
230 try
231 {
232 out.close();
233 }
234 catch (IOException ignored)
235 {
236 }
237 }
238
239 in = null;
240 out = null;
241 nclose(fd);
242 if (sock != null)
243 {
244 sock.close();
245 }
246 }
247
248 public InputStream getInputStream()
249 throws IOException
250 {
251 return in;
252 }
253
254 public OutputStream getOutputStream()
255 throws IOException
256 {
257 return out;
258 }
259 }