/razweb/src/com/razie/pub/comms/AgentHandle.java

http://razpub.googlecode.com/ · Java · 141 lines · 93 code · 20 blank · 28 comment · 11 complexity · 37de5cb01377062ebc1295494a78ca6c MD5 · raw file

  1. /**
  2. * Razvan's public code. Copyright 2008 based on Apache license (share alike) see LICENSE.txt for
  3. * details. No warranty implied nor any liability assumed for this code.
  4. */
  5. package com.razie.pub.comms;
  6. import java.io.IOException;
  7. import java.io.Serializable;
  8. import java.net.InetSocketAddress;
  9. import java.net.Socket;
  10. //import razie.assets.AssetKey;
  11. //import razie.assets.AssetLocation;
  12. /**
  13. * represents a remote agent. Agent's name should normally be the same as the hostname, but do as
  14. * you must...
  15. *
  16. * @author razvanc99
  17. */
  18. @SuppressWarnings("serial")
  19. public class AgentHandle /* extends AssetKey */implements Cloneable, Serializable {
  20. /** the handles keep transient peer status info, if an updater service is used */
  21. public static enum DeviceStatus {
  22. UNKOWN, DOWN, UP, EXCLUDED
  23. }
  24. public static final String sCLASS = "RazAgent";
  25. public String name;
  26. public String hostname;
  27. public String ip;
  28. public String port;
  29. public String localdir;
  30. public String os;
  31. public transient DeviceStatus status = DeviceStatus.UNKOWN;
  32. // TODO why not use the AssetLocation?
  33. /** cache the url, format: "http://[ip|hostname]:port" */
  34. public String url;
  35. /** minimum information - use it for temporary handles or small tests...you'll have to */
  36. public AgentHandle(String name, String hostname, String ip, String port) {
  37. this(name, hostname, ip, port, "http://" + ip + ":" + port, "", "");
  38. }
  39. /** minimum information - use it for temporary handles or small tests...you'll have to */
  40. public AgentHandle(String name, String hostname, String ip, String port, String url) {
  41. this(name, hostname, ip, port, url, "", "");
  42. }
  43. /** full constructor */
  44. public AgentHandle(String name, String hostname, String ip, String port, String url, String os,
  45. String localdir) {
  46. // super(sCLASS, name, new AssetLocation(url));
  47. this.name = name;
  48. this.hostname = hostname;
  49. this.ip = ip;
  50. this.port = port;
  51. this.url = url;
  52. this.os = os;
  53. this.localdir = localdir;
  54. // make sure port is ok...
  55. try {
  56. if (port != null && port.length() > 0) {
  57. int p = Integer.parseInt(port);
  58. }
  59. } catch (Throwable t) {
  60. String msg = "PORT " + port + " FOR device=" + hostname
  61. + " WRONG - please check again. Must be a 4 digit number!";
  62. throw new IllegalStateException(msg, t);
  63. }
  64. }
  65. public String toString() {
  66. return "AgentHandle(" + name + ";" + hostname + ";" + ip + ";" + port + ";" + url + ";" + os + ";"
  67. + localdir + ")";
  68. }
  69. public String toSimpleString() {
  70. return name + "(" + url + ")";
  71. }
  72. public static AgentHandle fromString(String s) {
  73. if (!s.startsWith("AgentHandle("))
  74. throw new IllegalArgumentException("String is not an AgentHandle: " + s);
  75. String ss[] = s.split("[;()]");
  76. return new AgentHandle(ss[1], ss[2], ss[3], snull(ss, 4), snull(ss, 5), snull(ss, 6), snull(ss, 7));
  77. }
  78. private static final String snull(String[] ss, int idx) {
  79. if (ss.length > idx)
  80. return ss[idx];
  81. else
  82. return "";
  83. }
  84. public AgentHandle clone() {
  85. return new AgentHandle(this.name, this.hostname, this.ip, this.port, this.url, this.os, this.localdir);
  86. }
  87. public boolean equals(Object o) {
  88. AgentHandle other = (AgentHandle) o;
  89. if (other != null && this.name.equals(other.name) && this.hostname.equals(other.hostname)
  90. && this.port.equals(other.port))
  91. return true;
  92. return false;
  93. }
  94. /**
  95. * means i am currently "connected" to it, i.e. i think it's up and running. If you want to
  96. * double-check outside of the normal heartbeat schedule, use isUpNow
  97. */
  98. public boolean isUp() {
  99. return DeviceStatus.UP.equals(status);
  100. }
  101. /** double check right now if the target is up */
  102. public boolean isUpNow() {
  103. // timeout quickly
  104. if (this.port.length() <= 0) {
  105. // for now ignore those that don't run agents:
  106. return false;
  107. }
  108. try {
  109. Socket server = new Socket();
  110. int port = Integer.parseInt(this.port);
  111. server.connect(new InetSocketAddress(ip, port), 250);
  112. server.close();
  113. return true;
  114. } catch (IOException e1) {
  115. return false;
  116. } catch (Exception e) {
  117. // don't care what hapened...
  118. return false;
  119. }
  120. }
  121. }