Debian Codegraph @

src/main/java/net/sf/antcontrib/antserver/server/Server.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.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 * Place class description here.
28 *
29 * @author <a href='mailto:[email protected]'>Matthew Inger</a>
30 * @author <additional author>
31 *
32 * @since
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 ; // gulp, no socket connection
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 ; // gulp
118 }
119 }
120 }
121 running = false;
122
123
124 }
125
126 }