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 ; 7 import java.net.DatagramSocket ; 8 import java.net.InetAddress ; 9 import java.util.List ; 10 11 18 public class SenderThread extends Thread { 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 nodes; 25 DatagramSocket send_sock; 26 27 28 29 public SenderThread(DatagramSocket 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 p; 41 InetAddress 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 (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 e) { 72 e.printStackTrace(); 73 } 74 finally { 75 } 76 } 77 78 79 80 String dumpStats(long sent_msgs) { 81 StringBuffer sb=new StringBuffer (); 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 |