KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > test > poweroff > Listener


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.test.poweroff;
6
7 import java.io.DataInputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.net.ServerSocket JavaDoc;
10 import java.net.Socket JavaDoc;
11
12 public class Listener implements Runnable JavaDoc {
13
14     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
15         new Listener().test(args);
16     }
17
18     volatile int maxValue;
19
20     void test(String JavaDoc[] args) throws Exception JavaDoc {
21         int port = 9099;
22         for (int i = 0; i < args.length; i++) {
23             if (args[i].equals("-port")) {
24                 port = Integer.parseInt(args[++i]);
25             }
26         }
27         listen(port);
28     }
29
30     public void run() {
31         while(true) {
32             try {
33                 Thread.sleep(10000);
34             } catch(Exception JavaDoc e) {
35                 // ignore
36
}
37             System.out.println("Max=" + maxValue);
38         }
39     }
40
41     void listen(int port) throws Exception JavaDoc {
42         new Thread JavaDoc(this).start();
43         ServerSocket JavaDoc serverSocket = new ServerSocket JavaDoc(port);
44         System.out.println("Listening on " + serverSocket.toString());
45         long time;
46         maxValue = 0;
47         while (true) {
48             Socket JavaDoc socket = serverSocket.accept();
49             DataInputStream JavaDoc in = new DataInputStream JavaDoc(socket.getInputStream());
50             System.out.println("Connected");
51             time = System.currentTimeMillis();
52             try {
53                 while (true) {
54                     int value = in.readInt();
55                     if (value < 0) {
56                         break;
57                     }
58                     maxValue = Math.max(maxValue, value);
59                 }
60             } catch (IOException JavaDoc e) {
61                 System.out.println("Closed with Exception: " + e);
62             }
63             time = System.currentTimeMillis() - time;
64             int ops = (int) (1000 * maxValue / time);
65             System.out.println("Max=" + maxValue + " operations/sec=" + ops);
66         }
67     }
68
69 }
70
Popular Tags