Debian Codegraph @

src/main/java/net/sf/antcontrib/antserver/client/Client.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.antcontrib.antserver.client;
17
18 import java.io.*;
19 import java.net.Socket;
20 import java.net.SocketException;
21
22 import org.apache.tools.ant.Project;
23
24 import net.sf.antcontrib.antserver.Command;
25 import net.sf.antcontrib.antserver.Response;
26 import net.sf.antcontrib.antserver.Util;
27 import net.sf.antcontrib.antserver.commands.DisconnectCommand;
28
29
30 /****************************************************************************
31 * Place class description here.
32 *
33 * @author <a href='mailto:[email protected]'>Matthew Inger</a>
34 * @author <additional author>
35 *
36 * @since
37 *
38 ****************************************************************************/
39
40
41 public class Client
42 {
43 private String machine;
44 private int port;
45 private Project project;
46
47
48 public Client(Project project, String machine, int port)
49 {
50 super();
51 this.machine = machine;
52 this.port = port;
53 this.project = project;
54 }
55
56
57 private Socket socket;
58 private OutputStream os;
59 private InputStream is;
60 private ObjectOutputStream oos;
61 private ObjectInputStream ois;
62 private boolean connected;
63
64
65 public void connect()
66 throws IOException
67 {
68 project.log("Opening connection to " + machine + ":" + port,
69 Project.MSG_DEBUG);
70
71 try
72 {
73 socket = new Socket(machine, port);
74 socket.setKeepAlive(true);
75 project.log("Got connection to " + machine + ":" + port,
76 Project.MSG_DEBUG);
77
78 os = socket.getOutputStream();
79 is = socket.getInputStream();
80
81 oos = new ObjectOutputStream(os);
82 ois = new ObjectInputStream(is);
83
84 connected = true;
85 try
86 {
87 // Read the initial response object so that the
88 // object stream is initialized
89 ois.readObject();
90 }
91 catch (ClassNotFoundException e)
92 {
93 ; // gulp
94 }
95 }
96 finally
97 {
98 // If we were unable to connect, close everything
99 if (!connected)
100 {
101
102 try
103 {
104 if (os != null)
105 os.close();
106 os = null;
107 oos = null;
108 }
109 catch (IOException e)
110 {
111
112 }
113
114 try
115 {
116 if (is != null)
117 is.close();
118 is = null;
119 ois = null;
120 }
121 catch (IOException e)
122 {
123
124 }
125
126 try
127 {
128 if (socket != null)
129 socket.close();
130 socket = null;
131 }
132 catch (IOException e)
133 {
134
135 }
136 }
137 }
138
139
140 }
141
142 public void shutdown()
143 {
144 try
145 {
146 if (os != null)
147 os.close();
148 }
149 catch (IOException e)
150 {
151 ; // gulp
152
153 }
154 os = null;
155 oos = null;
156
157 try
158 {
159 if (is != null)
160 is.close();
161 }
162 catch (IOException e)
163 {
164 ; // gulp
165
166 }
167 is = null;
168 ois = null;
169
170 try
171 {
172 socket.close();
173 }
174 catch (IOException e)
175 {
176 ; // gulp
177 }
178 socket = null;
179
180 connected = false;
181 }
182
183
184 public void disconnect()
185 throws IOException
186 {
187 if (!connected)
188 return;
189
190 try {
191 oos.writeObject(DisconnectCommand.DISCONNECT_COMMAND);
192 try
193 {
194 // Read disconnect response
195 ois.readObject();
196 }
197 catch (ClassNotFoundException e)
198 {
199 ; // gulp
200 }
201
202 shutdown();
203 }
204 catch (SocketException e) {
205 ; // connection was closed
206 }
207 catch (EOFException e) {
208 ; // connection was closed
209 }
210 }
211
212
213 public Response sendCommand(Command command)
214 throws IOException
215 {
216 project.log("Sending command: " + command,
217 Project.MSG_DEBUG);
218 oos.writeObject(command);
219
220 if (command.getContentLength() > 0)
221 {
222 Util.transferBytes(command.getContentStream(),
223 command.getContentLength(),
224 os,
225 true);
226 }
227
228 Response response = null;
229
230 try
231 {
232 // Read the response object
233 response = (Response) ois.readObject();
234 project.log("Received Response: " + response,
235 Project.MSG_DEBUG);
236 if (response.getContentLength() != 0)
237 {
238 command.respond(project,
239 response.getContentLength(),
240 is);
241 }
242 }
243 catch (ClassNotFoundException e)
244 {
245 ; // gulp
246 }
247
248 return response;
249 }
250
251 }