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.Closeable;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.ObjectInputStream;
34 import java.io.ObjectOutputStream;
35 import java.io.OutputStream;
36 import java.io.Serializable;
37 import net.sf.json.JSONObject;
38 import net.sf.mbus4j.json.JSONSerializable;
39 import net.sf.mbus4j.json.JsonSerializeType;
40
41
42
43
44
45 public abstract class Connection implements JSONSerializable, Serializable, Closeable {
46
47 public static final int DEFAULT_BAUDRATE = 2400;
48
49
50
51
52 public State getConnState() {
53 return connState;
54 }
55
56
57
58
59 protected void setConnState(State connState) {
60 this.connState = connState;
61 }
62
63
64
65
66 public OutputStream getLoggingStream() {
67 return loggingStream;
68 }
69
70
71
72
73 public void setLoggingStream(OutputStream loggingStream) {
74 this.loggingStream = loggingStream;
75 }
76
77 public enum State {
78
79 OPENING, OPEN, CLOSING, CLOSED
80 };
81
82 private int bitPerSecond;
83
84 private int responseTimeOutOffset;
85 protected transient InputStream is;
86 protected transient OutputStream os;
87 private transient State connState = State.CLOSED;
88 private transient OutputStream loggingStream;
89
90 public Connection() {
91
92 }
93
94 public Connection(int bitPerSecond, int responseTimeOutOffset) {
95 this.bitPerSecond = bitPerSecond;
96 this.responseTimeOutOffset = responseTimeOutOffset;
97 }
98
99 public InputStream getInputStream() {
100 return is;
101 }
102
103 public OutputStream getOutputStrteam() {
104 return os;
105 }
106
107
108
109
110 public int getBitPerSecond() {
111 return bitPerSecond;
112 }
113
114 public abstract String getName();
115
116
117
118
119
120 public void setBitPerSecond(int bitPerSecond) {
121 this.bitPerSecond = bitPerSecond;
122 }
123
124
125
126
127 public int getResponseTimeOutOffset() {
128 return responseTimeOutOffset;
129 }
130
131
132
133
134 public void setResponseTimeOutOffset(int responseTimeOutOffset) {
135 this.responseTimeOutOffset = responseTimeOutOffset;
136 }
137
138 public abstract void open() throws IOException;
139
140 @Override
141 public JSONObject toJSON(JsonSerializeType jsonSerializeType) {
142 JSONObject result = new JSONObject();
143
144 result.accumulate("bitPerSecond", bitPerSecond);
145 result.accumulate("responseTimeOutOffset", responseTimeOutOffset);
146
147 return result;
148 }
149
150 @Override
151 public void fromJSON(JSONObject json) {
152 bitPerSecond = json.getInt("bitPerSecond");
153 responseTimeOutOffset = json.getInt("responseTimeOutOffset");
154 }
155
156 private static final long serialVersionUID = -1;
157 private static final int version = 1;
158
159
160 private void writeObject(ObjectOutputStream out) throws IOException {
161 out.writeInt(version);
162 out.writeInt(bitPerSecond);
163 out.writeInt(responseTimeOutOffset);
164 }
165
166 private void readObject(ObjectInputStream in) throws IOException {
167 int ver = in.readInt();
168 switch (ver) {
169 case 1:
170 readObjectVer1(in);
171 break;
172 }
173 }
174
175 private void readObjectVer1(ObjectInputStream in) throws IOException {
176 bitPerSecond = in.readInt();
177 responseTimeOutOffset = in.readInt();
178 }
179
180
181
182
183
184
185 public static Connection createFromJSON(JSONObject json) {
186 Connection result = null;
187 if (json.containsKey(SerialPortConnection.SERIAL_CONNECTION)) {
188 result = new SerialPortConnection();
189 result.fromJSON(json.getJSONObject(SerialPortConnection.SERIAL_CONNECTION));
190 } else if (json.containsKey(TcpIpConnection.TCP_IP_CONNECTION)) {
191 result = new TcpIpConnection();
192 result.fromJSON(json.getJSONObject(TcpIpConnection.TCP_IP_CONNECTION));
193 }
194 return result;
195 }
196
197 public abstract String getJsonFieldName();
198
199 }