001: // $Id: UcastTest.java,v 1.5 2005/05/30 16:15:12 belaban Exp $
002:
003: package org.jgroups.tests;
004:
005: import java.io.DataInputStream;
006: import java.net.DatagramPacket;
007: import java.net.DatagramSocket;
008: import java.net.InetAddress;
009: import java.util.StringTokenizer;
010:
011: /**
012: @author Bela Ban
013: @version $Revision: 1.5 $
014: */
015: public class UcastTest {
016:
017: public static void main(String args[]) {
018: DatagramSocket sock;
019: DatagramPacket packet;
020: byte[] buf = new byte[0];
021: boolean receiver = true;
022: String val;
023: InetAddress receiver_addr = null, sender_addr = null;
024: int receiver_port = 0, sender_port = 0;
025:
026: for (int i = 0; i < args.length; i++) {
027: val = args[i];
028: if ("-help".equals(val)) {
029: help();
030: return;
031: }
032: if ("-sender".equals(val)) {
033: receiver = false;
034: continue;
035: }
036: if ("-sender_addr".equals(val)) {
037: try {
038: sender_addr = InetAddress.getByName(args[++i]);
039: sender_port = Integer.parseInt(args[++i]);
040: continue;
041: } catch (Exception e) {
042: System.err.println(e);
043: help();
044: return;
045: }
046: }
047: if ("-receiver".equals(val)) {
048: receiver = true;
049: try {
050: receiver_addr = InetAddress.getByName(args[++i]);
051: receiver_port = Integer.parseInt(args[++i]);
052: } catch (Exception e) {
053: System.err.println(e);
054: help();
055: return;
056: }
057: continue;
058: }
059: help();
060: }
061:
062: try {
063: if (receiver) {
064: sock = new DatagramSocket(receiver_port, receiver_addr);
065: System.out.println("Socket=" + sock.getLocalAddress()
066: + ':' + sock.getLocalPort());
067: System.out.println("starting as receiver");
068: receiverLoop(sock);
069: } else {
070: if (sender_addr != null && sender_port > 0)
071: sock = new DatagramSocket(sender_port, sender_addr);
072: else
073: sock = new DatagramSocket();
074: System.out.println("Socket=" + sock.getLocalAddress()
075: + ':' + sock.getLocalPort());
076: System.out.println("starting as sender");
077: senderLoop(sock);
078: }
079: } catch (Exception e) {
080: System.err.println(e);
081: }
082:
083: }
084:
085: static void receiverLoop(DatagramSocket sock) {
086: DatagramPacket packet;
087: byte[] buf;
088:
089: while (true) {
090: buf = new byte[256];
091: packet = new DatagramPacket(buf, buf.length);
092: try {
093: sock.receive(packet);
094: System.out.println("Receive packet from "
095: + packet.getAddress().getHostAddress() + ':'
096: + packet.getPort() + ": "
097: + new String(packet.getData()));
098:
099: buf = "ACK".getBytes();
100: packet = new DatagramPacket(buf, buf.length, packet
101: .getAddress(), packet.getPort());
102: sock.send(packet);
103: } catch (Exception e) {
104: System.err.println(e);
105: break;
106: }
107: }
108: }
109:
110: static void senderLoop(DatagramSocket sock) {
111: DataInputStream in;
112: String line;
113: DatagramPacket packet;
114: InetAddress dst;
115: int dst_port;
116: StringTokenizer tok;
117: StringBuffer sb;
118: String tmp, buf;
119: byte[] bbuf;
120: AckReceiver ack_receiver = new AckReceiver(sock);
121:
122: ack_receiver.start();
123: in = new DataInputStream(System.in);
124: while (true) {
125: try {
126: System.out.print("> ");
127: line = in.readLine();
128: if (line.startsWith("quit") || line.startsWith("exit"))
129: break;
130: tok = new StringTokenizer(line);
131: try {
132: dst = InetAddress.getByName(tok.nextToken());
133: dst_port = Integer.parseInt(tok.nextToken());
134: } catch (Exception e) {
135: System.err.println(e);
136: continue;
137: }
138: sb = new StringBuffer();
139: while (tok.hasMoreTokens()) {
140: tmp = tok.nextToken();
141: sb.append(tmp + ' ');
142: }
143: buf = sb.toString();
144: System.out.println("sending " + buf);
145: bbuf = buf.getBytes();
146: packet = new DatagramPacket(bbuf, bbuf.length, dst,
147: dst_port);
148: sock.send(packet);
149: } catch (Exception e) {
150: System.err.println(e);
151: break;
152: }
153: }
154: ack_receiver.stop();
155: }
156:
157: private static class AckReceiver implements Runnable {
158: DatagramSocket sock;
159: DatagramPacket packet;
160: byte[] buf;
161: Thread t = null;
162:
163: AckReceiver(DatagramSocket sock) {
164: this .sock = sock;
165: }
166:
167: public void run() {
168: while (t != null) {
169: try {
170: buf = new byte[256];
171: packet = new DatagramPacket(buf, buf.length);
172: sock.receive(packet);
173: System.out.println("<< Received packet from "
174: + packet.getAddress().getHostAddress()
175: + ':' + packet.getPort() + ": "
176: + new String(packet.getData()));
177: } catch (Exception e) {
178: System.err.println(e);
179: break;
180: }
181: }
182: t = null;
183: }
184:
185: void start() {
186: t = new Thread(this , "UcastTest.AckReceiver thread");
187: t.start();
188: }
189:
190: void stop() {
191: if (t != null && t.isAlive()) {
192: t = null;
193: try {
194: sock.close();
195: } catch (Exception e) {
196: }
197: }
198: }
199: }
200:
201: static void help() {
202: System.out
203: .println("UcastTest [-help] [-sender | -receiver <local addr> <local port>] "
204: + "[-sender_addr <sender's bind address> <sender' port>]");
205: }
206:
207: }
|