MyDaemonInstallationNodeContribution.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 XMLRPC_VARIABLE = "axis";
  16. private static final String ENABLED_KEY = "enabled";
  17. private static final String IP = "ip";
  18. private static final String PORT = "port";
  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(getIP(), Integer.parseInt(getPort()));
  28. // applyDesiredDaemonStatus();
  29. Activator.daemonInterface=daemonInterface;
  30. }
  31. @Input(id = "btnEnableDaemon")
  32. private InputButton enableDaemonButton;
  33. @Input(id = "btnRef")
  34. private InputButton refButtonRef;
  35. @Input(id = "btnReady")
  36. private InputButton refButtonrReady;
  37. @Input(id = "textIP")
  38. private InputTextField refTextIP;
  39. @Input(id = "textPort")
  40. private InputTextField refTextPort;
  41. @Label(id = "lblStatus")
  42. private LabelComponent refLblStatus;
  43. @Input(id = "btnDisableDaemon")
  44. private InputButton disableDaemonButton;
  45. @Label(id = "lblDaemonStatus")
  46. private LabelComponent daemonStatusLabel;
  47. @Input(id = "btnRef")
  48. public void onBtnRef(InputEvent event) {
  49. if (event.getEventType() == InputEvent.EventType.ON_CHANGE) {
  50. try {
  51. getDaemonInterface().client.execute("ref", new String[]{"foo", "bar"});
  52. } catch (XmlRpcException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. }
  57. @Input(id = "btnReady")
  58. public void onBtnReady(InputEvent event) {
  59. if (event.getEventType() == InputEvent.EventType.ON_RELEASED) {
  60. try {
  61. getDaemonInterface().client.execute("ready", new String[]{});
  62. } catch (XmlRpcException e) {
  63. e.printStackTrace();
  64. }
  65. }
  66. }
  67. @Input(id = "btnEnableDaemon")
  68. public void onStartClick(InputEvent event) {
  69. if (event.getEventType() == InputEvent.EventType.ON_CHANGE) {
  70. setDaemonEnabled(true);
  71. // applyDesiredDaemonStatus();
  72. }
  73. }
  74. @Input(id = "btnDisableDaemon")
  75. public void onStopClick(InputEvent event) {
  76. if (event.getEventType() == InputEvent.EventType.ON_CHANGE) {
  77. setDaemonEnabled(false);
  78. // applyDesiredDaemonStatus();
  79. }
  80. }
  81. @Override
  82. public void openView() {
  83. enableDaemonButton.setText("Start Daemon");
  84. disableDaemonButton.setText("Stop daemon");
  85. refButtonrReady.setText("Ready");
  86. refTextIP.setText(getIP());
  87. refTextPort.setText(getPort());
  88. //UI updates from non-GUI threads must use EventQueue.invokeLater (or SwingUtilities.invokeLater)
  89. uiTimer = new Timer(true);
  90. uiTimer.schedule(new TimerTask() {
  91. @Override
  92. public void run() {
  93. EventQueue.invokeLater(new Runnable() {
  94. @Override
  95. public void run() {
  96. updateUI();
  97. }
  98. });
  99. }
  100. }, 0, 1000);
  101. }
  102. private void updateUI() {
  103. DaemonContribution.State state = getDaemonState();
  104. if (state == DaemonContribution.State.RUNNING) {
  105. enableDaemonButton.setEnabled(false);
  106. disableDaemonButton.setEnabled(true);
  107. } else {
  108. enableDaemonButton.setEnabled(true);
  109. disableDaemonButton.setEnabled(false);
  110. }
  111. String text = "";
  112. switch (state) {
  113. case RUNNING:
  114. text = "My Daemon runs";
  115. break;
  116. case STOPPED:
  117. text = "My Daemon stopped";
  118. break;
  119. case ERROR:
  120. text = "My Daemon failed";
  121. break;
  122. }
  123. daemonStatusLabel.setText(text);
  124. }
  125. @Override
  126. public void closeView() {
  127. if (uiTimer != null) {
  128. uiTimer.cancel();
  129. }
  130. setIP(refTextIP.getText());
  131. setPort(refTextPort.getText());
  132. }
  133. @Override
  134. public void generateScript(ScriptWriter writer) {
  135. writer.assign(XMLRPC_VARIABLE, "rpc_factory(\"xmlrpc\", \"http://"+ getIP()+":"+ getPort()+"\")");
  136. writer.writeChildren();
  137. }
  138. public String getIP() {
  139. return model.get(IP, "127.0.0.1");
  140. }
  141. public void setIP(String ip) {
  142. model.set(IP, ip);
  143. }
  144. public String getPort() {
  145. return model.get(PORT, "8080");
  146. }
  147. public void setPort(String ip) {
  148. model.set(PORT, ip);
  149. }
  150. private void setDaemonTitle(String title) {
  151. try {
  152. daemonInterface.setTitle(title);
  153. } catch(Exception e){
  154. System.err.println("Could not set the title in the daemon process.");
  155. }
  156. }
  157. // private void applyDesiredDaemonStatus() {
  158. // new Thread(new Runnable() {
  159. // @Override
  160. // public void run() {
  161. // if (isDaemonEnabled()) {
  162. // // Download the daemon settings to the daemon process on initial start for real-time preview purposes
  163. // try {
  164. // awaitDaemonRunning(5000);
  165. // daemonInterface.setTitle(getPopupTitle());
  166. // } catch (Exception e) {
  167. // System.err.println("Could not set the title in the daemon process.");
  168. // }
  169. // } else {
  170. // daemonService.getDaemon().stop();
  171. // }
  172. // }
  173. // }).start();
  174. // }
  175. // private void awaitDaemonRunning(long timeOutMilliSeconds) throws InterruptedException {
  176. // daemonService.getDaemon().start();
  177. // long endTime = System.nanoTime() + timeOutMilliSeconds * 1000L * 1000L;
  178. // while(System.nanoTime() < endTime && (daemonService.getDaemon().getState() != DaemonContribution.State.RUNNING || !daemonInterface.isReachable())) {
  179. // Thread.sleep(100)
  180. // }
  181. // }
  182. //
  183. private DaemonContribution.State getDaemonState(){
  184. return DaemonContribution.State.RUNNING;
  185. // return daemonService.getDaemon().getState();
  186. }
  187. private Boolean isDaemonEnabled() {
  188. return model.get(ENABLED_KEY, true); //This daemon is enabled by default
  189. }
  190. private void setDaemonEnabled(Boolean enable) {
  191. model.set(ENABLED_KEY, enable);
  192. }
  193. public String getXMLRPCVariable(){
  194. return XMLRPC_VARIABLE;
  195. }
  196. public MyDaemonInterface getDaemonInterface() {return daemonInterface; }
  197. public boolean isDefined() {
  198. return true;
  199. }
  200. }