MyDaemonInstallationNodeContribution.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 DataModel model;
  20. // private final MyDaemonDaemonService daemonService;
  21. private MyDaemonInterface daemonInterface;
  22. private Timer uiTimer;
  23. private Timer statusTimer;
  24. public MyDaemonInstallationNodeContribution(MyDaemonDaemonService daemonService, DataModel model) {
  25. // this.daemonService = daemonService;
  26. this.model = model;
  27. daemonInterface = new MyDaemonInterface("10.0.31.42", 8082);
  28. // applyDesiredDaemonStatus();
  29. }
  30. @Input(id = POPUPTITLE_KEY)
  31. private InputTextField popupTitleField;
  32. @Input(id = "btnEnableDaemon")
  33. private InputButton enableDaemonButton;
  34. @Input(id = "btnRef")
  35. private InputButton refButton;
  36. @Input(id = "textIP")
  37. private InputTextField refTextIP;
  38. @Input(id = "textPort")
  39. private InputTextField refTextPort;
  40. @Label(id = "lblStatus")
  41. private LabelComponent refLblStatus;
  42. @Input(id = "btnDisableDaemon")
  43. private InputButton disableDaemonButton;
  44. @Label(id = "lblDaemonStatus")
  45. private LabelComponent daemonStatusLabel;
  46. @Input(id = POPUPTITLE_KEY)
  47. public void onMessageChange(InputEvent event) {
  48. if (event.getEventType() == InputEvent.EventType.ON_CHANGE) {
  49. setPopupTitle(popupTitleField.getText());
  50. }
  51. }
  52. @Input(id = "btnRef")
  53. public void onBtnRef(InputEvent event) {
  54. if (event.getEventType() == InputEvent.EventType.ON_CHANGE) {
  55. try {
  56. getDaemonInterface().client.execute("ref", new String[]{"foo", "bar"});
  57. } catch (XmlRpcException e) {
  58. e.printStackTrace();
  59. }
  60. }
  61. }
  62. @Input(id = "btnEnableDaemon")
  63. public void onStartClick(InputEvent event) {
  64. if (event.getEventType() == InputEvent.EventType.ON_CHANGE) {
  65. setDaemonEnabled(true);
  66. // applyDesiredDaemonStatus();
  67. }
  68. }
  69. @Input(id = "btnDisableDaemon")
  70. public void onStopClick(InputEvent event) {
  71. if (event.getEventType() == InputEvent.EventType.ON_CHANGE) {
  72. setDaemonEnabled(false);
  73. // applyDesiredDaemonStatus();
  74. }
  75. }
  76. @Override
  77. public void openView() {
  78. enableDaemonButton.setText("Start Daemon");
  79. disableDaemonButton.setText("Stop daemon");
  80. popupTitleField.setText(getPopupTitle());
  81. //UI updates from non-GUI threads must use EventQueue.invokeLater (or SwingUtilities.invokeLater)
  82. uiTimer = new Timer(true);
  83. uiTimer.schedule(new TimerTask() {
  84. @Override
  85. public void run() {
  86. EventQueue.invokeLater(new Runnable() {
  87. @Override
  88. public void run() {
  89. updateUI();
  90. }
  91. });
  92. }
  93. }, 0, 1000);
  94. statusTimer = new Timer(true);
  95. statusTimer.schedule(new TimerTask() {
  96. @Override
  97. public void run() {
  98. EventQueue.invokeLater(new Runnable() {
  99. @Override
  100. public void run() {
  101. try {
  102. Object res = getDaemonInterface().client.execute("status", new String[]{});
  103. refLblStatus.setText(res.toString());
  104. System.out.println(res);
  105. } catch (XmlRpcException e) {
  106. e.printStackTrace();
  107. }
  108. }
  109. });
  110. }},0,1000);
  111. }
  112. private void updateUI() {
  113. DaemonContribution.State state = getDaemonState();
  114. if (state == DaemonContribution.State.RUNNING) {
  115. enableDaemonButton.setEnabled(false);
  116. disableDaemonButton.setEnabled(true);
  117. } else {
  118. enableDaemonButton.setEnabled(true);
  119. disableDaemonButton.setEnabled(false);
  120. }
  121. String text = "";
  122. switch (state) {
  123. case RUNNING:
  124. text = "My Daemon runs";
  125. break;
  126. case STOPPED:
  127. text = "My Daemon stopped";
  128. break;
  129. case ERROR:
  130. text = "My Daemon failed";
  131. break;
  132. }
  133. daemonStatusLabel.setText(text);
  134. }
  135. @Override
  136. public void closeView() {
  137. if (uiTimer != null) {
  138. uiTimer.cancel();
  139. }
  140. }
  141. @Override
  142. public void generateScript(ScriptWriter writer) {
  143. writer.globalVariable(XMLRPC_VARIABLE, "rpc_factory(\"xmlrpc\", \"http://192.168.20.111:8081/\")");
  144. // Apply the settings to the daemon on program start in the Installation pre-amble
  145. writer.appendLine(XMLRPC_VARIABLE + ".set_title(\"" + getPopupTitle() + "\")");
  146. }
  147. public String getPopupTitle() {
  148. if (!model.isSet(POPUPTITLE_KEY)) {
  149. resetToDefaultValue();
  150. }
  151. return model.get(POPUPTITLE_KEY, DEFAULT_VALUE);
  152. }
  153. private void setPopupTitle(String title) {
  154. if ("".equals(title)) {
  155. resetToDefaultValue();
  156. } else {
  157. model.set(POPUPTITLE_KEY, title);
  158. // Apply the new setting to the daemon for real-time preview purposes
  159. // Note this might influence a running program, since the actual state is stored in the daemon.
  160. setDaemonTitle(title);
  161. }
  162. }
  163. private void resetToDefaultValue() {
  164. popupTitleField.setText(DEFAULT_VALUE);
  165. model.set(POPUPTITLE_KEY, DEFAULT_VALUE);
  166. setDaemonTitle(DEFAULT_VALUE);
  167. }
  168. private void setDaemonTitle(String title) {
  169. try {
  170. daemonInterface.setTitle(title);
  171. } catch(Exception e){
  172. System.err.println("Could not set the title in the daemon process.");
  173. }
  174. }
  175. // private void applyDesiredDaemonStatus() {
  176. // new Thread(new Runnable() {
  177. // @Override
  178. // public void run() {
  179. // if (isDaemonEnabled()) {
  180. // // Download the daemon settings to the daemon process on initial start for real-time preview purposes
  181. // try {
  182. // awaitDaemonRunning(5000);
  183. // daemonInterface.setTitle(getPopupTitle());
  184. // } catch (Exception e) {
  185. // System.err.println("Could not set the title in the daemon process.");
  186. // }
  187. // } else {
  188. // daemonService.getDaemon().stop();
  189. // }
  190. // }
  191. // }).start();
  192. // }
  193. // private void awaitDaemonRunning(long timeOutMilliSeconds) throws InterruptedException {
  194. // daemonService.getDaemon().start();
  195. // long endTime = System.nanoTime() + timeOutMilliSeconds * 1000L * 1000L;
  196. // while(System.nanoTime() < endTime && (daemonService.getDaemon().getState() != DaemonContribution.State.RUNNING || !daemonInterface.isReachable())) {
  197. // Thread.sleep(100);
  198. // }
  199. // }
  200. //
  201. private DaemonContribution.State getDaemonState(){
  202. return DaemonContribution.State.RUNNING;
  203. // return daemonService.getDaemon().getState();
  204. }
  205. private Boolean isDaemonEnabled() {
  206. return model.get(ENABLED_KEY, true); //This daemon is enabled by default
  207. }
  208. private void setDaemonEnabled(Boolean enable) {
  209. model.set(ENABLED_KEY, enable);
  210. }
  211. public String getXMLRPCVariable(){
  212. return XMLRPC_VARIABLE;
  213. }
  214. public MyDaemonInterface getDaemonInterface() {return daemonInterface; }
  215. }