1
2
3
4
5
6 package org.neo.swarm.services.tcp;
7
8 import java.net.InetAddress;
9 import java.util.logging.Logger;
10
11 import org.neo.swarm.util.network.tcp.ListenCallback;
12 import org.neo.swarm.util.network.tcp.NIOTcpServer;
13 import org.neo.swarm.util.network.tcp.TcpServiceAPI;
14 import org.neo.swarm.util.threads.ThreadPool;
15
16 /***
17 * NBIO Tcp Server
18 *
19 * @author neil.avery
20 */
21 public class TcpServer implements TcpService {
22
23 private static Logger logger = Logger.getLogger("org.neo.swarm.services");
24 public static final String NAME = "tcpService";
25
26 int bufferSize = 4 * 1024;
27 int timeout = 500;
28 ThreadPool threadPool;
29 InetAddress host;
30 int port = 8888;
31 TcpServiceAPI server = null;
32 String name = "tcpServer";
33 ListenCallback scr = null;
34
35
36 public TcpServer(InetAddress host, int port, ListenCallback scr, ThreadPool threadPool) {
37 this.host = host;
38 this.port = port;
39 this.scr = scr;
40 this.threadPool = threadPool;
41 }
42
43
44
45
46
47
48 public void start() {
49 try {
50 logger.info("starting:"+this);
51
52 server =
53 new NIOTcpServer(
54 scr,
55 threadPool,
56 bufferSize,
57 host,
58 port,
59 timeout);
60 server.startServer();
61 } catch (Exception e) {
62 e.printStackTrace();
63 logger.warning("failed to start TcpServiceAPI:" + e);
64 }
65 }
66
67
68
69
70
71
72 public void stop() {
73 try {
74 logger.info("stopping service");
75 server.stopServer();
76 } catch (Exception e) {
77 e.printStackTrace();
78 logger.warning("error when stopping service:" + e);
79 }
80 }
81
82
83 /***
84 * @param timeout
85 * The timeout to set.
86 */
87 public void setTimeout(int timeout) {
88 this.timeout = timeout;
89 }
90
91
92
93 public String getName() {
94 return this.name;
95 }
96
97
98
99
100 public void setName(String name) {
101 this.name = name;
102 }
103 }