KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > UcastTest


1 // $Id: UcastTest.java,v 1.3 2004/07/05 14:15:11 belaban Exp $
2

3 package org.jgroups.tests;
4
5 import java.io.DataInputStream JavaDoc;
6 import java.net.DatagramPacket JavaDoc;
7 import java.net.DatagramSocket JavaDoc;
8 import java.net.InetAddress JavaDoc;
9 import java.util.StringTokenizer JavaDoc;
10
11
12
13 /**
14    @author Bela Ban
15    @version $Revision: 1.3 $
16  */

17 public class UcastTest {
18
19     public static void main(String JavaDoc args[]) {
20     DatagramSocket JavaDoc sock;
21     DatagramPacket JavaDoc packet;
22     byte[] buf=new byte[0];
23     boolean receiver=true;
24     String JavaDoc val;
25     InetAddress JavaDoc receiver_addr=null, sender_addr=null;
26     int receiver_port=0, sender_port=0;
27
28     
29     
30     for(int i=0; i < args.length; i++) {
31         val=args[i];
32         if("-help".equals(val)) {
33         help();
34         return;
35         }
36         if("-sender".equals(val)) {
37         receiver=false;
38         continue;
39         }
40         if("-sender_addr".equals(val)) {
41         try {
42             sender_addr=InetAddress.getByName(args[++i]);
43             sender_port=Integer.parseInt(args[++i]);
44             continue;
45         }
46         catch(Exception JavaDoc e) {
47             System.err.println(e);
48             help();
49             return;
50         }
51         }
52         if("-receiver".equals(val)) {
53         receiver=true;
54         try {
55             receiver_addr=InetAddress.getByName(args[++i]);
56             receiver_port=Integer.parseInt(args[++i]);
57         }
58         catch(Exception JavaDoc e) {
59             System.err.println(e);
60             help();
61             return;
62         }
63         continue;
64         }
65         help();
66     }
67
68
69     try {
70         if(receiver) {
71         sock=new DatagramSocket JavaDoc(receiver_port, receiver_addr);
72         System.out.println("Socket=" + sock.getLocalAddress() + ':' + sock.getLocalPort());
73         System.out.println("starting as receiver");
74         receiverLoop(sock);
75         }
76         else {
77         if(sender_addr != null && sender_port > 0)
78             sock=new DatagramSocket JavaDoc(sender_port, sender_addr);
79         else
80             sock=new DatagramSocket JavaDoc();
81         System.out.println("Socket=" + sock.getLocalAddress() + ':' + sock.getLocalPort());
82         System.out.println("starting as sender");
83         senderLoop(sock);
84         }
85     }
86     catch(Exception JavaDoc e) {
87         System.err.println(e);
88     }
89
90     }
91
92
93     static void receiverLoop(DatagramSocket JavaDoc sock) {
94     DatagramPacket JavaDoc packet;
95     byte[] buf;
96
97     while(true) {
98         buf=new byte[256];
99         packet=new DatagramPacket JavaDoc(buf, buf.length);
100         try {
101         sock.receive(packet);
102         System.out.println("Receive packet from " + packet.getAddress().getHostAddress() +
103                            ':' + packet.getPort() + ": " + new String JavaDoc(packet.getData()));
104
105         buf="ACK".getBytes();
106         packet=new DatagramPacket JavaDoc(buf, buf.length, packet.getAddress(), packet.getPort());
107         sock.send(packet);
108         }
109         catch(Exception JavaDoc e) {
110         System.err.println(e);
111         break;
112         }
113     }
114     }
115
116
117     
118     static void senderLoop(DatagramSocket JavaDoc sock) {
119     DataInputStream JavaDoc in;
120     String JavaDoc line;
121     DatagramPacket JavaDoc packet;
122     InetAddress JavaDoc dst;
123     int dst_port;
124     StringTokenizer JavaDoc tok;
125     StringBuffer JavaDoc sb;
126     String JavaDoc tmp, buf;
127     byte[] bbuf;
128     AckReceiver ack_receiver=new AckReceiver(sock);
129
130     ack_receiver.start();
131     in=new DataInputStream JavaDoc(System.in);
132     while(true) {
133         try {
134         System.out.print("> ");
135         line=in.readLine();
136         if(line.startsWith("quit") || line.startsWith("exit"))
137             break;
138         tok=new StringTokenizer JavaDoc(line);
139         try {
140             dst=InetAddress.getByName(tok.nextToken());
141             dst_port=Integer.parseInt(tok.nextToken());
142         }
143         catch(Exception JavaDoc e) {
144             System.err.println(e);
145             continue;
146         }
147         sb=new StringBuffer JavaDoc();
148         while(tok.hasMoreTokens()) {
149             tmp=tok.nextToken();
150             sb.append(tmp + ' ');
151         }
152         buf=sb.toString();
153         System.out.println("sending " + buf);
154         bbuf=buf.getBytes();
155         packet=new DatagramPacket JavaDoc(bbuf, bbuf.length, dst, dst_port);
156         sock.send(packet);
157         }
158         catch(Exception JavaDoc e) {
159         System.err.println(e);
160         break;
161         }
162     }
163     ack_receiver.stop();
164     }
165
166
167     private static class AckReceiver implements Runnable JavaDoc {
168     DatagramSocket JavaDoc sock;
169     DatagramPacket JavaDoc packet;
170     byte[] buf;
171     Thread JavaDoc t=null;
172
173     AckReceiver(DatagramSocket JavaDoc sock) {
174         this.sock=sock;
175     }
176
177     public void run() {
178         while(t != null) {
179         try {
180             buf=new byte[256];
181             packet=new DatagramPacket JavaDoc(buf, buf.length);
182             sock.receive(packet);
183             System.out.println("<< Received packet from " +
184                        packet.getAddress().getHostAddress() + ':' +
185                        packet.getPort() + ": " + new String JavaDoc(packet.getData()));
186         }
187         catch(Exception JavaDoc e) {
188             System.err.println(e);
189             break;
190         }
191         }
192         t=null;
193     }
194
195     void start() {
196         t=new Thread JavaDoc(this, "UcastTest.AckReceiver thread");
197         t.start();
198     }
199
200     void stop() {
201         if(t != null && t.isAlive()) {
202         t=null;
203         try {
204             sock.close();
205         }
206         catch(Exception JavaDoc e) {
207         }
208         }
209     }
210     }
211
212
213     static void help() {
214     System.out.println("UcastTest [-help] [-sender | -receiver <local addr> <local port>] " +
215                "[-sender_addr <sender's bind address> <sender' port>]");
216     }
217
218     
219 }
220
Popular Tags