PageRenderTime 13ms CodeModel.GetById 8ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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