KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xmladder > XmlAdderClient


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package xmladder;
16
17 import java.io.*;
18 import java.net.*;
19 import java.util.*;
20
21 public class XmlAdderClient {
22     private static boolean debug = false;
23     private static boolean brokenReq = false;
24
25     static {
26         debug = Boolean.getBoolean("xmladder.XmlAdderClient.bebug");
27         brokenReq = Boolean.getBoolean("xmladder.XmlAdderClient.brokenReq");
28     }
29
30     private String JavaDoc host = "127.0.0.1";
31     private int port = 2222;
32     private long time = -1;
33
34
35     public XmlAdderClient() {}
36
37     public XmlAdderClient(String JavaDoc host, int port) {
38         this.host = host;
39         this.port = port;
40     }
41
42     public long getTime() {
43         return time;
44     }
45
46     public void test() {
47         BufferedOutputStream bos = null;
48         BufferedInputStream bis = null;
49         Socket socket = null;
50
51         if(debug) System.out.println("Connecting.. ");
52         long stime = System.currentTimeMillis();
53         try {
54             socket = new Socket(host, port);
55             bis = new BufferedInputStream(socket.getInputStream());
56             bos = new BufferedOutputStream(socket.getOutputStream());
57
58             if(debug) {
59                 System.out.println("========== Got ==========");
60                 System.out.println(readInputStream(bis)+"\n");
61             } else {
62                 readInputStream(bis);
63             }
64
65             int a = 14;
66             int b = 9;
67             AddNumberReq addNumberReq = new AddNumberReq();
68             addNumberReq.setNumberA(a);
69             addNumberReq.setNumberB(b);
70             String JavaDoc msg = addNumberReq.toXML();
71
72             if(debug) {
73                 System.out.println("======== Sending ========\n");
74                 System.out.println(msg+"\n");
75             }
76
77             bos.write(msg.getBytes(),0,msg.length());
78             bos.flush();
79
80
81             String JavaDoc msg11 = "<add-number-req><number-a>1</number-a>";
82             String JavaDoc msg12 = "<number-b>12</number-b></add-number-req>";
83
84             if(brokenReq) {
85                 if(debug) {
86                     System.out.println("======== Sending ========\n");
87                     System.out.println(msg11+"\n");
88                 }
89
90                 bos.write(msg11.getBytes(),0,msg11.length());
91                 bos.flush();
92             }
93
94             if(debug) {
95                 System.out.println("========== Got ==========\n");
96                 System.out.println(readInputStream(bis)+"\n");
97             } else {
98                 readInputStream(bis);
99             }
100
101             if(brokenReq) {
102                 if(debug) {
103                     System.out.println("======== Sending ========\n");
104                     System.out.println(msg12+"\n");
105                 }
106
107                 bos.write(msg12.getBytes(),0,msg12.length());
108                 bos.flush();
109
110                 if(debug) {
111                     System.out.println("========== Got ==========\n");
112                     System.out.println(readInputStream(bis)+"\n");
113                 } else {
114                     readInputStream(bis);
115                 }
116             }
117
118             msg = "<quit />";
119
120             if(debug) {
121                 System.out.println("======== Sending ========\n");
122                 System.out.println(msg+"\n");
123             }
124
125             bos.write(msg.getBytes(),0,msg.length());
126             bos.flush();
127
128             if(debug) {
129                 System.out.println("========== Got ==========\n");
130                 System.out.println(readInputStream(bis)+"\n");
131             } else {
132                 readInputStream(bis);
133             }
134
135             if(debug) System.out.println("Closing socket.");
136
137             bos.close();
138             bis.close();
139         } catch(Exception JavaDoc e) {
140             System.err.println("Error ("+host+":"+port+")" + e);
141         } finally {
142             try {
143                 if(socket!=null)
144                     socket.close();
145             } catch(Exception JavaDoc er) {
146                     System.err.println("Error closing socket: " + er);
147             }
148         }
149         long etime = System.currentTimeMillis();
150         time = etime - stime;
151     }
152
153     private static String JavaDoc readInputStream(BufferedInputStream bin)
154             throws IOException {
155         if(bin==null) {
156             return null;
157         }
158         byte data[] = null;
159         int s = bin.read();
160         if(s==-1) {
161             return null; //Connection lost
162
}
163         int alength = bin.available();
164         if(alength > 0) {
165             data = new byte[alength+1];
166             
167             bin.read(data, 1, alength);
168         } else {
169             data = new byte[1];
170         }
171         data[0] = (byte)s;
172         return new String JavaDoc(data);
173     }
174
175     public static void main(String JavaDoc args[]) {
176         XmlAdderClient client = null;
177
178         if(args.length!=0 && args.length<2) {
179             System.err.println("Usage : "+
180             "\n XmlAdderClient <ip_address> <port>");
181             System.exit(0);
182         }
183
184         if(args.length==2) {
185             try {
186                 int p = Integer.parseInt(args[1]);
187                 client = new XmlAdderClient(args[0], p);
188             } catch(Exception JavaDoc e) {
189                 System.err.println("Error " + e);
190                 return;
191             }
192         } else {
193             client = new XmlAdderClient();
194         }
195         client.test();
196         System.out.println("Time Taken: "+client.getTime()+"ms");
197     }
198 }
199
Popular Tags