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 org.quickserver.net.client;
016:
017: import java.io.*;
018: import java.net.*;
019: import java.util.logging.*;
020:
021: /**
022: * Blocking Client socket.
023: * @author Akshathkumar Shetty
024: * @since 1.4.7
025: */
026: public class BlockingClient implements ClientService {
027: private static Logger logger = Logger
028: .getLogger(BlockingClient.class.getName());
029: private static String charset = "ISO-8859-1";
030:
031: private String host = "localhost";
032: private int port = 0;
033:
034: private Socket socket;
035:
036: private OutputStream out;
037: private BufferedOutputStream b_out;
038: private ObjectOutputStream o_out;
039:
040: private InputStream in;
041: private BufferedInputStream b_in;
042: private BufferedReader br;
043: private ObjectInputStream o_in;
044:
045: public int getMode() {
046: return ClientService.BLOCKING;
047: }
048:
049: public void connect(String host, int port) throws IOException {
050: this .host = host;
051: this .port = port;
052:
053: logger.fine("Connecting to " + host + ":" + port);
054: socket = new Socket(host, port);
055:
056: in = socket.getInputStream();
057: out = socket.getOutputStream();
058: logger.fine("Connected");
059: }
060:
061: public boolean isConnected() {
062: if (socket == null)
063: return false;
064: return socket.isConnected();
065: }
066:
067: public void close() throws IOException {
068: logger.fine("Closing");
069: try {
070: if (out != null) {
071: logger.finest("Closing output streams");
072: try {
073: out.flush();
074: } catch (IOException ioe) {
075: logger.finest("Flushing output streams failed: "
076: + ioe);
077: }
078:
079: if (socket != null /*&& isSecure()==false*/) {
080: socket.shutdownOutput();
081: }
082: if (o_out != null) {
083: o_out.close();
084: }
085: if (b_out != null) {
086: b_out.close();
087: }
088: if (out != null)
089: out.close();
090: }
091:
092: if (in != null) {
093: logger.finest("Closing input streams");
094: /*
095: if(socket!=null) {
096: socket.shutdownInput();
097: }*/
098:
099: if (o_in != null) {
100: o_in.close();
101: }
102: if (b_in != null) {
103: b_in.close();
104: }
105: if (br != null) {
106: br.close();
107: }
108: if (in != null)
109: in.close();
110: }
111: } catch (IOException e) {
112: logger.warning("Error in closing streams: " + e);
113: }
114: socket.close();
115: socket = null;
116: }
117:
118: public void sendBinary(byte[] data) throws IOException {
119: logger.fine("Sending bytes: " + data.length);
120: checkBufferedOutputStream();
121: b_out.write(data);
122: b_out.flush();
123: }
124:
125: public void sendBytes(String data) throws IOException {
126: logger.fine("Sending: " + data);
127: checkBufferedOutputStream();
128: byte d[] = data.getBytes(charset);
129: b_out.write(d, 0, d.length);
130: b_out.flush();
131: }
132:
133: public void sendString(String data) throws IOException {
134: logger.fine("Sending: " + data);
135: checkBufferedOutputStream();
136: byte d[] = data.getBytes(charset);
137: b_out.write(d, 0, d.length);
138: d = "\r\n".getBytes(charset);
139: b_out.write(d, 0, d.length);
140: b_out.flush();
141: }
142:
143: public void sendObject(Object data) throws IOException {
144: checkObjectOutputStream();
145: o_out.writeObject(data);
146: o_out.flush();
147: }
148:
149: public byte[] readBinary() throws IOException {
150: checkBufferedInputStream();
151: return readInputStream(b_in);
152: }
153:
154: public String readBytes() throws IOException {
155: byte data[] = readBinary();
156: return new String(data, charset);
157: }
158:
159: public String readString() throws IOException {
160: checkBufferedReader();
161: return br.readLine();
162: }
163:
164: public Object readObject() throws IOException,
165: ClassNotFoundException {
166: checkObjectInputStream();
167: return o_in.readObject();
168: }
169:
170: public Socket getSocket() {
171: return socket;
172: }
173:
174: private void checkObjectOutputStream() throws IOException {
175: if (o_out == null) {
176: b_out = null;
177: o_out = new ObjectOutputStream(out);
178: o_out.flush();
179: }
180: }
181:
182: private void checkBufferedOutputStream() throws IOException {
183: if (b_out == null) {
184: o_out = null;
185: b_out = new BufferedOutputStream(out);
186: }
187: }
188:
189: private void checkBufferedInputStream() throws IOException {
190: if (b_in == null) {
191: br = null;
192: o_in = null;
193: b_in = new BufferedInputStream(in);
194: }
195: }
196:
197: private void checkBufferedReader() throws IOException {
198: if (br == null) {
199: b_in = null;
200: o_in = null;
201: br = new BufferedReader(new InputStreamReader(in));
202: }
203: }
204:
205: private void checkObjectInputStream() throws IOException {
206: if (o_in == null) {
207: b_in = null;
208: br = null;
209: o_in = new ObjectInputStream(in);
210: }
211: }
212:
213: protected static byte[] readInputStream(InputStream _in)
214: throws IOException {
215: byte data[] = null;
216: if (_in == null)
217: throw new IOException("InputStream can't be null!");
218:
219: int s = _in.read();
220: if (s == -1) {
221: return null; //Connection lost
222: }
223: int alength = _in.available();
224: if (alength > 0) {
225: data = new byte[alength + 1];
226: _in.read(data, 1, alength);
227: } else {
228: data = new byte[1];
229: }
230: data[0] = (byte) s;
231: return data;
232: }
233: }
|