View Javadoc
1   package net.sf.mbus4j;
2   
3   /*
4    * #%L
5    * mbus4j-core
6    * %%
7    * Copyright (C) 2009 - 2014 MBus4J
8    * %%
9    * mbus4j - Drivers for the M-Bus protocol - http://mbus4j.sourceforge.net/
10   * Copyright (C) 2009-2014, mbus4j.sf.net, and individual contributors as indicated
11   * by the @authors tag. See the copyright.txt in the distribution for a
12   * full listing of individual contributors.
13   * 
14   * This is free software; you can redistribute it and/or modify it
15   * under the terms of the GNU General Public License as
16   * published by the Free Software Foundation; either version 3 of
17   * the License, or (at your option) any later version.
18   * 
19   * This software is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22   * Lesser General Public License for more details.
23   * 
24   * You should have received a copy of the GNU Lesser General Public
25   * License along with this software; if not, write to the Free
26   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
27   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
28   * #L%
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   * @author aploese
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          // load here to sppeddup furher access!!!
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     // Serialization for saveDataSource
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 }