df038e8f2d4d5ccff13ce6cc66a39a4b334b367e
[IRC.git] / Robust / src / Benchmarks / Chat / NetsClient.java
1 import java.awt.*;
2 import java.io.*;
3 import java.net.*;
4 import java.util.*;
5
6 public class NetsClient extends Thread {
7
8     static boolean debug;
9     static int sendoption=0;
10
11     public static void main(String argv[]) {
12
13         String host=null;
14         int numberofclients=0;
15         int numberofmessages=0;
16         int port=4321;
17
18         NetsClient.debug=false;
19         try {
20             host=argv[0];
21             port=Integer.parseInt(argv[1]);
22             numberofclients=Integer.parseInt(argv[2]);
23             numberofmessages=Integer.parseInt(argv[3]);
24         }
25         catch (Exception e) {
26             System.out.println("NetsClient host port numberofclients numberofmessages debugflag");
27         }
28         try {
29             NetsClient.debug=(Integer.parseInt(argv[4])==1);
30             NetsClient.sendoption=Integer.parseInt(argv[5]);
31         } catch (Exception e) {}
32
33         NetsClient[] tarray=new NetsClient[numberofclients];
34         for (int i = 0; i < numberofclients; i++) {
35             String room="room";
36             tarray[i] = new NetsClient(i, host, port,
37                                        numberofmessages, numberofclients, room);
38             if (debug)
39                 System.out.println("Attempting to start "+i);
40             tarray[i].connectt();
41         }
42
43         long starttime=System.currentTimeMillis();
44         for (int i = 0; i < numberofclients; i++)
45             tarray[i].start();
46         try {
47             for (int i = 0; i < numberofclients; i++) {
48                 tarray[i].join();
49             }
50         } catch (InterruptedException e) {
51             e.printStackTrace();
52             System.out.println(e);
53         }
54         long endtime=System.currentTimeMillis();
55
56         System.out.println("ChatClient");
57         System.out.println("numclients:" + numberofclients);
58         System.out.println("port:" + port);
59         System.out.println("number of messages:" + numberofmessages);
60         System.out.println("Elapsed time:(mS)" + (endtime - starttime));
61         System.out.println("Throughput:" + (double) numberofclients*
62                            ((sendoption==4) ? 1 : numberofclients) *
63                            numberofmessages/((double) (endtime-starttime)));
64     }
65
66     public NetsClient(int clientnumber, String host,
67                       int port, int nom, int noc, String room) {
68         this.port=port;
69         this.clientnumber=clientnumber;
70         this.host=host;
71         this.nom=nom;
72         this.noc=noc;
73         this.room=room;
74     }
75
76     String room;
77     int nom, noc,clientnumber,port;
78     String host;
79     Socket sock;
80     PrintStream pout;
81     InputStream in;
82     OutputStream out;
83     //DataInputStream din;
84     BufferedReader d;
85
86     public void connectt() {
87         try{
88             sock = new Socket(host, port); // unix server
89             if (debug)
90                 System.out.println("connection made");
91             in = sock.getInputStream();
92             out = sock.getOutputStream();
93             pout = new PrintStream(out);
94             //din = new DataInputStream(in);
95             d = new BufferedReader(new InputStreamReader(in));  
96             pout.println(room);
97         }
98         catch (UnknownHostException e ) {
99             System.out.println("can't find host");
100         }
101         catch (IOException e) {
102             System.out.println("Error connecting to host");
103         }
104     }
105
106     public void run() {
107         if (debug)
108             System.out.println("client thread started");
109         int ns=0;
110
111         try {
112             for(int nr=0;nr<noc*nom;) {
113                 if ((ns<nom)&&((nr%noc)==clientnumber)) {
114                     ns++;
115                     pout.println("0|"+clientnumber+"|hello#"+ns+"**");
116                 }
117                 String request = d.readLine();
118                 if (debug)
119                     System.out.println(request+nr);
120             }
121             pout.flush();
122         }
123
124         catch (UnknownHostException e ) {System.out.println("can't find host"); }
125         catch ( IOException e ) {System.out.println("Error connecting to host");}
126
127     }
128
129 } // end of client class