summaryrefslogtreecommitdiff
path: root/src/org/noreply/fancydress/directory/Server.java
blob: c4052d9717b95ebb3ac30e96310fcf2e5538dbad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* $Id$ */
package org.noreply.fancydress.directory;

import org.noreply.fancydress.misc.Util;
import org.noreply.fancydress.status.*;
import org.noreply.fancydress.crypto.*;
import java.util.*;

/**
 * This class represents a server, pooling one or more server descriptors into one class.
 *
 * @see ServerDescriptor
 */
public class Server {
	/**
	 * Nickname of the server.
	 *
	 * Note that all nickname comparisons need to be done in a case insensitive way.
	 */
	String nickname;

	/**
	 * The asn1 representation of the server's identity key.
	 */
	byte[] identity;

	/**
	 * The hash of <code>identity</code>, which is the node's keyid
	 */
	byte[] keyid;

	/**
	 * A list of one of more server descriptors.
	 */
	ArrayList descriptors;

	/**
	 * Construct a Server from a given ServerDescriptor.
	 *
	 * @param descriptor a ServerDescriptor.
	 */
	public Server(ServerDescriptor descriptor) {
		nickname = descriptor.getNickname();
		identity = descriptor.getIdentity();
		keyid = CryptoPrimitives.hash(identity);

		descriptors = new ArrayList();
		descriptors.add(descriptor);
	}

	/**
	 * Add a ServerDescriptor to this Server.
	 *
	 * @param descriptor a ServerDescriptor to add.
	 * @throws Mix3BadServerFormatException if the nickname or the identity key do not match this server's nickname or identity key.
	 */
	public void addDescriptor(ServerDescriptor descriptor) throws Mix3BadServerFormatException {

		if (!nickname.equalsIgnoreCase(descriptor.getNickname()))
			/* This indicates an error in whoever called us */
			throw new Error("Nickname does not match.");

		if (!Util.equal(identity, descriptor.getIdentity()))
			/* While this might happen.  In this case this ServerDesc should be ignored */
			throw new Mix3BadServerFormatException("Identity does not match.");

		descriptors.add(descriptor);
	}

	/**
	 * Get the keyid (hash of the identity key) of this Server.
	 *
	 * @return keyid
	 */
	public byte[] getKeyID() {
		return keyid;
	}

	/**
	 * get the the currently valid server descriptor.
	 *
	 * @return current server descriptor
	 */
	public ServerDescriptor getDescriptor() throws Mix3Exception {
		ServerDescriptor result = null;
		Date now = new Date();

		for (int i=0; i<descriptors.size(); i++) {
			ServerDescriptor desc = (ServerDescriptor) descriptors.get(i);
			if (now.after( desc.getValidAfter() ) &&
			    now.before( desc.getValidUntil() ) &&
			    ((result == null) || desc.getPublished().after( result.getPublished())))
				result = desc;
		}
		if (result == null)
			throw new Mix3Exception("No valid server descriptor found.");
		return result;
	}
}