source: trunk/java-client/org/nzdl/gsdl/ptp/Connection.java@ 2136

Last change on this file since 2136 was 2124, checked in by say1, 23 years ago

documentation updates

  • Property svn:keywords set to Author Date Id Revision
File size: 7.0 KB
Line 
1/*
2 * TODO.java
3 * Copyright (C) 2000 Stuart Yeates
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20// the package we're in
21package org.nzdl.gsdl.ptp;
22
23// java standard library classes used
24import java.io.IOException;
25import java.io.Reader;
26import java.io.InputStreamReader;
27import java.io.Writer;
28import java.io.OutputStreamWriter;
29import java.io.BufferedInputStream;
30import java.io.BufferedOutputStream;
31import java.io.Serializable;
32//import java.io.;
33import java.util.*;
34import java.net.*;
35
36/**
37 * Class Connection prepresents one end of a connection between two peers in a Peer-To-Peer Network.
38 *
39 * Handles all protocol work except parsing of Locators.
40 *
41 * @author stuart yeates ([email protected])
42 * @version $Revision: 2124 $
43 * @see org.nzdl.gsdl.ptp.Node
44 * @see org.nzdl.gsdl.ptp.Poller
45 * @see org.nzdl.gsdl.ptp.Listener
46 *
47 */
48
49public 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 /** Are we a server ? (false indicates we're a client) */
72 boolean server = false;
73
74 /** Has the connection been closed ? */
75 boolean connectionClosed = false;
76 /** the port we're connected to */
77 int port = 0;
78 /** the host we're connected to */
79 String remoteHostName = "";
80 /** the host we're connected to */
81 InetAddress remoteHost = null;
82
83 /** The underlying socket */
84 Socket socket = null;
85 /** The input from the socket */
86 Reader input = null;
87 /** The output to the socket */
88 Writer output = null;
89
90 Connection(String host, int port) {
91 new Thread("Client") ;
92 server = false;
93
94 this.port = port;
95 this.remoteHostName = host;
96 try {
97 remoteHost = InetAddress.getByName(remoteHostName);
98 socket = new Socket(remoteHost, port, InetAddress.getLocalHost(), 4567);
99 } catch (Exception exception) {
100 System.err.println("caught exception: "+exception);
101 }
102 try {
103 input = new InputStreamReader(new BufferedInputStream(socket.getInputStream()));
104 output = new OutputStreamWriter(new BufferedOutputStream(socket.getOutputStream()));
105 } catch (IOException exception) {
106 System.err.println("caught exception: "+exception);
107 }
108 }
109 Connection(Socket socket) {
110 new Thread("Server") ;
111 server = true;
112 setPriority(MIN_PRIORITY);
113
114 this.socket = socket;
115 port = socket.getPort();
116 remoteHost = socket.getInetAddress();
117 remoteHostName = remoteHost.getHostName();
118
119 try {
120 input = new InputStreamReader(new BufferedInputStream(socket.getInputStream()));
121 output = new OutputStreamWriter(new BufferedOutputStream(socket.getOutputStream()));
122 } catch (IOException exception) {
123 System.err.println("caught exception: "+exception);
124 }
125 }
126
127 String readline() throws IOException {
128 StringBuffer buff = new StringBuffer(1000);
129 while (true) {
130 int c = input.read();
131 if (c == '\n' || c == '\r')
132 return buff.toString();
133 buff.append((char)c);
134 }
135 }
136
137 public void run() {
138 try {
139 while (!server || !connectionClosed) {
140 String line = readline();
141 reply(line);
142 }
143 } catch (Exception exception) {
144 System.err.println("caught exception:" + exception);
145 connectionClosed = true;
146 }
147 }
148
149 int reply(String line) throws IOException {
150
151 String first = line.substring(0,WHOAREYOU.length());
152 if (first.compareToIgnoreCase(WHOAREYOU) == 0) {
153 System.err.println("We were asked who we are ...");
154 return whoWeAre();
155 }
156
157 first = line.substring(0,WHODOYOUKNOW.length());
158 if (first.compareToIgnoreCase(WHODOYOUKNOW) == 0) {
159 System.err.println("We were asked who we know ...");
160 return whoWeKnow();
161 }
162
163 first = line.substring(0,HELPREQUEST.length());
164 if (first.compareToIgnoreCase(HELPREQUEST) == 0) {
165 System.err.println("We were asked for help ...");
166 return help();
167 }
168
169 first = line.substring(0,WHATVERSIONAREYOU.length());
170 if (first.compareToIgnoreCase(WHATVERSIONAREYOU) == 0) {
171 System.err.println("We were asked what verrsion we are ...");
172 return whatVersionAreWe();
173 }
174
175 first = line.substring(0,OUT.length());
176 if (first.compareToIgnoreCase(OUT) == 0) {
177 System.err.println("We were asked to close the connection ...");
178 connectionClosed = true;
179 return gracefulClose();
180 }
181 System.err.println("Nothing matched \"" + line + "\" sending help ...");
182 return help();
183 }
184
185 int gracefulClose()
186 {
187 return -1;
188 }
189
190
191 int whoWeAre() throws IOException {
192 output.write("I AM IOR:1234567890");
193 output.write("I AM HTTP://www.someplace");
194 output.write("I AM P2P:www.someplace:4444");
195 return 3;
196 }
197
198 int whoWeKnow() throws IOException {
199 output.write("I KNOW IOR:12345678901");
200 output.write("I KNOW IOR:12345678902");
201 output.write("I KNOW HTTP://www.someplace.else");
202 output.write("I KNOW HTTP://www.someplace.elsewhere");
203 output.write("I KNOW P2P:www.someplace:4444:A");
204 output.write("I KNOW P2P:www.someplace:4444:B");
205 return 6;
206 }
207
208 int help() throws IOException {
209 output.write("HELP: This is NZDL P2P protocol $Revision: 2124 $");
210 output.write("HELP: There are five valid commands:");
211 output.write("HELP: WHO ARE YOU?");
212 output.write("HELP: WHO DO YOU KNOW?");
213 output.write("HELP: WHAT VERSION ARE YOU?");
214 output.write("HELP: HELP");
215 output.write("HELP: OUT");
216 return 7;
217 }
218
219 int whatVersionAreWe() throws IOException {
220 output.write("VERSION: Protocol: NZDL-P2P-0.1 ");
221 output.write("VERSION: Implementation: $Revision: 2124 $");
222 return 2;
223 }
224
225 /**
226 * Tests...
227 *
228 *
229 * @exception java.io.IOException when ...
230 * @param args the arguments ...
231 */
232
233 public static void main(String args[]) throws IOException
234 {
235
236 }
237}
238
Note: See TracBrowser for help on using the repository browser.