PageRenderTime 41ms CodeModel.GetById 28ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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