Changeset 2109


Ignore:
Timestamp:
2001-03-05T09:29:42+13:00 (23 years ago)
Author:
say1
Message:

P2P work. still not complete

Location:
trunk/java-client
Files:
6 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/java-client/Makefile

    r2104 r2109  
    1818
    1919all : org/nzdl/gsdl/corba/gsdlInterface/corbaiface.java
    20     (cd gnu/getopt && make all)
    21     (cd org/nzdl/gsdl && make all)
     20    (cd gnu/getopt && $(MAKE) all)
     21    (cd org/nzdl/gsdl && $(MAKE) all)
    2222    diff --brief ./corbaiface.idl $(GREENSTONEIDLFILE)
    2323
     
    3636
    3737clean :
    38     (cd org/nzdl/gsdl && make clean)
     38    (cd gnu/getopt && $(MAKE) clean)
     39    (cd org/nzdl/gsdl && $(MAKE) clean)
    3940    rm -rf docs
    4041    rm -rf org/nzdl/gsdl/corba
     
    4445
    4546test : all
    46     java -classpath $(CLASSPATH) org.nzdl.gsdl.SimpleClient -a
     47    java -classpath "$(CLASSPATH)" org.nzdl.gsdl.SimpleClient -a
    4748
    4849many : all
    49     java -classpath $(CLASSPATH) org.nzdl.gsdl.SearchAllCollections -q "marriage customs"
     50    java -classpath "$(CLASSPATH)" org.nzdl.gsdl.SearchAllCollections -q "marriage customs"
    5051    echo "This is causing a bunch of corba protocol errors for me.  How about you?"
     52
  • trunk/java-client/org/nzdl/gsdl/Makefile

    r2102 r2109  
    2222#JAVACOPTIONS= -g:none -O
    2323
    24 %.class : %.java subdirs
     24%.class : %.java
    2525    $(JAVAC) $(JAVACOPTIONS)  $<
    2626
  • trunk/java-client/org/nzdl/gsdl/ptp/Node.java

    r2087 r2109  
    1919
    2020// the package we're in
    21 package org.nzdl.ptp;
     21package org.nzdl.gsdl.ptp;
    2222
    2323// java standard library classes used
     
    3434import java.net.*;
    3535
     36
    3637/**
    3738 * Class ...
     
    4748 */
    4849
    49 class Connection extends Thread
    50 {
    51   // queries ...
    52   public static final String WHOAREYOU = "WHO ARE YOU?";
    53   public static final String WHODOYOUKNOW = "WHO DO YOU KNOW?";
    54   public static final String HELPREQUEST = "HELP?";
    55   public static final String WHATVERSIONAREYOU = "WHAT VERSION ARE YOU?";
    56   public static final String OUT = "OUT.";
    57 
    58   // responses
    59   public static final String IAMIOR = "I AM IOR:";
    60   public static final String IAMHTTP = "I AM HTTP:";
    61   public static final String IAMP2P = "I AM P2P:";
    62 
    63   public static final String IKNOWIOR = "I AM IOR:";
    64   public static final String IKNOWHTTP = "I AM HTTP:";
    65   public static final String IKNOWP2P = "I AM P2P:";
    66 
    67   public static final String HELPRESPONSE = "HELP "; // note the space ...
    68 
    69   public static final String VERSION = "VERSION ";
    70 
    71   boolean server = false;
    72   boolean connectionClosed = false;
    73 
    74   int port = 0;
    75   String remoteHostName = "";
    76   InetAddress  remoteHost = null;
    77 
    78   Socket socket = null;
    79   Reader input = null;
    80   Writer output = null;
    81  
    82   Connection(String host, int port) {
    83     new Thread("Client") ;
    84     server = false;
    85 
    86     this.port = port;
    87     this.remoteHostName = host;
    88     try {
    89       remoteHost = InetAddress.getByName(remoteHostName);
    90       socket = new Socket(remoteHost, port, InetAddress.getLocalHost(), 4567);
    91     } catch (Exception exception) {
    92       System.err.println("caught exception: "+exception);
    93     }
    94     try {
    95       input = new InputStreamReader(new BufferedInputStream(socket.getInputStream()));
    96       output = new OutputStreamWriter(new BufferedOutputStream(socket.getOutputStream())); 
    97     } catch (IOException exception) {
    98       System.err.println("caught exception: "+exception);
    99     }
    100   }
    101   Connection(Socket socket) {
    102     new Thread("Server") ;
    103     server = true;
    104     setPriority(MIN_PRIORITY);
    105 
    106     this.socket = socket;
    107     port = socket.getPort();
    108     remoteHost = socket.getInetAddress();
    109     remoteHostName = remoteHost.getHostName();
    110 
    111     try {
    112       input = new InputStreamReader(new BufferedInputStream(socket.getInputStream()));
    113       output = new OutputStreamWriter(new BufferedOutputStream(socket.getOutputStream())); 
    114     } catch (IOException exception) {
    115       System.err.println("caught exception: "+exception);
    116     }
    117   }
    118 
    119   String readline() throws IOException {
    120     StringBuffer buff = new StringBuffer(1000);
    121     while (true) {
    122       int c = input.read();
    123       if (c == '\n' || c == '\r')
    124     return buff.toString();
    125       buff.append((char)c);
    126     }
    127   }
    128 
    129   public void run() {
    130     try {
    131       while (!server || !connectionClosed) {
    132     String line = readline();   
    133     reply(line);
    134       }
    135     } catch (Exception exception) {
    136       System.err.println("caught exception:" + exception);
    137       connectionClosed = true;
    138     }
    139   }
    140  
    141   int reply(String line) throws IOException {
    142 
    143     String first = line.substring(0,WHOAREYOU.length());
    144     if (first.compareToIgnoreCase(WHOAREYOU) == 0) {
    145       System.err.println("We were asked who we are ...");
    146       return whoWeAre();
    147     }
    148 
    149     first = line.substring(0,WHODOYOUKNOW.length());
    150     if (first.compareToIgnoreCase(WHODOYOUKNOW) == 0) {
    151       System.err.println("We were asked who we know ...");
    152       return whoWeKnow();
    153     }
    154 
    155     first = line.substring(0,HELPREQUEST.length());
    156     if (first.compareToIgnoreCase(HELPREQUEST) == 0) {
    157       System.err.println("We were asked for help ...");
    158       return help();
    159     }
    160 
    161     first = line.substring(0,WHATVERSIONAREYOU.length());
    162     if (first.compareToIgnoreCase(WHATVERSIONAREYOU) == 0) {
    163       System.err.println("We were asked what verrsion we are ...");
    164       return whatVersionAreWe();
    165     }
    166 
    167     first = line.substring(0,OUT.length());
    168     if (first.compareToIgnoreCase(OUT) == 0) {
    169       System.err.println("We were asked to close the connection ...");
    170       connectionClosed = true;
    171       return gracefulClose();
    172     }
    173     System.err.println("Nothing matched \"" + line + "\" sending help ...");
    174     return help();
    175   }
    176 
    177   int gracefulClose()
    178   {
    179     return -1;
    180   }
    181 
    182 
    183   int whoWeAre() throws IOException {
    184     output.write("I AM IOR:1234567890");
    185     output.write("I AM HTTP://www.someplace");
    186     output.write("I AM P2P:www.someplace:4444");
    187     return 3;
    188   }
    189 
    190   int whoWeKnow() throws IOException {
    191     output.write("I KNOW IOR:12345678901");
    192     output.write("I KNOW IOR:12345678902");
    193     output.write("I KNOW HTTP://www.someplace.else");
    194     output.write("I KNOW HTTP://www.someplace.elsewhere");
    195     output.write("I KNOW P2P:www.someplace:4444:A");
    196     output.write("I KNOW P2P:www.someplace:4444:B");
    197     return 6;
    198   }
    199 
    200   int help() throws IOException {
    201     output.write("HELP: This is NZDL P2P protocol $Revision$");
    202     output.write("HELP: There are five valid commands:");
    203     output.write("HELP: WHO ARE YOU?");
    204     output.write("HELP: WHO DO YOU KNOW?");
    205     output.write("HELP: WHAT VERSION ARE YOU?");
    206     output.write("HELP: HELP");
    207     output.write("HELP: OUT");
    208     return 7;
    209   }
    210 
    211   int whatVersionAreWe() throws IOException {
    212     output.write("VERSION: Protocol: NZDL-P2P-0.1 ");
    213     output.write("VERSION: Implementation: $Revision$");
    214     return 2;
    215   }
    216 }
    217   /**
    218    *
    219    *
    220    * @exception java.io.IOException when ...
    221    * @param arg2 ...
    222    * @param arg1 ...
    223    * @return ...
    224    * @see
    225    * @see
    226    */
    227 
    228 
    229 class Poller extends Thread {
    230   int secondsBetweenPollCycles = 60;
    231  
    232  
    233   public void run() {
    234     while (true) {
    235       poll();
    236       try {
    237     sleep(secondsBetweenPollCycles * 1000);
    238       } catch (InterruptedException exception) {
    239     System.err.println("caught exception: "+exception);
    240     return;
    241       }
    242     }
    243   }
    244 
    245   void poll() {
    246     Enumeration e = Node.knownList.propertyNames();
    247     while (e.hasMoreElements()) {
    248       String next = (String) e.nextElement();
    249       if (P2P.valid(next)) {
    250     System.err.println("About to create new thread for " + next);
    251     Connection connection =
    252       new Connection(P2P.toHost(next),P2P.toPort(next));
    253     System.err.println("About to start new thread");
    254     connection.run();
    255     System.err.println("About to wait for new thread");
    256     try {
    257       connection.join();
    258     } catch (InterruptedException exception) {
    259       System.err.println("caught exception: "+ exception);
    260       return;
    261     }
    262     System.err.println("Joined with new thread");
    263       }
    264     }
    265   }
    266 
    267 }
    268 
    269 class IOR {
    270   // TODO
    271   String clean (String str) { return str; } ;
    272 
    273   boolean valid (String str) {
    274     String match = "IOR:";
    275     String first = str.substring(0,match.length());
    276     if (first.compareToIgnoreCase(match) == 0) {
    277       return true;
    278     }
    279     return false;
    280   }
    281 
    282 }
    283 class P2P {
    284   // TODO
    285   static String clean (String str) { return str; } ;
    286 
    287   static boolean valid (String str) {
    288     String match = "P2P:";
    289     String first = str.substring(0,match.length());
    290     if (first.compareToIgnoreCase(match) == 0) {
    291       return true;
    292     }
    293     return false;
    294   }
    295   static int toPort (String str) {
    296     StringBuffer leading = new StringBuffer();
    297     StringBuffer host = new StringBuffer();
    298     StringBuffer port = new StringBuffer();
    299     StringBuffer trailing = new StringBuffer();
    300 
    301     int i = 0;
    302     while(i < str.length() && str.charAt(i) != ':') {
    303       leading.append(str.charAt(i));
    304       i++;
    305     }
    306     i++;
    307     while(i < str.length() && str.charAt(i) != ':') {
    308       host.append(str.charAt(i));
    309       i++;
    310     }
    311     i++;
    312     while(i < str.length() && Character.isDigit(str.charAt(i)) ) {
    313       port.append(str.charAt(i));
    314       i++;
    315     }
    316     while(i < str.length() ) {
    317       trailing.append(str.charAt(i));
    318       i++;
    319     }
    320     System.err.println("P2P parsed \"" + str + "\" into host = \"" +
    321                host + "\" and port = \"" + port + "\"");
    322     return Integer.parseInt(port.toString());
    323    
    324   }
    325   static String toHost (String str) {
    326     StringBuffer leading = new StringBuffer();
    327     StringBuffer host = new StringBuffer();
    328     StringBuffer port = new StringBuffer();
    329     StringBuffer trailing = new StringBuffer();
    330 
    331     int i = 0;
    332     while(i < str.length() && str.charAt(i) != ':') {
    333       leading.append(str.charAt(i));
    334       i++;
    335     }
    336     i++;
    337     while(i < str.length() && str.charAt(i) != ':') {
    338       host.append(str.charAt(i));
    339       i++;
    340     }
    341     i++;
    342     while(i < str.length() && Character.isDigit(str.charAt(i)) ) {
    343       port.append(str.charAt(i));
    344       i++;
    345     }
    346     while(i < str.length() ) {
    347       trailing.append(str.charAt(i));
    348       i++;
    349     }
    350     System.err.println("P2P parsed \"" + str  + "\" into host = \"" +
    351                host + "\" and port = \"" + port + "\"");
    352     return host.toString();
    353 
    354   }
    355 }
    356 
    357 class HTTP {
    358   // TODO
    359   String clean (String str) { return str; } ;
    360 
    361   boolean valid (String str) {
    362     String match = "HTTP:";
    363     String first = str.substring(0,match.length());
    364     if (first.compareToIgnoreCase(match) == 0) {
    365       return true;
    366     }
    367     return false;
    368   }
    369 
    370 }
    371 
    372 class Node {
     50public class Node {
    37351
    37452  static Properties knownList = new Properties();
     
    40482   
    40583    // start the poller ...
    406     new Poller().run();
     84    Poller poller = new Poller();
     85    poller.run();
    40786 
    40887  }
Note: See TracChangeset for help on using the changeset viewer.