KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > adaptudp > SenderThread


1 package org.jgroups.tests.adaptudp;
2
3 import org.apache.log4j.Logger;
4 import org.jgroups.util.Util;
5
6 import java.net.DatagramPacket JavaDoc;
7 import java.net.DatagramSocket JavaDoc;
8 import java.net.InetAddress JavaDoc;
9 import java.util.List JavaDoc;
10
11 /** Sender thread: inputs into the system a num_busts bursts
12  * of msgs_burst messages composed of msg_size bytes.
13  * Sleeps for sleep_msec after each burst.
14  * @author Milcan Prica (prica@deei.units.it)
15  * @author Bela Ban (belaban@yahoo.com)
16  * @version $Id: SenderThread.java,v 1.8 2004/07/05 14:15:09 belaban Exp $
17  */

18 public class SenderThread extends Thread JavaDoc {
19     private int num_msgs;
20     private int msg_size;
21     Logger log=Logger.getLogger(this.getClass());
22     long log_interval=1000;
23     boolean gnuplot_output=Boolean.getBoolean("gnuplot_output");
24     List JavaDoc nodes;
25     DatagramSocket JavaDoc send_sock;
26
27
28
29     public SenderThread(DatagramSocket JavaDoc send_sock, int num_msgs, int ms, long log_interval) {
30         this.num_msgs=num_msgs;
31         msg_size=ms;
32         this.log_interval=log_interval;
33         this.send_sock=send_sock;
34     }
35
36     public void run() {
37         long total_msgs=0;
38         Request req;
39         byte[] buf;
40         DatagramPacket JavaDoc p;
41         InetAddress JavaDoc mcast_addr;
42
43         System.out.println("Sender thread started...");
44
45         try {
46             mcast_addr=InetAddress.getByName(Test.mcast_addr);
47
48             byte[] msg=new byte[msg_size];
49             for(int h=0; h < msg_size; h++) {
50                 msg[h]=(byte)h;
51             }
52
53             System.out.println("Everyone joined, ready to begin test...\n");
54
55             for(int i=0; i < num_msgs; i++) {
56                 req=new Request(Request.DATA, msg);
57                 buf=Util.objectToByteBuffer(req);
58                 p=new DatagramPacket JavaDoc(buf, buf.length, mcast_addr, Test.mcast_port);
59                 send_sock.send(p);
60                 total_msgs++;
61                 if(total_msgs % 1000 == 0) {
62                     System.out.println("++ sent " + total_msgs);
63                 }
64                 if(total_msgs % log_interval == 0) {
65                     if(gnuplot_output == false)
66                         if(log.isInfoEnabled()) log.info(dumpStats(total_msgs));
67                 }
68             }
69             System.out.println("Sent all bursts. Sender terminates.\n");
70         }
71         catch(Exception JavaDoc e) {
72             e.printStackTrace();
73         }
74         finally {
75         }
76     }
77
78
79
80     String JavaDoc dumpStats(long sent_msgs) {
81         StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
82         sb.append("\nmsgs_sent=").append(sent_msgs).append('\n');
83         sb.append("free_mem=").append(Runtime.getRuntime().freeMemory());
84         sb.append(" (total_mem=").append(Runtime.getRuntime().totalMemory()).append(")\n");
85         return sb.toString();
86     }
87
88 }
89
Popular Tags