KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcraft > jhttptunnel > IOBoundDoJa


1 /* -*-mode:java; c-basic-offset:2; -*- */
2 /*
3 Copyright (c) 2005 ymnk, JCraft,Inc. All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8   1. Redistributions of source code must retain the above copyright notice,
9      this list of conditions and the following disclaimer.
10
11   2. Redistributions in binary form must reproduce the above copyright
12      notice, this list of conditions and the following disclaimer in
13      the documentation and/or other materials provided with the distribution.
14
15   3. The names of the authors may not be used to endorse or promote products
16      derived from this software without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */

29
30 package com.jcraft.jhttptunnel;
31
32 import java.io.*;
33 import javax.microedition.io.*;
34 import com.nttdocomo.io.*;
35
36 public class IOBoundDoJa{
37   OutBoundDoJa obdj=null;
38   InBoundDoJa ibdj=null;
39   private String JavaDoc sessionid=null;
40
41   public IOBoundDoJa(){
42     obdj=new OutBoundDoJa();
43     ibdj=new InBoundDoJa();
44   }
45   public OutBound getOutBound(){return obdj;}
46   public InBound getInBound(){return ibdj;}
47
48   class OutBoundDoJa extends OutBound{
49     private InputStream in=null;
50     private OutputStream out=null;
51     private HttpConnection con=null;
52     private final byte[] _TUNNEL_DISCONNECT={(byte)0x47};
53
54     private int count=0;
55     boolean connected=false;
56
57     public void connect() throws IOException{
58 //System.out.println("OutBound: connect");
59

60       String JavaDoc host=getHost();
61       int port=getPort();
62
63       String JavaDoc uri="http://"+host+":"+port+"/index.html?crap=1"+"&count="+count;
64       if(sessionid!=null){
65         uri=uri+"&SESSIONID="+sessionid;
66       }
67 //System.out.println("uri: "+uri);
68
con=(HttpConnection)Connector.open(uri, Connector.READ_WRITE, true);
69       count++;
70       con.setRequestMethod(HttpConnection.POST);
71       out=con.openOutputStream();
72       sendCount=getContentLength();
73     }
74
75     public synchronized void sendData(byte[] foo, int s, int l, boolean flush) throws IOException{
76 //System.out.println("sendData: l="+l+" sendCount="+sendCount+" flush="+flush);
77
if(foo!=null && l<=0) return;
78
79       if(con==null){
80     connect();
81       }
82
83       if(sendCount<=0){
84     connect();
85       }
86
87       int retry=2;
88       while(retry>0){
89     try{
90       if(l>0)
91         out.write(foo, s, l);
92       sendCount-=l;
93
94 // if(flush){
95
out.close();
96         out=null;
97         try{
98           con.connect();
99         }
100         catch(ConnectionException e){
101           try{con.close();}catch(Exception JavaDoc ee){System.out.println(e);}
102           con=null;
103           if(foo==null){ // data retrieve
104
connect();
105                 retry--;
106         continue;
107           }
108           ibdj.connected=false;
109               return;
110         }
111
112         sessionid=con.getHeaderField("x-SESSIONID");
113 //System.out.println("sessionid: "+sessionid);
114
in=con.openInputStream();
115         readData();
116 // sendCount=0;
117

118 // return;
119
// }
120

121       return;
122     }
123     catch(IOException e){
124       connect();
125     }
126     retry--;
127       }
128     }
129
130     private void readData() throws IOException{
131       //System.out.println("readData: "+this+" con="+con+" in="+in);
132
if(con!=null){
133     if(out!=null){ try{out.close(); out=null;}catch(IOException e){} }
134     long datalen=con.getLength();
135     //System.out.println("datalen: "+datalen);
136
if(in!=null){
137       try{
138         while(true){
139           //System.out.println("now read");
140
int c=in.read();
141           //System.out.println("c="+c);
142
if(c==-1)break;
143           while(true){
144         if(ibdj.space()>=1){
145           break;
146         }
147         try{Thread.sleep(1000);}catch(Exception JavaDoc e){}
148           }
149           ibdj.push(c);
150         }
151         in.close(); in=null;
152       }
153       catch(IOException e){}
154     }
155     try{con.close();}catch(Exception JavaDoc e){System.out.println(e);}
156     con=null;
157       }
158     //System.out.println("close() done");
159
}
160
161     public void close() throws IOException{
162       connected=false;
163     }
164   }
165
166   public class InBoundDoJa extends InBound{
167     private InputStream in=null;
168     private HttpConnection con=null;
169
170     boolean connected=false;
171     public void connect() throws IOException{
172       connected=true;
173     }
174
175     public int receiveData(byte[] buf, int s, int l) throws IOException{
176 //System.out.println("receiveData: buf="+buf+" l="+l);
177
if(l<=0){ return -1; }
178       int retry=2;
179       while(connected){
180     synchronized(this){
181       int bl=be-bs;
182 //System.out.println(" connected="+connected+" bl="+bl);
183
if(bl>0){
184         if(buf==null){
185           if(bl>l){ bl=l; }
186           bs+=bl;
187           System.arraycopy(this.buf, bs, this.buf, 0, be-bs);
188           be=be-bs;
189           bs=0;
190           if(bl==l){
191         return -1; // ??
192
}
193           l-=bl;
194           continue;
195         }
196         if(bl>l) bl=l;
197         System.arraycopy(this.buf, bs, buf, s, bl);
198         bs+=bl;
199         System.arraycopy(this.buf, bs, this.buf, 0, be-bs);
200         be=be-bs;
201         bs=0;
202         return bl;
203       }
204     }
205         if(retry>0){
206           retry--;
207       obdj.sendData(null, 0, 0, true);
208       obdj.readData();
209 //System.out.println("be-bs="+(be-bs));
210
if(be-bs>0){ continue; }
211
212       /*
213           if(blockToRead>0){
214         try{ Thread.sleep(blockToRead); }
215         catch(Exception e){ }
216             continue;
217       }
218       else if(blockToRead==0){ // non block
219             return 0;
220       }
221           else{
222         try{ Thread.sleep(10000); } // block foreaver
223         catch(Exception e){ }
224             retry=1;
225             continue;
226       }
227       */

228
229       try{ Thread.sleep(5000); }
230       catch(Exception JavaDoc e){ }
231       continue;
232
233     }
234     else{
235           return 0;
236     }
237     //return 0;
238
}
239       return -1;
240     }
241
242 // byte[] buf=new byte[1024];
243
byte[] buf=new byte[4096];
244     int bs=0;
245     int be=0;
246     public synchronized int space(){
247       return buf.length-be;
248     }
249     public synchronized void push(int c){
250 //System.out.println("push: "+c);
251
buf[bs+be]=(byte)c; be++;
252 //System.out.println(" be: "+be);
253
}
254     public void close() throws IOException{
255       connected=false;
256     }
257   }
258 }
259
Popular Tags