1 package net.sf.mbus4j;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 import java.io.IOException;
31 import java.io.ObjectInputStream;
32 import java.io.ObjectOutputStream;
33 import java.net.Socket;
34 import java.util.Objects;
35 import net.sf.json.JSONObject;
36 import net.sf.mbus4j.json.JsonSerializeType;
37
38
39
40
41
42 public class TcpIpConnection extends Connection {
43
44 public static final int DEFAULT_RESPONSE_TIMEOUT_OFFSET = 600;
45 static final String TCP_IP_CONNECTION = "tcpIpConnection";
46 private transient Socket socket;
47 private String host;
48 private int port;
49
50 public TcpIpConnection() {
51 super(Connection.DEFAULT_BAUDRATE, DEFAULT_RESPONSE_TIMEOUT_OFFSET);
52 }
53
54 public TcpIpConnection(String host, int port) {
55 super(Connection.DEFAULT_BAUDRATE, DEFAULT_RESPONSE_TIMEOUT_OFFSET);
56 this.host = host;
57 this.port = port;
58 }
59
60 public TcpIpConnection(String host, int port, int bitPerSecond, int responseTimeoutOffset) {
61 super(bitPerSecond, responseTimeoutOffset);
62 this.host = host;
63 this.port = port;
64 }
65
66 @Override
67 public void open() throws IOException {
68 setConnState(State.OPENING);
69 socket = new Socket(host, port);
70
71 is = socket.getInputStream();
72 os = socket.getOutputStream();
73 setConnState(State.OPEN);
74 }
75
76 @Override
77 public void close() throws IOException {
78 setConnState(State.CLOSING);
79 socket.close();
80 setConnState(State.CLOSED);
81 }
82
83 @Override
84 public JSONObject toJSON(JsonSerializeType jsonSerializeType) {
85 JSONObject result = super.toJSON(jsonSerializeType);
86 result.accumulate("host", host);
87 result.accumulate("port", port);
88 return result;
89 }
90
91 @Override
92 public void fromJSON(JSONObject json) {
93 super.fromJSON(json);
94 host = json.getString("host");
95 port = json.getInt("port");
96 }
97
98 public String getHost() {
99 return host;
100 }
101
102 public int getPort() {
103 return port;
104 }
105
106 public void setHost(String host) {
107 this.host = host;
108 }
109
110 public void setPort(int port) {
111 this.port = port;
112 }
113
114 private static final long serialVersionUID = -1;
115 private static final int SERIAL_VERSION = 1;
116
117
118 private void writeObject(ObjectOutputStream out) throws IOException {
119 out.writeInt(SERIAL_VERSION);
120 out.writeUTF(host);
121 out.writeInt(port);
122 }
123
124 private void readObject(ObjectInputStream in) throws IOException {
125 int ver = in.readInt();
126 switch (ver) {
127 case 1:
128 readObjectVer1(in);
129 break;
130 }
131 }
132
133 private void readObjectVer1(ObjectInputStream in) throws IOException {
134 host = in.readUTF();
135 port = in.readInt();
136 }
137
138 @Override
139 public String getJsonFieldName() {
140 return TCP_IP_CONNECTION;
141 }
142
143 @Override
144 public int hashCode() {
145 int hash = 7;
146 hash = 17 * hash + Objects.hashCode(this.host);
147 hash = 17 * hash + this.port;
148 return hash;
149 }
150
151 @Override
152 public boolean equals(Object obj) {
153 if (this == obj) {
154 return true;
155 }
156 if (obj == null) {
157 return false;
158 }
159 if (getClass() != obj.getClass()) {
160 return false;
161 }
162 final TcpIpConnection other = (TcpIpConnection) obj;
163 if (this.port != other.port) {
164 return false;
165 }
166 if (!Objects.equals(this.host, other.host)) {
167 return false;
168 }
169 return true;
170 }
171
172 @Override
173 public String toString() {
174 return "TcpIpConnection{" + "host=" + host + ", port=" + port + '}';
175 }
176
177 @Override
178 public String getName() {
179 return host + ":" + port;
180 }
181 }