2 |
26 Feb 07 |
jari |
1 |
/* |
2 |
26 Feb 07 |
jari |
Copyright @ 1999-2003, The Institute for Genomic Research (TIGR). |
2 |
26 Feb 07 |
jari |
All rights reserved. |
2 |
26 Feb 07 |
jari |
4 |
*/ |
2 |
26 Feb 07 |
jari |
5 |
/* |
2 |
26 Feb 07 |
jari |
* $RCSfile: PVMResponseAcceptStrategy.java,v $ |
2 |
26 Feb 07 |
jari |
* $Revision: 1.3 $ |
2 |
26 Feb 07 |
jari |
* $Date: 2005/03/10 15:30:16 $ |
2 |
26 Feb 07 |
jari |
* $Author: braistedj $ |
2 |
26 Feb 07 |
jari |
* $State: Exp $ |
2 |
26 Feb 07 |
jari |
11 |
*/ |
2 |
26 Feb 07 |
jari |
12 |
package org.tigr.remote.gateway; |
2 |
26 Feb 07 |
jari |
13 |
|
2 |
26 Feb 07 |
jari |
14 |
import java.io.BufferedInputStream; |
2 |
26 Feb 07 |
jari |
15 |
import java.io.ByteArrayOutputStream; |
2 |
26 Feb 07 |
jari |
16 |
import java.io.IOException; |
2 |
26 Feb 07 |
jari |
17 |
import java.io.OutputStream; |
2 |
26 Feb 07 |
jari |
18 |
import java.io.PrintWriter; |
2 |
26 Feb 07 |
jari |
19 |
|
2 |
26 Feb 07 |
jari |
20 |
import javax.servlet.http.HttpServletRequest; |
2 |
26 Feb 07 |
jari |
21 |
import javax.servlet.http.HttpServletResponse; |
2 |
26 Feb 07 |
jari |
22 |
|
2 |
26 Feb 07 |
jari |
23 |
import org.tigr.remote.gateway.util.IQueue; |
2 |
26 Feb 07 |
jari |
24 |
import org.tigr.util.ConfMap; |
2 |
26 Feb 07 |
jari |
25 |
|
2 |
26 Feb 07 |
jari |
26 |
class PVMResponseAcceptStrategy { |
2 |
26 Feb 07 |
jari |
27 |
private static final int MAX_PACKET_SIZE = 1024 * 100; // 100K packet |
2 |
26 Feb 07 |
jari |
28 |
private static final int MAX_MESSAGES_IN_QUEUE = 10; // Maximum messages not sent |
2 |
26 Feb 07 |
jari |
29 |
|
2 |
26 Feb 07 |
jari |
30 |
/** |
2 |
26 Feb 07 |
jari |
* Constructs a <code>PVMResponseAcceptStrategy</code> with specified |
2 |
26 Feb 07 |
jari |
* session and configuration. |
2 |
26 Feb 07 |
jari |
33 |
*/ |
2 |
26 Feb 07 |
jari |
34 |
public PVMResponseAcceptStrategy( SessionState session, ConfMap cfg ) { |
2 |
26 Feb 07 |
jari |
35 |
m_session = session; |
2 |
26 Feb 07 |
jari |
36 |
m_maxSize = cfg.getInt("gateway.cache.max-packet-size", MAX_PACKET_SIZE); |
2 |
26 Feb 07 |
jari |
37 |
m_maxMessages = cfg.getInt("gateway.cache.max-messages-in-queue", MAX_MESSAGES_IN_QUEUE ); |
2 |
26 Feb 07 |
jari |
38 |
} |
2 |
26 Feb 07 |
jari |
39 |
|
2 |
26 Feb 07 |
jari |
40 |
/** |
2 |
26 Feb 07 |
jari |
* Accepts a PVM response. |
2 |
26 Feb 07 |
jari |
42 |
*/ |
2 |
26 Feb 07 |
jari |
43 |
public void acceptPVMResponse( HttpServletRequest req, HttpServletResponse resp) throws IOException, InterruptedException { |
2 |
26 Feb 07 |
jari |
44 |
int packetSize = req.getContentLength(); |
2 |
26 Feb 07 |
jari |
45 |
if (req.getContentLength() < m_maxSize) { |
2 |
26 Feb 07 |
jari |
46 |
ByteArrayOutputStream tmp = new ByteArrayOutputStream( packetSize ); |
2 |
26 Feb 07 |
jari |
47 |
BufferedInputStream in = new BufferedInputStream( req.getInputStream() ); |
2 |
26 Feb 07 |
jari |
48 |
int counter = 0; |
2 |
26 Feb 07 |
jari |
49 |
int c = 0; |
2 |
26 Feb 07 |
jari |
50 |
while (( ( c = in.read() ) != -1 ) && ( counter < packetSize )) { |
2 |
26 Feb 07 |
jari |
51 |
++counter; |
2 |
26 Feb 07 |
jari |
52 |
tmp.write( c ); |
2 |
26 Feb 07 |
jari |
53 |
} |
2 |
26 Feb 07 |
jari |
54 |
String response = tmp.toString(); |
2 |
26 Feb 07 |
jari |
55 |
IQueue queue = m_session.getMessageQueue(); |
2 |
26 Feb 07 |
jari |
56 |
if (queue.getSize() >= m_maxMessages) queue.getHead(); |
2 |
26 Feb 07 |
jari |
57 |
queue.addTail( response ); |
2 |
26 Feb 07 |
jari |
58 |
} else { // |
2 |
26 Feb 07 |
jari |
59 |
synchronized( m_session ) { |
2 |
26 Feb 07 |
jari |
60 |
m_session.createMonitor(); |
2 |
26 Feb 07 |
jari |
61 |
m_session.wait(); |
2 |
26 Feb 07 |
jari |
62 |
} |
2 |
26 Feb 07 |
jari |
63 |
BufferedInputStream in = new BufferedInputStream( req.getInputStream() ); |
2 |
26 Feb 07 |
jari |
64 |
OutputStream out_pipe = m_session.getOutputPipe(); |
2 |
26 Feb 07 |
jari |
65 |
|
2 |
26 Feb 07 |
jari |
66 |
byte[] b = new byte[1024*100]; |
2 |
26 Feb 07 |
jari |
67 |
int cnt; |
2 |
26 Feb 07 |
jari |
68 |
while ((cnt = in.read(b)) >= 0) { |
2 |
26 Feb 07 |
jari |
69 |
out_pipe.write(b, 0, cnt); |
2 |
26 Feb 07 |
jari |
70 |
} |
2 |
26 Feb 07 |
jari |
71 |
out_pipe.flush(); |
2 |
26 Feb 07 |
jari |
72 |
out_pipe.close(); |
2 |
26 Feb 07 |
jari |
73 |
} |
2 |
26 Feb 07 |
jari |
74 |
IQueue queue = m_session.getRequestQueue(); |
2 |
26 Feb 07 |
jari |
75 |
if (! queue.isEmpty()) { |
2 |
26 Feb 07 |
jari |
76 |
String request = (String)queue.getHead(); |
2 |
26 Feb 07 |
jari |
77 |
PrintWriter p = new PrintWriter( resp.getOutputStream() ); |
2 |
26 Feb 07 |
jari |
78 |
p.print( request ); |
2 |
26 Feb 07 |
jari |
79 |
p.flush(); |
2 |
26 Feb 07 |
jari |
80 |
p = null; |
2 |
26 Feb 07 |
jari |
81 |
} |
2 |
26 Feb 07 |
jari |
82 |
} |
2 |
26 Feb 07 |
jari |
83 |
|
2 |
26 Feb 07 |
jari |
84 |
SessionState m_session; |
2 |
26 Feb 07 |
jari |
85 |
int m_maxSize; |
2 |
26 Feb 07 |
jari |
86 |
int m_maxMessages; |
2 |
26 Feb 07 |
jari |
87 |
} |