MyDaemonInstallationNodeContribution.java 7.4 KB

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