HeartbeatAdapter.java

  1. package emissary.server.mvc.adapters;

  2. import emissary.client.EmissaryClient;
  3. import emissary.core.Namespace;
  4. import emissary.core.NamespaceException;
  5. import emissary.directory.DirectoryPlace;
  6. import emissary.directory.KeyManipulator;
  7. import emissary.place.IServiceProviderPlace;

  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;

  10. import javax.annotation.Nullable;

  11. /**
  12.  * Stuff for adapting the Directory heartbeat calls to HTTP
  13.  */
  14. public class HeartbeatAdapter extends EmissaryClient {

  15.     private static final Logger logger = LoggerFactory.getLogger(HeartbeatAdapter.class);

  16.     public static final String FROM_PLACE_NAME = "hbf";
  17.     public static final String TO_PLACE_NAME = "hbt";

  18.     /**
  19.      * Process the heartbeat call coming remotely over HTTP request params onto the specified (local) directory place
  20.      *
  21.      * @return reference to the place when found, null otherwise
  22.      */
  23.     @Nullable
  24.     public IServiceProviderPlace inboundHeartbeat(final String fromName, final String toName) throws NamespaceException {
  25.         if (toName == null) {
  26.             throw new IllegalArgumentException("No place specified in msg from " + fromName);
  27.         }

  28.         logger.debug("Servicing inbound heartbeat for {} to {}", fromName, toName);

  29.         final DirectoryPlace d = (DirectoryPlace) Namespace.lookup(toName);
  30.         if (!d.isStaticPeer(KeyManipulator.getDefaultDirectoryKey(fromName))) {
  31.             logger.warn("Contact attempted from {} but it is not a configured peer", fromName);
  32.             return null;
  33.         }

  34.         IServiceProviderPlace place = null;

  35.         try {
  36.             place = (IServiceProviderPlace) Namespace.lookup(toName);
  37.         } catch (NamespaceException ne) {
  38.             throw ne;
  39.         }

  40.         if (place == null) {
  41.             throw new IllegalArgumentException("No place found using name " + toName + " from " + fromName);
  42.         }

  43.         logger.debug("Heartbeat returning {}", place);

  44.         return place;
  45.     }
  46. }