/org.eclipse.paho.android.sample/src/main/java/org/eclipse/paho/android/sample/model/ConnectionModel.java

https://gitlab.com/jettijam/pubsub_project · Java · 343 lines · 279 code · 63 blank · 1 comment · 66 complexity · 233b8b7888064de57e88a3f63bdddb6b MD5 · raw file

  1. package org.eclipse.paho.android.sample.model;
  2. import android.os.Bundle;
  3. import android.util.Log;
  4. import org.eclipse.paho.android.sample.activity.ActivityConstants;
  5. import org.eclipse.paho.android.sample.activity.Connection;
  6. public class ConnectionModel {
  7. private static final String CLIENT_HANDLE = "CLIENT_HANDLE";
  8. private static final String CLIENT_ID = "CLIENT_ID";
  9. private static final String HOST_NAME = "HOST_NAME";
  10. private static final String PORT = "PORT";
  11. private static final String CLEAN_SESSION = "CLEAN_SESSION";
  12. private static final String USERNAME = "USERNAME";
  13. private static final String PASSWORD = "PASSWORD";
  14. private static final String TLS_SERVER_KEY = "TLS_SERVER_KEY";
  15. private static final String TLS_CLIENT_KEY = "TLS_CLIENT_KEY";
  16. private static final String TIMEOUT = "TIMEOUT";
  17. private static final String KEEP_ALIVE = "KEEP_ALIVE";
  18. private static final String LWT_TOPIC = "LWT_TOPIC";
  19. private static final String LWT_MESSAGE = "LWT_MESSAGE";
  20. private static final String LWT_QOS = "LWT_QOS";
  21. private static final String LWT_RETAIN = "LWT_RETAIN";
  22. private static final String TAG = "ConnectionModel";
  23. private String clientHandle = new String();
  24. private String clientId = "AndroidExampleClient";
  25. private String serverHostName = "iot.eclipse.org";
  26. private int serverPort = 1883;
  27. private boolean cleanSession = true;
  28. private String username = new String();
  29. private String password = new String();
  30. private boolean tlsConnection = false;
  31. private String tlsServerKey = new String();
  32. private String tlsClientKey = new String();
  33. private int timeout = 80;
  34. private int keepAlive = 200;
  35. private String lwtTopic = new String();
  36. private String lwtMessage = new String();
  37. private int lwtQos = 0;
  38. private boolean lwtRetain = false;
  39. public ConnectionModel(){
  40. }
  41. /** Initialise the ConnectionModel with an existing connection **/
  42. public ConnectionModel(Connection connection){
  43. clientHandle = connection.handle();
  44. clientId = connection.getId();
  45. serverHostName = connection.getHostName();
  46. serverPort = connection.getPort();
  47. cleanSession = connection.getConnectionOptions().isCleanSession();
  48. if(connection.getConnectionOptions().getUserName() == null){
  49. username = new String();
  50. }else {
  51. username = connection.getConnectionOptions().getUserName();
  52. }
  53. if(connection.getConnectionOptions().getPassword() != null) {
  54. password = new String(connection.getConnectionOptions().getPassword());
  55. } else {
  56. password = new String();
  57. }
  58. tlsServerKey = "--- TODO ---";
  59. tlsClientKey = "--- TODO ---";
  60. timeout = connection.getConnectionOptions().getConnectionTimeout();
  61. keepAlive = connection.getConnectionOptions().getKeepAliveInterval();
  62. if(connection.getConnectionOptions().getWillDestination() == null){
  63. lwtTopic = new String();
  64. } else {
  65. lwtTopic = connection.getConnectionOptions().getWillDestination();
  66. }
  67. if(connection.getConnectionOptions().getWillMessage() != null) {
  68. lwtMessage = new String(connection.getConnectionOptions().getWillMessage().getPayload());
  69. lwtQos = connection.getConnectionOptions().getWillMessage().getQos();
  70. lwtRetain = connection.getConnectionOptions().getWillMessage().isRetained();
  71. } else {
  72. lwtMessage = new String();
  73. lwtQos = 0;
  74. lwtRetain = false;
  75. }
  76. }
  77. public ConnectionModel(Bundle connectionBundle) {
  78. clientHandle = connectionBundle.getString(CLIENT_HANDLE);
  79. clientId = connectionBundle.getString(CLIENT_ID);
  80. serverHostName = connectionBundle.getString(HOST_NAME);
  81. serverPort = connectionBundle.getInt(PORT);
  82. cleanSession = connectionBundle.getBoolean(CLEAN_SESSION);
  83. username = connectionBundle.getString(USERNAME);
  84. password = connectionBundle.getString(PASSWORD);
  85. tlsServerKey = connectionBundle.getString(TLS_SERVER_KEY);
  86. tlsClientKey = connectionBundle.getString(TLS_CLIENT_KEY);
  87. timeout = connectionBundle.getInt(TIMEOUT);
  88. keepAlive = connectionBundle.getInt(KEEP_ALIVE);
  89. lwtTopic = connectionBundle.getString(LWT_TOPIC);
  90. lwtMessage = connectionBundle.getString(LWT_MESSAGE);
  91. lwtQos = connectionBundle.getInt(LWT_QOS);
  92. lwtRetain = connectionBundle.getBoolean(LWT_RETAIN);
  93. }
  94. public Bundle getConnectionBundle(){
  95. Bundle connectionBundle = new Bundle();
  96. connectionBundle.putString(CLIENT_HANDLE, clientHandle);
  97. connectionBundle.putString(CLIENT_ID, clientId);
  98. connectionBundle.putString(HOST_NAME, serverHostName);
  99. connectionBundle.putInt(PORT, serverPort);
  100. connectionBundle.putBoolean(CLEAN_SESSION, cleanSession);
  101. connectionBundle.putString(USERNAME, username);
  102. connectionBundle.putString(PASSWORD, password);
  103. connectionBundle.putString(TLS_SERVER_KEY, tlsServerKey);
  104. connectionBundle.putString(TLS_CLIENT_KEY, tlsClientKey);
  105. connectionBundle.putInt(TIMEOUT, timeout);
  106. connectionBundle.putInt(KEEP_ALIVE, keepAlive);
  107. connectionBundle.putString(LWT_TOPIC, lwtTopic);
  108. connectionBundle.putString(LWT_MESSAGE, lwtMessage);
  109. connectionBundle.putInt(LWT_QOS, lwtQos);
  110. connectionBundle.putBoolean(LWT_RETAIN, lwtRetain);
  111. return connectionBundle;
  112. }
  113. public String getClientHandle() {
  114. return clientHandle;
  115. }
  116. public void setClientHandle(String clientHandle) {
  117. this.clientHandle = clientHandle;
  118. }
  119. public String getClientId() {
  120. return clientId;
  121. }
  122. public void setClientId(String clientId) {
  123. this.clientId = clientId;
  124. }
  125. public String getServerHostName() {
  126. return serverHostName;
  127. }
  128. public void setServerHostName(String serverHostName) {
  129. this.serverHostName = serverHostName;
  130. }
  131. public int getServerPort() {
  132. return serverPort;
  133. }
  134. public void setServerPort(int serverPort) {
  135. this.serverPort = serverPort;
  136. }
  137. public boolean isCleanSession() {
  138. return cleanSession;
  139. }
  140. public void setCleanSession(boolean cleanSession) {
  141. this.cleanSession = cleanSession;
  142. }
  143. public String getUsername() {
  144. return username;
  145. }
  146. public void setUsername(String username) {
  147. this.username = username;
  148. }
  149. public String getPassword() {
  150. return password;
  151. }
  152. public void setPassword(String password) {
  153. this.password = password;
  154. }
  155. public String getTlsServerKey() {
  156. return tlsServerKey;
  157. }
  158. public void setTlsServerKey(String tlsServerKey) {
  159. this.tlsServerKey = tlsServerKey;
  160. }
  161. public String getTlsClientKey() {
  162. return tlsClientKey;
  163. }
  164. public void setTlsClientKey(String tlsClientKey) {
  165. this.tlsClientKey = tlsClientKey;
  166. }
  167. public int getTimeout() {
  168. return timeout;
  169. }
  170. public void setTimeout(int timeout) {
  171. this.timeout = timeout;
  172. }
  173. public int getKeepAlive() {
  174. return keepAlive;
  175. }
  176. public void setKeepAlive(int keepAlive) {
  177. this.keepAlive = keepAlive;
  178. }
  179. public String getLwtTopic() {
  180. return lwtTopic;
  181. }
  182. public void setLwtTopic(String lwtTopic) {
  183. this.lwtTopic = lwtTopic;
  184. }
  185. public String getLwtMessage() {
  186. return lwtMessage;
  187. }
  188. public void setLwtMessage(String lwtMessage) {
  189. this.lwtMessage = lwtMessage;
  190. }
  191. public int getLwtQos() {
  192. return lwtQos;
  193. }
  194. public void setLwtQos(int lwtQos) {
  195. this.lwtQos = lwtQos;
  196. }
  197. public boolean isLwtRetain() {
  198. return lwtRetain;
  199. }
  200. public void setLwtRetain(boolean lwtRetain) {
  201. this.lwtRetain = lwtRetain;
  202. }
  203. public boolean isTlsConnection() {
  204. return tlsConnection;
  205. }
  206. public void setTlsConnection(boolean tlsConnection) {
  207. this.tlsConnection = tlsConnection;
  208. }
  209. @Override
  210. public String toString() {
  211. return "ConnectionModel{" +
  212. "clientHandle='" + clientHandle + '\'' +
  213. ", clientId='" + clientId + '\'' +
  214. ", serverHostName='" + serverHostName + '\'' +
  215. ", serverPort=" + serverPort +
  216. ", cleanSession=" + cleanSession +
  217. ", username='" + username + '\'' +
  218. ", password='" + password + '\'' +
  219. ", tlsConnection=" + tlsConnection +
  220. ", tlsServerKey='" + tlsServerKey + '\'' +
  221. ", tlsClientKey='" + tlsClientKey + '\'' +
  222. ", timeout=" + timeout +
  223. ", keepAlive=" + keepAlive +
  224. ", lwtTopic='" + lwtTopic + '\'' +
  225. ", lwtMessage='" + lwtMessage + '\'' +
  226. ", lwtQos=" + lwtQos +
  227. ", lwtRetain=" + lwtRetain +
  228. '}';
  229. }
  230. @Override
  231. public boolean equals(Object o) {
  232. if (this == o) return true;
  233. if (o == null || getClass() != o.getClass()) return false;
  234. ConnectionModel that = (ConnectionModel) o;
  235. if (serverPort != that.serverPort) return false;
  236. if (cleanSession != that.cleanSession) return false;
  237. if (tlsConnection != that.tlsConnection) return false;
  238. if (timeout != that.timeout) return false;
  239. if (keepAlive != that.keepAlive) return false;
  240. if (lwtQos != that.lwtQos) return false;
  241. if (lwtRetain != that.lwtRetain) return false;
  242. if (clientHandle != null ? !clientHandle.equals(that.clientHandle) : that.clientHandle != null)
  243. return false;
  244. if (clientId != null ? !clientId.equals(that.clientId) : that.clientId != null)
  245. return false;
  246. if (serverHostName != null ? !serverHostName.equals(that.serverHostName) : that.serverHostName != null)
  247. return false;
  248. if (username != null ? !username.equals(that.username) : that.username != null)
  249. return false;
  250. if (password != null ? !password.equals(that.password) : that.password != null)
  251. return false;
  252. if (tlsServerKey != null ? !tlsServerKey.equals(that.tlsServerKey) : that.tlsServerKey != null)
  253. return false;
  254. if (tlsClientKey != null ? !tlsClientKey.equals(that.tlsClientKey) : that.tlsClientKey != null)
  255. return false;
  256. if (lwtTopic != null ? !lwtTopic.equals(that.lwtTopic) : that.lwtTopic != null)
  257. return false;
  258. return !(lwtMessage != null ? !lwtMessage.equals(that.lwtMessage) : that.lwtMessage != null);
  259. }
  260. @Override
  261. public int hashCode() {
  262. int result = clientHandle != null ? clientHandle.hashCode() : 0;
  263. result = 31 * result + (clientId != null ? clientId.hashCode() : 0);
  264. result = 31 * result + (serverHostName != null ? serverHostName.hashCode() : 0);
  265. result = 31 * result + serverPort;
  266. result = 31 * result + (cleanSession ? 1 : 0);
  267. result = 31 * result + (username != null ? username.hashCode() : 0);
  268. result = 31 * result + (password != null ? password.hashCode() : 0);
  269. result = 31 * result + (tlsConnection ? 1 : 0);
  270. result = 31 * result + (tlsServerKey != null ? tlsServerKey.hashCode() : 0);
  271. result = 31 * result + (tlsClientKey != null ? tlsClientKey.hashCode() : 0);
  272. result = 31 * result + timeout;
  273. result = 31 * result + keepAlive;
  274. result = 31 * result + (lwtTopic != null ? lwtTopic.hashCode() : 0);
  275. result = 31 * result + (lwtMessage != null ? lwtMessage.hashCode() : 0);
  276. result = 31 * result + lwtQos;
  277. result = 31 * result + (lwtRetain ? 1 : 0);
  278. return result;
  279. }
  280. }