| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package net.sf.antcontrib.antserver.server; |
| 17 |
|
| 18 |
import java.io.IOException; |
| 19 |
import java.io.InterruptedIOException; |
| 20 |
import java.net.ServerSocket; |
| 21 |
import java.net.Socket; |
| 22 |
|
| 23 |
import org.apache.tools.ant.BuildException; |
| 24 |
import org.apache.tools.ant.Project; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class Server |
| 38 |
implements Runnable |
| 39 |
{ |
| 40 |
private ServerTask task; |
| 41 |
private int port = 17000; |
| 42 |
private boolean running = false; |
| 43 |
private Thread thread = null; |
| 44 |
|
| 45 |
public Server(ServerTask task, int port) |
| 46 |
{ |
| 47 |
super(); |
| 48 |
this.task = task; |
| 49 |
this.port = port; |
| 50 |
} |
| 51 |
|
| 52 |
public void start() |
| 53 |
throws InterruptedException |
| 54 |
{ |
| 55 |
thread = new Thread(this); |
| 56 |
thread.start(); |
| 57 |
thread.join(); |
| 58 |
} |
| 59 |
|
| 60 |
public void stop() |
| 61 |
{ |
| 62 |
running = false; |
| 63 |
} |
| 64 |
|
| 65 |
public void run() |
| 66 |
{ |
| 67 |
ServerSocket server = null; |
| 68 |
running = true; |
| 69 |
try |
| 70 |
{ |
| 71 |
task.getProject().log("Starting server on port: " + port, |
| 72 |
Project.MSG_DEBUG); |
| 73 |
try |
| 74 |
{ |
| 75 |
server = new ServerSocket(port); |
| 76 |
server.setSoTimeout(500); |
| 77 |
} |
| 78 |
catch (IOException e) |
| 79 |
{ |
| 80 |
throw new BuildException(e); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
while (running) |
| 85 |
{ |
| 86 |
try |
| 87 |
{ |
| 88 |
Socket clientSocket = server.accept(); |
| 89 |
task.getProject().log("Got a client connection. Starting Handler.", |
| 90 |
Project.MSG_DEBUG); |
| 91 |
ConnectionHandler handler = new ConnectionHandler(task, |
| 92 |
clientSocket); |
| 93 |
handler.start(); |
| 94 |
} |
| 95 |
catch (InterruptedIOException e) |
| 96 |
{ |
| 97 |
; |
| 98 |
} |
| 99 |
catch (IOException e) |
| 100 |
{ |
| 101 |
task.getProject().log(e.getMessage(), |
| 102 |
Project.MSG_ERR); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
finally |
| 107 |
{ |
| 108 |
if (server != null) |
| 109 |
{ |
| 110 |
try |
| 111 |
{ |
| 112 |
server.close(); |
| 113 |
server = null; |
| 114 |
} |
| 115 |
catch (IOException e) |
| 116 |
{ |
| 117 |
; |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
running = false; |
| 122 |
|
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
} |