start of new file
[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
10     public static void main(String argv[]) {
11
12         String host=null;
13         int numberofclients=0;
14         int numberofmessages=0;
15         int groups=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             groups=Integer.parseInt(argv[4]);
25         }
26         catch (Exception e) {
27             System.out.println("NetsClient host port numberofclients numberofmessages debugflag");
28         }
29         try {
30             NetsClient.debug=(Integer.parseInt(argv[5])==1);
31         } catch (Exception e) {}
32
33         NetsClient[][] tarray=new NetsClient[groups][numberofclients];
34         for (int g=0;g<groups;g++) {
35             for (int i = 0; i < numberofclients; i++) {
36                 String room="group"+g;
37                 tarray[g][i] = new NetsClient(i, host, port,
38                                            numberofmessages, numberofclients, room);
39                 if (debug)
40                     System.out.println("Attempting to start "+i);
41                 tarray[g][i].connectt();
42             }
43             
44         try {
45             Thread.sleep(1000);
46         } catch (Exception e) {};
47         for (int i = 0; i < numberofclients; i++)
48             tarray[g][i].start();
49         try {
50             for (int i = 0; i < numberofclients; i++) {
51                 tarray[g][i].join();
52             }
53         } catch (InterruptedException e) {
54             e.printStackTrace();
55             System.out.println(e);
56         }
57         }
58
59         int messages=0;
60         for (int g=0;g<groups;g++)
61             for(int i=0;i<numberofclients;i++)
62                 messages+=tarray[g][i].lines;
63
64         System.out.println("ChatClient");
65         System.out.println("numclients:" + numberofclients);
66         System.out.println("groups:"+groups);
67         System.out.println("port:" + port);
68         System.out.println("number of messages:" + numberofmessages);
69
70         System.out.println("Lines="+messages+" out of "+groups*numberofclients*(numberofclients-1)*numberofmessages);
71     }
72
73     public NetsClient(int clientnumber, String host,
74                       int port, int nom, int noc, String room) {
75         this.port=port;
76         this.clientnumber=clientnumber;
77         this.host=host;
78         this.nom=nom;
79         this.noc=noc;
80         this.room=room;
81     }
82
83     String room;
84     int nom, noc,clientnumber,port;
85     String host;
86     Socket sock;
87     PrintStream pout;
88     InputStream in;
89     OutputStream out;
90     //DataInputStream din;
91     BufferedReader d;
92     int lines=0;
93
94     public void connectt() {
95         try{
96             sock = new Socket(host, port); // unix server
97             if (debug)
98                 System.out.println("connection made");
99             in = sock.getInputStream();
100             out = sock.getOutputStream();
101             pout = new PrintStream(out);
102             //din = new DataInputStream(in);
103             pout.println(room);
104             pout.flush();
105         }
106         catch (UnknownHostException e ) {
107             System.out.println("can't find host");
108         }
109         catch (IOException e) {
110             System.out.println("Error connecting to host");
111         }
112     }
113
114     public void run() {
115         if (debug)
116             System.out.println("client thread started");
117         int ns=0;
118
119         try {
120             for(int nr=0;nr<noc*nom;nr++) {
121                 if ((nr%noc)==clientnumber) {
122                     ns++;
123                     pout.println(room+"|"+clientnumber+"|hello#"+ns);
124                 }
125                 while(in.available()>0) {
126                     int nchar=in.read();
127                     if (nchar==10)
128                         lines++;
129                 }
130             }
131             pout.flush();
132             long time=System.currentTimeMillis();
133             while((System.currentTimeMillis()-time)<8*1000) {
134                 if(in.available()>0) {
135                     int nchar=in.read();
136                     time=System.currentTimeMillis();
137                     if (nchar==10)
138                         lines++;
139             } else try {Thread.sleep(2);} catch (Exception e) {}
140             }
141         }
142
143         catch (UnknownHostException e ) {System.out.println("can't find host"); }
144         catch ( IOException e ) {System.out.println("Error connecting to host");}
145
146     }
147
148 } // end of client class