MyDaemonInstallationNodeContribution.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. package uraxis;
  2. import com.ur.urcap.api.contribution.DaemonContribution;
  3. import com.ur.urcap.api.contribution.InstallationNodeContribution;
  4. import com.ur.urcap.api.domain.data.DataModel;
  5. import com.ur.urcap.api.domain.script.ScriptWriter;
  6. import com.ur.urcap.api.ui.annotation.Div;
  7. import com.ur.urcap.api.ui.annotation.Input;
  8. import com.ur.urcap.api.ui.annotation.Label;
  9. import com.ur.urcap.api.ui.component.*;
  10. import org.apache.xmlrpc.XmlRpcException;
  11. import java.awt.EventQueue;
  12. import java.util.Timer;
  13. import java.util.TimerTask;
  14. public class MyDaemonInstallationNodeContribution implements InstallationNodeContribution {
  15. private static final String POPUPTITLE_KEY = "popuptitle";
  16. private static final String XMLRPC_VARIABLE = "my_daemon";
  17. private static final String ENABLED_KEY = "enabled";
  18. private static final String DEFAULT_VALUE = "HelloWorld";
  19. private static final String IP = "ip";
  20. private static final String PORT = "port";
  21. private DataModel model;
  22. // private final MyDaemonDaemonService daemonService;
  23. private MyDaemonInterface daemonInterface;
  24. private Timer uiTimer;
  25. private Timer statusTimer;
  26. public MyDaemonInstallationNodeContribution(MyDaemonDaemonService daemonService, DataModel model) {
  27. // this.daemonService = daemonService;
  28. this.model = model;
  29. daemonInterface = new MyDaemonInterface("10.0.31.42", 8082);
  30. // applyDesiredDaemonStatus();
  31. }
  32. @Input(id = POPUPTITLE_KEY)
  33. private InputTextField popupTitleField;
  34. @Input(id = "btnEnableDaemon")
  35. private InputButton enableDaemonButton;
  36. @Input(id = "btnRef")
  37. private InputButton refButtonRef;
  38. @Input(id = "textIP")
  39. private InputTextField refTextIP;
  40. @Input(id = "textPort")
  41. private InputTextField refTextPort;
  42. @Label(id = "lblStatus")
  43. private LabelComponent refLblStatus;
  44. @Input(id = "numberPos")
  45. private InputTextField refNumberPos;
  46. @Input(id = "btnPos")
  47. private InputButton refButtonPos;
  48. @Input(id = "btnDisableDaemon")
  49. private InputButton disableDaemonButton;
  50. @Label(id = "lblDaemonStatus")
  51. private LabelComponent daemonStatusLabel;
  52. @Input(id = POPUPTITLE_KEY)
  53. public void onMessageChange(InputEvent event) {
  54. if (event.getEventType() == InputEvent.EventType.ON_CHANGE) {
  55. setPopupTitle(popupTitleField.getText());
  56. }
  57. }
  58. @Input(id = "btnRef")
  59. public void onBtnRef(InputEvent event) {
  60. if (event.getEventType() == InputEvent.EventType.ON_CHANGE) {
  61. try {
  62. getDaemonInterface().client.execute("ref", new String[]{"foo", "bar"});
  63. } catch (XmlRpcException e) {
  64. e.printStackTrace();
  65. }
  66. }
  67. }
  68. @Input(id = "btnPos")
  69. public void onBtnPos(InputEvent event) {
  70. if (event.getEventType() == InputEvent.EventType.ON_CHANGE) {
  71. try {
  72. String pos=refNumberPos.getText();
  73. getDaemonInterface().client.execute("pos", new String[]{pos});
  74. } catch (XmlRpcException e) {
  75. e.printStackTrace();
  76. }
  77. }
  78. }
  79. @Input(id = "btnEnableDaemon")
  80. public void onStartClick(InputEvent event) {
  81. if (event.getEventType() == InputEvent.EventType.ON_CHANGE) {
  82. setDaemonEnabled(true);
  83. // applyDesiredDaemonStatus();
  84. }
  85. }
  86. @Input(id = "btnDisableDaemon")
  87. public void onStopClick(InputEvent event) {
  88. if (event.getEventType() == InputEvent.EventType.ON_CHANGE) {
  89. setDaemonEnabled(false);
  90. // applyDesiredDaemonStatus();
  91. }
  92. }
  93. @Override
  94. public void openView() {
  95. enableDaemonButton.setText("Start Daemon");
  96. disableDaemonButton.setText("Stop daemon");
  97. popupTitleField.setText(getPopupTitle());
  98. refTextIP.setText(getIP());
  99. //UI updates from non-GUI threads must use EventQueue.invokeLater (or SwingUtilities.invokeLater)
  100. uiTimer = new Timer(true);
  101. uiTimer.schedule(new TimerTask() {
  102. @Override
  103. public void run() {
  104. EventQueue.invokeLater(new Runnable() {
  105. @Override
  106. public void run() {
  107. updateUI();
  108. }
  109. });
  110. }
  111. }, 0, 1000);
  112. // statusTimer = new Timer(true);
  113. // statusTimer.schedule(new TimerTask() {
  114. // @Override
  115. // public void run() {
  116. // EventQueue.invokeLater(new Runnable() {
  117. // @Override
  118. // public void run() {
  119. // try {
  120. // Object res = getDaemonInterface().client.execute("status", new String[]{});
  121. // refLblStatus.setText("<html>"+res.toString()+"</html>");
  122. // System.out.println(res);
  123. // } catch (XmlRpcException e) {
  124. // e.printStackTrace();
  125. // }
  126. // }
  127. // });
  128. // }},0,1000);
  129. }
  130. private void updateUI() {
  131. DaemonContribution.State state = getDaemonState();
  132. if (state == DaemonContribution.State.RUNNING) {
  133. enableDaemonButton.setEnabled(false);
  134. disableDaemonButton.setEnabled(true);
  135. } else {
  136. enableDaemonButton.setEnabled(true);
  137. disableDaemonButton.setEnabled(false);
  138. }
  139. String text = "";
  140. switch (state) {
  141. case RUNNING:
  142. text = "My Daemon runs";
  143. break;
  144. case STOPPED:
  145. text = "My Daemon stopped";
  146. break;
  147. case ERROR:
  148. text = "My Daemon failed";
  149. break;
  150. }
  151. daemonStatusLabel.setText(text);
  152. }
  153. @Override
  154. public void closeView() {
  155. if (uiTimer != null) {
  156. uiTimer.cancel();
  157. }
  158. setIP(refTextIP.getText());
  159. setPort(refTextPort.getText());
  160. }
  161. @Override
  162. public void generateScript(ScriptWriter writer) {
  163. writer.assign(XMLRPC_VARIABLE, "rpc_factory(\"xmlrpc\", \"http://10.0.31.42:8082/\")");
  164. }
  165. public String getPopupTitle() {
  166. if (!model.isSet(POPUPTITLE_KEY)) {
  167. resetToDefaultValue();
  168. }
  169. return model.get(POPUPTITLE_KEY, DEFAULT_VALUE);
  170. }
  171. private void setPopupTitle(String title) {
  172. if ("".equals(title)) {
  173. resetToDefaultValue();
  174. } else {
  175. model.set(POPUPTITLE_KEY, title);
  176. // Apply the new setting to the daemon for real-time preview purposes
  177. // Note this might influence a running program, since the actual state is stored in the daemon.
  178. setDaemonTitle(title);
  179. }
  180. }
  181. public String getIP() {
  182. return model.get(IP, "127.0.0.1");
  183. }
  184. public void setIP(String ip) {
  185. model.set(IP, ip);
  186. }
  187. public String getPort() {
  188. return model.get(PORT, "127.0.0.1");
  189. }
  190. public void setPort(String ip) {
  191. model.set(PORT, ip);
  192. }
  193. private void resetToDefaultValue() {
  194. popupTitleField.setText(DEFAULT_VALUE);
  195. model.set(POPUPTITLE_KEY, DEFAULT_VALUE);
  196. setDaemonTitle(DEFAULT_VALUE);
  197. }
  198. private void setDaemonTitle(String title) {
  199. try {
  200. daemonInterface.setTitle(title);
  201. } catch(Exception e){
  202. System.err.println("Could not set the title in the daemon process.");
  203. }
  204. }
  205. // private void applyDesiredDaemonStatus() {
  206. // new Thread(new Runnable() {
  207. // @Override
  208. // public void run() {
  209. // if (isDaemonEnabled()) {
  210. // // Download the daemon settings to the daemon process on initial start for real-time preview purposes
  211. // try {
  212. // awaitDaemonRunning(5000);
  213. // daemonInterface.setTitle(getPopupTitle());
  214. // } catch (Exception e) {
  215. // System.err.println("Could not set the title in the daemon process.");
  216. // }
  217. // } else {
  218. // daemonService.getDaemon().stop();
  219. // }
  220. // }
  221. // }).start();
  222. // }
  223. // private void awaitDaemonRunning(long timeOutMilliSeconds) throws InterruptedException {
  224. // daemonService.getDaemon().start();
  225. // long endTime = System.nanoTime() + timeOutMilliSeconds * 1000L * 1000L;
  226. // while(System.nanoTime() < endTime && (daemonService.getDaemon().getState() != DaemonContribution.State.RUNNING || !daemonInterface.isReachable())) {
  227. // Thread.sleep(100);
  228. // }
  229. // }
  230. //
  231. private DaemonContribution.State getDaemonState(){
  232. return DaemonContribution.State.RUNNING;
  233. // return daemonService.getDaemon().getState();
  234. }
  235. private Boolean isDaemonEnabled() {
  236. return model.get(ENABLED_KEY, true); //This daemon is enabled by default
  237. }
  238. private void setDaemonEnabled(Boolean enable) {
  239. model.set(ENABLED_KEY, enable);
  240. }
  241. public String getXMLRPCVariable(){
  242. return XMLRPC_VARIABLE;
  243. }
  244. public MyDaemonInterface getDaemonInterface() {return daemonInterface; }
  245. }