001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package xmladder;
016:
017: import java.io.*;
018: import java.net.*;
019: import java.util.*;
020:
021: public class XmlAdderClient {
022: private static boolean debug = false;
023: private static boolean brokenReq = false;
024:
025: static {
026: debug = Boolean.getBoolean("xmladder.XmlAdderClient.bebug");
027: brokenReq = Boolean
028: .getBoolean("xmladder.XmlAdderClient.brokenReq");
029: }
030:
031: private String host = "127.0.0.1";
032: private int port = 2222;
033: private long time = -1;
034:
035: public XmlAdderClient() {
036: }
037:
038: public XmlAdderClient(String host, int port) {
039: this .host = host;
040: this .port = port;
041: }
042:
043: public long getTime() {
044: return time;
045: }
046:
047: public void test() {
048: BufferedOutputStream bos = null;
049: BufferedInputStream bis = null;
050: Socket socket = null;
051:
052: if (debug)
053: System.out.println("Connecting.. ");
054: long stime = System.currentTimeMillis();
055: try {
056: socket = new Socket(host, port);
057: bis = new BufferedInputStream(socket.getInputStream());
058: bos = new BufferedOutputStream(socket.getOutputStream());
059:
060: if (debug) {
061: System.out.println("========== Got ==========");
062: System.out.println(readInputStream(bis) + "\n");
063: } else {
064: readInputStream(bis);
065: }
066:
067: int a = 14;
068: int b = 9;
069: AddNumberReq addNumberReq = new AddNumberReq();
070: addNumberReq.setNumberA(a);
071: addNumberReq.setNumberB(b);
072: String msg = addNumberReq.toXML();
073:
074: if (debug) {
075: System.out.println("======== Sending ========\n");
076: System.out.println(msg + "\n");
077: }
078:
079: bos.write(msg.getBytes(), 0, msg.length());
080: bos.flush();
081:
082: String msg11 = "<add-number-req><number-a>1</number-a>";
083: String msg12 = "<number-b>12</number-b></add-number-req>";
084:
085: if (brokenReq) {
086: if (debug) {
087: System.out.println("======== Sending ========\n");
088: System.out.println(msg11 + "\n");
089: }
090:
091: bos.write(msg11.getBytes(), 0, msg11.length());
092: bos.flush();
093: }
094:
095: if (debug) {
096: System.out.println("========== Got ==========\n");
097: System.out.println(readInputStream(bis) + "\n");
098: } else {
099: readInputStream(bis);
100: }
101:
102: if (brokenReq) {
103: if (debug) {
104: System.out.println("======== Sending ========\n");
105: System.out.println(msg12 + "\n");
106: }
107:
108: bos.write(msg12.getBytes(), 0, msg12.length());
109: bos.flush();
110:
111: if (debug) {
112: System.out.println("========== Got ==========\n");
113: System.out.println(readInputStream(bis) + "\n");
114: } else {
115: readInputStream(bis);
116: }
117: }
118:
119: msg = "<quit />";
120:
121: if (debug) {
122: System.out.println("======== Sending ========\n");
123: System.out.println(msg + "\n");
124: }
125:
126: bos.write(msg.getBytes(), 0, msg.length());
127: bos.flush();
128:
129: if (debug) {
130: System.out.println("========== Got ==========\n");
131: System.out.println(readInputStream(bis) + "\n");
132: } else {
133: readInputStream(bis);
134: }
135:
136: if (debug)
137: System.out.println("Closing socket.");
138:
139: bos.close();
140: bis.close();
141: } catch (Exception e) {
142: System.err.println("Error (" + host + ":" + port + ")" + e);
143: } finally {
144: try {
145: if (socket != null)
146: socket.close();
147: } catch (Exception er) {
148: System.err.println("Error closing socket: " + er);
149: }
150: }
151: long etime = System.currentTimeMillis();
152: time = etime - stime;
153: }
154:
155: private static String readInputStream(BufferedInputStream bin)
156: throws IOException {
157: if (bin == null) {
158: return null;
159: }
160: byte data[] = null;
161: int s = bin.read();
162: if (s == -1) {
163: return null; //Connection lost
164: }
165: int alength = bin.available();
166: if (alength > 0) {
167: data = new byte[alength + 1];
168:
169: bin.read(data, 1, alength);
170: } else {
171: data = new byte[1];
172: }
173: data[0] = (byte) s;
174: return new String(data);
175: }
176:
177: public static void main(String args[]) {
178: XmlAdderClient client = null;
179:
180: if (args.length != 0 && args.length < 2) {
181: System.err.println("Usage : "
182: + "\n XmlAdderClient <ip_address> <port>");
183: System.exit(0);
184: }
185:
186: if (args.length == 2) {
187: try {
188: int p = Integer.parseInt(args[1]);
189: client = new XmlAdderClient(args[0], p);
190: } catch (Exception e) {
191: System.err.println("Error " + e);
192: return;
193: }
194: } else {
195: client = new XmlAdderClient();
196: }
197: client.test();
198: System.out.println("Time Taken: " + client.getTime() + "ms");
199: }
200: }
|