Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: raining/examples/ProxyServer.java


1   /*
2    * $Author: rahul_kumar $
3    * $Id: ProxyServer.java,v 1.5 2003/10/12 12:28:55 rahul_kumar Exp $
4    */
5    
6   package raining.examples;
7   import raining.core.*;
8   import java.util.*;
9   import java.nio.channels.*;
10  import java.nio.*;
11  
12  /**
13   * A very simple proxy server taking requests from clients and forwarding
14   * them to a server/s, and returning the results.
15   * This is based on the python proxy server example on
16   * nightmare.com/medusa/
17   * However, this is not pipelined - i need to make a
18   * PipelinedNioSocket.
19   *
20   * We could use the Server class already developed or just make on from
21   * scratch here. First we scratch, then reuse.
22   *
23   * handle_read in NioSocket needed a terminator check for connect and
24   * read cases also. it was only there for accept channels.
25   */
26  
27  public class ProxyServer extends NioSocket {
28  
29      public static final String TERMINATOR = "\n";
30      public String there_host;
31      public int    there_port;
32  
33      public static void main (String args[]){ 
34          try {
35              String host = "localhost";
36              int port = 80;
37  
38              if (args.length > 0){
39                  host = args[1];
40                  port = Integer.parseInt(args[2]);
41              }
42              else
43                  System.err.println(  ">>>> Defaulting to localhost and 80");
44  
45              ProxyServer ps = new ProxyServer("localhost",80);
46              NioSocket.setDebug(true);
47              NioSocket.start();
48          } catch (Exception exc) { System.err.println( "ProxyServer Main 22 EXC:"+ exc.toString()); exc.printStackTrace(); } 
49      }
50  
51      /** Constructor taking port and host of server to connect to. It
52       * will listen on 8000+port on localhost and proxy with host+port.
53       */
54      public ProxyServer (String host, int port) throws Exception {
55          super();
56          try {
57              this.create_server_socket(8000 + port);
58              this.there_host = host;
59              this.there_port = port;
60              //NioSocket.start();
61          } catch (Exception exc) { System.err.println( "ProxyServer 30 EXC:"+ exc.toString()); exc.printStackTrace(); } 
62  
63      }
64      /** create our own receiver object with the incoming channel.
65       */
66      public void handle_accept(){
67          try {
68              SocketChannel schannel = this.accept();
69              //NioSocket servsock = new NioSocket (schannel);
70              System.err.println(  "ACC:"+ schannel.socket().getInetAddress() ); // XXX see if this gets overriden
71              ProxyReceiver rcvr = new ProxyReceiver (this, schannel);
72          } catch (Exception exc) { System.err.println( "ProxyServer handle accept 675 EXC:"+ exc.toString()); exc.printStackTrace(); } 
73      }
74  
75  
76  } // end of class
77  
78  
79  class ProxyReceiver extends NioSocket {
80  
81      static long counter = 0;
82  
83      ProxyServer server;
84      long id;
85      ProxySender sender;
86  
87      public ProxyReceiver (ProxyServer ps, SocketChannel sc) throws Exception {
88  
89          super (sc);
90  
91          System.out.println(  "constr of Pr rcv");
92          //this.setTerminator(ProxyServer.TERMINATOR);
93          this.server = ps;
94          this.id = counter++;
95          this.sender = new ProxySender (this, 
96                  server.there_host, 
97                  server.there_port);
98          this.sender.id = this.id;
99          // this.self_buffer = ''; // XXX
100     } 
101     //public void handle_terminator(String mdata)
102     // System.err.println(  "ProxyReceiver: handle_terminator ..");
103 
104     public void handle_read_complete(String mdata){
105 
106         System.err.println(  "ProxyReceiver: handle_read_complete ..");
107         System.out.println(  "<== " + this.id + " " + mdata);
108         clear_read_buffer();
109         this.sender.setSendData( mdata + ProxyServer.TERMINATOR );
110 
111     }
112     public void handle_close(){
113         System.err.println(  "ProxyReceiver: closing ..");
114         this.sender.nio_close();
115         this.nio_close();
116     }
117     public void handle_write_complete(){
118         //handle_close();
119     }
120     public boolean is_read_complete(){
121         return (rsb.indexOf(ProxyServer.TERMINATOR) > -1);
122     }
123 
124 } // end of class ProxyReceiver
125 
126 class ProxySender extends NioSocket {
127 
128 
129     ProxyReceiver rcvr;
130     
131     /** constructor
132      */
133     public ProxySender ( ProxyReceiver rcvr, String host, int port)
134     throws Exception {
135 
136         super();
137         this.rcvr = rcvr;
138         //this.setTerminator(null); // ???
139         this.create_client_socket(host, port);
140         //this.setTerminator(ProxyServer.TERMINATOR);
141     }
142     //public void handle_terminator(String mdata)
143     //    System.err.println(  "ProxySender: handle_terminator ..");
144 
145     public void handle_read_complete(String mdata){
146 
147         System.err.println(  "ProxyReceiver: handle_read_complete ..");
148         System.err.println(  "==> " + this.id + " " + mdata.length());
149         clear_read_buffer();
150         this.rcvr.setSendData( mdata + ProxyServer.TERMINATOR );
151 
152     }
153     public void handle_close(){
154         System.err.println(  "ProxySender: closing ..");
155         this.rcvr.nio_close();
156         this.nio_close();
157     }
158     public void handle_connect(){
159         System.err.println(  "ProxySender: connect ..");
160     }
161     public void handle_write_complete(){
162         System.err.println(  "PS:please override handle_write_complete");
163         //handle_close();
164     }
165     public boolean is_read_complete(){
166         return (rsb.indexOf(ProxyServer.TERMINATOR) > -1);
167     }
168 
169 
170 } // end of class ProxySender
171