View Javadoc

1   package cz.cuni.amis.pogamut.base.agent.impl;
2   
3   import java.net.InetAddress;
4   import java.net.UnknownHostException;
5   import java.rmi.server.UID;
6   import java.util.Random;
7   import java.util.UUID;
8   
9   import com.google.inject.Inject;
10  import com.google.inject.name.Named;
11  
12  import cz.cuni.amis.pogamut.base.agent.IAgentId;
13  import cz.cuni.amis.pogamut.base.utils.guice.AgentScoped;
14  import cz.cuni.amis.utils.NullCheck;
15  import cz.cuni.amis.utils.flag.Flag;
16  import cz.cuni.amis.utils.token.Token;
17  import cz.cuni.amis.utils.token.Tokens;
18  
19  /**
20   * Default AgentId providing unique id based on {@link UID} and IP of the host.
21   * @author Jimmy
22   */
23  @AgentScoped
24  public class AgentId implements IAgentId {
25  
26  	public static final String AGENT_NAME_DEPENDENCY = "AgentName";
27  	
28  	/**
29  	 * Provides unique number of the agent instance in the scope of JVM.
30  	 */
31  	private static int agentCounter = 0;
32  	
33  	protected static Random random = new Random(System.currentTimeMillis());
34  	
35  	public static final String NO_NAME = "no_name";
36  
37  	private Flag<String> name = new Flag<String>(NO_NAME);
38  
39  	private Token token;
40  	
41  	public AgentId() {
42  		UUID uuid = null;
43  		try {
44  			synchronized(random) {
45  				uuid = new UUID(random.nextLong(), random.nextLong());
46  			}
47  			this.token = Tokens.get(getClass().getSimpleName() + "-" + (++agentCounter) + "@"
48  					+ InetAddress.getLocalHost().getCanonicalHostName() + "/" + uuid.toString());
49  		} catch (UnknownHostException ex) {
50  			if (uuid == null) throw new IllegalStateException("Can't initialize AgentId, instantiation of UUID failed.");
51  			this.token = Tokens.get(getClass().getSimpleName() + "-" + (++agentCounter) + "@unknownHost/" + uuid.toString());
52  		}
53  	}
54  	
55  	@Inject
56  	public AgentId(@Named(AGENT_NAME_DEPENDENCY) String agentName) {
57  		NullCheck.check(agentName, "name");
58  		UUID uuid = null;
59  		try {
60  			synchronized(random) {
61  				uuid = new UUID(random.nextLong(), random.nextLong());
62  			}
63  			this.token = Tokens.get(agentName + "-" + (++agentCounter) + "@"
64  					+ InetAddress.getLocalHost().getCanonicalHostName() + "/" + uuid.toString());
65  		} catch (UnknownHostException ex) {
66  			this.token = Tokens.get(getClass().getSimpleName() + "-" + (++agentCounter) + "@unknownHost/" + uuid.toString());
67  		}
68  		getName().setFlag(agentName);
69  	}
70  	
71  	@Override
72  	public int hashCode() {
73  		return this.token.hashCode();
74  	}
75  	
76  	@Override
77  	public boolean equals(Object obj) {
78  		if (obj == null) return false;
79  		if (!(obj instanceof AgentId)) return false;
80  		return token.equals(((AgentId)obj).token);
81  	}
82  	
83  	@Override
84  	public Flag<String> getName() {
85  		return name;
86  	}
87  
88  	@Override
89  	public long[] getIds() {
90  		return token.getIds();
91  	}
92  
93  	@Override
94  	public String getToken() {
95  		return token.getToken();
96  	}
97  	
98  	@Override
99  	public String toString() {
100 		return "AgentId[" + getToken() + "]";
101 	}
102 	
103 }