KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jgroups.tests;
2
3 import java.io.IOException JavaDoc;
4 import java.io.OutputStream JavaDoc;
5 import java.net.InetAddress JavaDoc;
6 import java.net.ServerSocket JavaDoc;
7 import java.net.Socket JavaDoc;
8
9 /**
10  * @author Bela Ban
11  * @version $Id: LargeStateServer.java,v 1.1 2006/05/26 06:21:13 belaban Exp $
12  */

13 public class LargeStateServer {
14     ServerSocket JavaDoc srv_sock;
15     InetAddress JavaDoc bind_addr;
16
17
18     private void start(String JavaDoc bind_addr, int chunk_size) throws Exception JavaDoc {
19         this.bind_addr=InetAddress.getByName(bind_addr);
20         srv_sock=new ServerSocket JavaDoc(7500, 50, this.bind_addr);
21         while(true) {
22             System.out.println("-- waiting for clients to connect");
23             Socket JavaDoc sock=srv_sock.accept();
24             sock.setSendBufferSize(chunk_size);
25             sendLargeState(sock);
26             sock.close();
27         }
28     }
29
30     private void sendLargeState(Socket JavaDoc sock) throws IOException JavaDoc {
31         long total=0, start, stop;
32         OutputStream JavaDoc out=sock.getOutputStream();
33         start=System.currentTimeMillis();
34         for(int i=0; i < 1000000; i++) {
35             byte[] buf=new byte[1000];
36             out.write(buf, 0, buf.length);
37             total+=1000;
38             if(total % 100000000 == 0)
39                 System.out.println("-- sent " + (total / 1000000) + " MB");
40         }
41         stop=System.currentTimeMillis();
42         out.close();
43         System.out.println("- done, wrote " + (total / 1000000) + " MB in " + (stop-start) + "ms (" +
44                 (total / 1000000) / ((stop-start) / 1000.0) + " MB/sec");
45     }
46
47     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
48         String JavaDoc bind_addr=null;
49         int chunk=10000;
50
51         for(int i=0; i < args.length; i++) {
52             if(args[i].equals("-bind_addr")) {
53                 bind_addr=args[++i];
54                 continue;
55             }
56             if(args[i].equals("-chunk")) {
57                 chunk=Integer.parseInt(args[++i]);
58                 continue;
59             }
60             System.out.println("LargeStateServer [-bind_addr <addr>] [-chunk <size in bytes>]");
61             return;
62         }
63         new LargeStateServer().start(bind_addr, chunk);
64     }
65
66
67 }
68
Popular Tags