| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 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 |
|
| 88 |
|
| 89 |
ois.readObject(); |
| 90 |
} |
| 91 |
catch (ClassNotFoundException e) |
| 92 |
{ |
| 93 |
; |
| 94 |
} |
| 95 |
} |
| 96 |
finally |
| 97 |
{ |
| 98 |
|
| 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 |
; |
| 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 |
; |
| 165 |
|
| 166 |
} |
| 167 |
is = null; |
| 168 |
ois = null; |
| 169 |
|
| 170 |
try |
| 171 |
{ |
| 172 |
socket.close(); |
| 173 |
} |
| 174 |
catch (IOException e) |
| 175 |
{ |
| 176 |
; |
| 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 |
|
| 195 |
ois.readObject(); |
| 196 |
} |
| 197 |
catch (ClassNotFoundException e) |
| 198 |
{ |
| 199 |
; |
| 200 |
} |
| 201 |
|
| 202 |
shutdown(); |
| 203 |
} |
| 204 |
catch (SocketException e) { |
| 205 |
; |
| 206 |
} |
| 207 |
catch (EOFException e) { |
| 208 |
; |
| 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 |
|
| 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 |
; |
| 246 |
} |
| 247 |
|
| 248 |
return response; |
| 249 |
} |
| 250 |
|
| 251 |
} |