home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 1999 May / APC452.ISO / netkit / xitami / xitami.exe / LRWP.JAV < prev    next >
Encoding:
Text File  |  1998-08-14  |  8.6 KB  |  329 lines

  1. /************************************************************************
  2.  
  3.                Copyright (c) 1998 by Eugen Woronenko
  4.  
  5. ------------------------------------------------------------------------
  6.  
  7. Class Name:     LRWP_Connection.java
  8.  
  9. Description:    The Java class providing interface to 
  10.                 assist with the creation of LRWP Peer applications.
  11.  
  12. Creation Date:  14/08/1998 
  13.  
  14. # License:      This is free software.  You may use this software for any
  15. #               purpose including modification/redistribution, so long as
  16. #               this header remains intact and that you do not claim any
  17. #               rights of ownership or authorship of this software.  This
  18. #               software has been tested, but no warranty is expressed or
  19. #               implied.
  20.  
  21. ************************************************************************/
  22.  
  23. import java.lang.*;
  24. import java.net.*;
  25. import java.io.*;
  26. import java.util.*;
  27.  
  28. /**********************************************************************************/
  29.  
  30. public class LRWP_Connection {
  31.  
  32.   protected static int lenSize=9;
  33.  
  34.   protected Socket sock=null;
  35.   protected DataInputStream in=null;
  36.   protected DataOutputStream out=null;
  37.  
  38.   protected byte[] postBytes=null;
  39.   protected byte[] envBytes=null;
  40.  
  41.   protected String error="";
  42.  
  43. /**********************************************************************************/
  44.   public LRWP_Connection() {}
  45.  
  46.  
  47. /**********************************************************************************
  48.     Method: main
  49.  
  50.     Demonstrates the class functionality. Can be useful for debugging.   
  51. **********************************************************************************/
  52.  
  53.   public static void main(String[] args) {
  54.     
  55.     LRWP_Connection t=new LRWP_Connection();
  56.     if (!t.connect("lrwp","localhost",81)) {
  57.         System.out.println(t.getError());
  58.     return;
  59.     }
  60.  
  61.     System.out.println("Connected!");
  62.  
  63.     for(int i=1;i<=3;i++) {
  64.  
  65.       if(t.accept_request()) {
  66.  
  67.         System.out.println(new String(t.getEnv()));
  68.  
  69.         System.out.println("\n");
  70.  
  71.         if(t.getPost()!=null) {
  72.           System.out.println(new String(t.getPost()));
  73.         }
  74.  
  75.         System.out.println("\n\n");
  76.  
  77.         String s="Content-type: text/plain\n\n *** Response " + i + " ***";
  78.  
  79.         if(!t.sendData(s.getBytes(),s.getBytes().length)) {
  80.           System.out.println(t.getError());
  81.           break;
  82.         }      
  83.  
  84.       } else {
  85.         System.out.println(t.getError());
  86.         break;  
  87.       }
  88.     }
  89.  
  90.     t.disconnect();
  91.   }
  92.  
  93. /**********************************************************************************
  94.     Method: connect
  95.  
  96.     Connects to the LRWP agent running in Xitami on host and port.  Sends
  97.     the given appname to use as the URI alias that will trigger requests to
  98.     be sent to this peer. 
  99.  
  100.     Returns true on success, false otherwise. getError() method than returns
  101.     a readable error message.
  102. **********************************************************************************/
  103.  
  104.   public boolean connect(String appname,       /* Name or alias of Peer app         */
  105.                          String host,          /* hostname/IP address to connect to */
  106.                          int port              /* port number to connect to         */
  107.                         ) 
  108.   {
  109.  
  110.   byte[] buf=new byte[1024];
  111.   int len=0;
  112.  
  113.   try {
  114.  
  115.     this.sock=new Socket(host, port);
  116.     sock.setSoTimeout(0);
  117.  
  118.   } catch(Exception e) {
  119.     cleanUp("Cannot open socket: "+e.toString());
  120.     return false;
  121.   }
  122.  
  123.   try {
  124.  
  125.     this.in=new DataInputStream(sock.getInputStream());
  126.  
  127.   } catch(Exception e) {
  128.     cleanUp("Cannot open DataInputStream: "+e.toString());
  129.     return false;
  130.   }    
  131.  
  132.   try {
  133.  
  134.   this.out=new DataOutputStream(sock.getOutputStream());    
  135.  
  136.   } catch(Exception e) {
  137.     cleanUp("Cannot open DataOutputStream: "+e.toString());
  138.     return false;
  139.   }
  140.  
  141.   /* send the startup sting */
  142.  
  143.   byte[] appnameBytes=appname.getBytes();
  144.   
  145.   for(int i=0;i<appnameBytes.length;i++)
  146.     buf[i]=appnameBytes[i];
  147.  
  148.   buf[appnameBytes.length]=(byte)255;
  149.   buf[appnameBytes.length+1]=(byte)255;
  150.  
  151.   try {
  152.  
  153.   out.write(buf,0,appnameBytes.length+2);  
  154.   out.flush();    
  155.  
  156.   } catch(Exception e) {
  157.     cleanUp("Cannot write startup string: "+e.toString());
  158.     return false;
  159.   }
  160.  
  161.   /* wait for an acknowledgement... */
  162.  
  163.   try {
  164.  
  165.   in.read(buf);    
  166.  
  167.   } catch(Exception e) {
  168.     cleanUp("Cannot read startup response: "+e.toString());
  169.     return false;
  170.   }
  171.  
  172.   if (new String(buf).startsWith("OK"))
  173.       return true;
  174.   else {
  175.       cleanUp(new String(buf)); 
  176.       return false;
  177.   }
  178. }
  179.  
  180. /**********************************************************************************
  181.     Method: accept_request
  182.  
  183.     This method waits for and recieves a request from the LRWP agent.
  184.     On return, the received POST and environment data can be accessed via
  185.     getPost() and getEnv() methods.
  186.  
  187.     Returns true on success, false otherwise. getError() method than returns
  188.     a readable error message.
  189. **********************************************************************************/
  190.  
  191.   public boolean accept_request() {
  192.     
  193.     postBytes =null;
  194.     envBytes  =null;
  195.     error     ="";
  196.  
  197.     byte[] lenBuf=new byte[lenSize];
  198.  
  199.     try {
  200.  
  201.       in.readFully(lenBuf,0,lenSize);
  202.  
  203.       int envLen=Integer.parseInt(new String(lenBuf));
  204.  
  205.       envBytes=new byte[envLen];
  206.  
  207.       in.readFully(envBytes,0,envLen);
  208.  
  209.       in.readFully(lenBuf,0,lenSize);
  210.       int postLen=Integer.parseInt(new String(lenBuf));
  211.  
  212.       if(postLen!=0) {
  213.         postBytes=new byte[postLen];
  214.         in.readFully(postBytes,0,postLen); 
  215.       }
  216.      
  217.       return true;    
  218.  
  219.     } catch(Exception e) {
  220.       error="Accept_request error: "+e.toString();
  221.       return false;
  222.     }
  223.   }
  224.  
  225. /**********************************************************************************
  226.     Method: getEnv
  227.  
  228.     This method returns the received environment data.
  229. **********************************************************************************/
  230.  
  231.   public byte[] getEnv() {
  232.  
  233.     return envBytes;
  234.   }
  235.  
  236. /**********************************************************************************
  237.     Method: getPost
  238.  
  239.     This method returns the received POST data or null if there isn't any.
  240. **********************************************************************************/
  241.  
  242.   public byte[] getPost() {
  243.  
  244.     return postBytes;
  245.   }
  246.  
  247. /**********************************************************************************
  248.     Method: sendData
  249.  
  250.     Sends the response data as array of bytes back to Xitami.
  251.  
  252.     Returns true on success, false otherwise. getError() method than returns
  253.     a readable error message.
  254. **********************************************************************************/
  255.  
  256.   public boolean sendData(byte[] data,  /* The data array */
  257.                           int len       /* The position in the array to start from */
  258.                          )
  259.   {
  260.     try {
  261.  
  262.       out.write(LRWPInt(len).getBytes(), 0, lenSize);
  263.       out.write(data, 0, len); 
  264.  
  265.       out.flush();    
  266.  
  267.       return true;
  268.  
  269.     } catch(Exception e) {
  270.       error="sendData error: "+e.toString();
  271.       return false;
  272.     }
  273.   }
  274.  
  275. /**********************************************************************************
  276.     Method: disconnect
  277.  
  278.     Closes the connection to Xitami.
  279.  
  280.     Returns true on success, false otherwise. getError() method than returns
  281.     a readable error message.
  282. **********************************************************************************/
  283.  
  284.   public boolean disconnect() {
  285.      return cleanUp("");
  286.   }
  287.  
  288. /**********************************************************************************
  289.     Method: getError
  290.  
  291.     Returns a readable error message.
  292. **********************************************************************************/
  293.  
  294.   public String getError() {
  295.     return error;
  296.   }
  297.  
  298. /**********************************************************************************/
  299.  
  300.   protected boolean cleanUp(String err) {
  301.  
  302.     try {
  303.  
  304.       if (sock!=null) sock.close();
  305.       this.error=err;
  306.       return true;
  307.  
  308.     } catch(Exception e) {
  309.       error="Cannot close the socket: "+e.toString();
  310.       return false;
  311.     }
  312.   }
  313.  
  314. /**********************************************************************************/
  315.  
  316.   protected String LRWPInt(int i) {
  317.  
  318.     String num=String.valueOf(i);
  319.     String nulls="";
  320.  
  321.     for(int x=0;x<this.lenSize-num.length();x++) {
  322.       nulls=nulls+"0";
  323.     }
  324.         
  325.     return nulls+num;
  326.   }
  327.  
  328. /**********************************************************************************/
  329. }