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

http://razpub.googlecode.com/ · Java · 66 lines · 29 code · 7 blank · 30 comment · 2 complexity · 98d8b9c903510f7d38fbeb72d6133dd6 MD5 · raw file

  1. /**
  2. * Razvan's code. Copyright 2008 based on Apache (share alike) see LICENSE.txt for details.
  3. */
  4. package com.razie.pub.comms;
  5. import java.util.Collection;
  6. import java.util.Collections;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. /**
  10. * a group of agents representing a logical structure, i.e. all the agents in my home (that is
  11. * actually a special group, the "home cloud". The different groups you could have/belong to are
  12. * equivalent, except the home cloud. The home cloud is by default the current group that is the
  13. * target of distributed operations.
  14. *
  15. * agent clouds are the logical unit for distributed services, including negociation, database sync
  16. * etc.
  17. *
  18. * agent clouds can be configured statically (agent.xml) or dynamically, based on
  19. * AgentCloudNegociation
  20. *
  21. * TODO genericize this - clouds of any kind of agent/device, together with its protocol
  22. *
  23. * @author razvanc
  24. */
  25. public class AgentCloud {
  26. /** map<name,handle> - i'm so lazy I used a synchronized map :) */
  27. private Map<String, AgentHandle> agents = Collections
  28. .synchronizedMap(new HashMap<String, AgentHandle>());
  29. /** simple constructor with optional pre-population */
  30. public AgentCloud (AgentHandle...handles) {
  31. for (AgentHandle h : handles) this.put(h);
  32. }
  33. /** simple constructor with optional pre-population */
  34. public AgentCloud (Iterable<AgentHandle> handles) {
  35. for (AgentHandle h : handles) this.put(h);
  36. }
  37. /**
  38. * only access is to clone the sync'd collection. The individual agents may still be modified by
  39. * async status updates, but as assignments are atomic should be ok
  40. */
  41. public Map<String, AgentHandle> agents() {
  42. synchronized (agents) {
  43. Map<String, AgentHandle> copy = new HashMap<String, AgentHandle>();
  44. copy.putAll(agents);
  45. return copy;
  46. }
  47. }
  48. /**
  49. * agents are indexed by name which can't change. You can change IPs etc without any obvious
  50. * side effects (at least at this level :)
  51. */
  52. public AgentHandle put(AgentHandle h) {
  53. agents.put(h.name, h);
  54. return h;
  55. }
  56. public AgentHandle get(String name) {
  57. return agents.get(name);
  58. }
  59. }