/netcon Software/netcond/src/program/threads/ModuleConnector.java

https://github.com/sli92/netcon · Java · 335 lines · 213 code · 116 blank · 6 comment · 16 complexity · 3823a8992a37770923fd6d38e33d66b7 MD5 · raw file

  1. package program.threads;
  2. import java.io.BufferedReader;
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.net.Socket;
  7. import program.main.Netcond;
  8. import lib.module.Module;
  9. import lib.protocol.Netcon;
  10. import lib.protocol.NetconGET;
  11. public class ModuleConnector implements Runnable {
  12. Thread t;
  13. private Module module;
  14. private int connectionTries;
  15. private Socket clientSocket = null;
  16. private String terminate;
  17. private String timeout;
  18. public ModuleConnector(Module module) {
  19. this.module = module;
  20. setConnectionTries(1);
  21. t = new Thread(this, "ModuleConnector");
  22. t.start();
  23. terminate = "E: ModulThread stopped " + t.getId() + " (" + module.getHostname() + ", " + module.getIp() + ")";
  24. timeout = "E: Timeout " + t.getId() + " (" + module.getHostname() + ", " + module.getIp() + ")";
  25. }
  26. public void run() {
  27. System.out.println("I: ModulThread started " + t.getId() + " (" + module.getHostname() + ", " + module.getIp() + ")");
  28. DataOutputStream outToServer = null;
  29. BufferedReader inFromServer = null;
  30. // connection to the module
  31. while(true){
  32. try {
  33. clientSocket = new Socket(module.getIp(), 50003);
  34. clientSocket.setSoTimeout(5000);
  35. outToServer = new DataOutputStream(clientSocket.getOutputStream());
  36. // get number of devices
  37. outToServer.write(Netcon.netcon(NetconGET.devicecount, ""));
  38. inFromServer = new BufferedReader(new InputStreamReader(
  39. clientSocket.getInputStream()));
  40. // check wheter module is netcon compatible
  41. if (inFromServer.readLine().equals("OK")) {
  42. module.setDevicecount(Integer.parseInt(inFromServer.readLine()));
  43. module.setType(new int[module.getDevicecount()]);
  44. module.setValue(new String[module.getDevicecount()]);
  45. module.setDtype(new String[module.getDevicecount()]);
  46. } else {
  47. terminate(terminate);
  48. return;
  49. }
  50. break;
  51. // connection not possible
  52. } catch (Exception e) {
  53. if(connectionTries > 0) {
  54. timeout();
  55. continue;
  56. }
  57. else{
  58. System.out.println(timeout);
  59. terminate(terminate);
  60. return;
  61. }
  62. }
  63. }
  64. int i;
  65. int type[] = new int[module.getDevicecount()];
  66. String value[] = new String[module.getDevicecount()];
  67. String dtype[] = new String[module.getDevicecount()];
  68. String minValue[] = new String[module.getDevicecount()];
  69. String maxValue[] = new String[module.getDevicecount()];
  70. setConnectionTries(1);
  71. // get devicetype and value type for each device
  72. for (i = 0; i < module.getDevicecount(); i++) {
  73. try {
  74. outToServer.write(Netcon.netcon(NetconGET.devicetype,
  75. String.valueOf(i)));
  76. if(inFromServer.readLine().equals("OK"))
  77. type[i] = Integer.parseInt(inFromServer.readLine());
  78. else{
  79. inFromServer.readLine();
  80. continue;
  81. }
  82. threadSleep(10);
  83. outToServer.write(Netcon.netcon(NetconGET.min,
  84. String.valueOf(i)));
  85. if(inFromServer.readLine().equals("OK"))
  86. minValue[i] = inFromServer.readLine();
  87. else{
  88. inFromServer.readLine();
  89. continue;
  90. }
  91. threadSleep(10);
  92. outToServer.write(Netcon.netcon(NetconGET.max,
  93. String.valueOf(i)));
  94. if(inFromServer.readLine().equals("OK")) {
  95. maxValue[i] = inFromServer.readLine();
  96. }
  97. else{
  98. inFromServer.readLine();
  99. continue;
  100. }
  101. threadSleep(10);
  102. outToServer.write(Netcon.netcon(NetconGET.dtype,
  103. String.valueOf(i)));
  104. if(inFromServer.readLine().equals("OK"))
  105. dtype[i] = inFromServer.readLine();
  106. else{
  107. inFromServer.readLine();
  108. continue;
  109. }
  110. threadSleep(10);
  111. } catch (IOException e) {
  112. if(connectionTries > 0) {
  113. timeout();
  114. ;
  115. i = 0;
  116. continue;
  117. }
  118. else{
  119. System.out.println(timeout);
  120. terminate(terminate);
  121. return;
  122. }
  123. }
  124. module.setType(type);
  125. module.setDtype(dtype);
  126. module.setMinValue(minValue);
  127. module.setMaxValue(maxValue);
  128. }
  129. setConnectionTries(1);
  130. while (true) {
  131. long startTime = System.currentTimeMillis();
  132. try {
  133. outToServer.write(Netcon.netcon(NetconGET.uptime,
  134. String.valueOf(i)));
  135. if(inFromServer.readLine().equals("OK"))
  136. module.setUptime(inFromServer.readLine());
  137. else{
  138. inFromServer.readLine();
  139. continue;
  140. }
  141. } catch (IOException e2) {
  142. if(connectionTries > 0) {
  143. timeout();
  144. i = 0;
  145. continue;
  146. }
  147. else{
  148. System.out.println(timeout);
  149. terminate(terminate);
  150. closeSocket();
  151. synchronized ( Netcond.moduleList ) {
  152. Netcond.moduleList.remove(module);
  153. }
  154. return;
  155. }
  156. }
  157. threadSleep(10);
  158. for (i = 0; i < module.getDevicecount(); i++) {
  159. try {
  160. outToServer.write(Netcon.netcon(NetconGET.value,
  161. String.valueOf(i)));
  162. if(inFromServer.readLine().equals("OK")){
  163. value[i] = inFromServer.readLine();
  164. }
  165. else{
  166. inFromServer.readLine();
  167. continue;
  168. }
  169. threadSleep(10);
  170. setConnectionTries(1);
  171. } catch (IOException e) {
  172. if(connectionTries > 0) {
  173. timeout();
  174. i = 0;
  175. continue;
  176. }
  177. else{
  178. System.out.println(timeout);
  179. terminate(terminate);
  180. return;
  181. }
  182. }
  183. }
  184. module.setValue(value);
  185. while ((System.currentTimeMillis() - startTime) < 1000) {
  186. threadSleep(1);
  187. }
  188. }
  189. }
  190. private void setConnectionTries(int connectionTries) {
  191. this.connectionTries = connectionTries;
  192. }
  193. private void closeSocket() {
  194. try {
  195. clientSocket.close();
  196. } catch (IOException e1) {
  197. // TODO Auto-generated catch block
  198. e1.printStackTrace();
  199. }
  200. }
  201. private void timeout(){
  202. System.out.println(timeout);
  203. connectionTries--;
  204. }
  205. private void terminate(String msg) {
  206. System.out.println(msg);
  207. closeSocket();
  208. synchronized ( Netcond.moduleList ) {
  209. Netcond.moduleList.remove(module);
  210. }
  211. }
  212. private void threadSleep(int time){
  213. try {
  214. Thread.sleep(time);
  215. } catch (InterruptedException e) {
  216. e.printStackTrace();
  217. }
  218. }
  219. }