/* $Id$ */
package org.noreply.fancydress.directory;
import org.bouncycastle.util.encoders.Base64;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.noreply.fancydress.directory.parser.*;
import org.noreply.fancydress.misc.Util;
import org.noreply.fancydress.crypto.*;
import org.noreply.fancydress.status.*;
import java.util.*;
/**
* This class represents a type III Directory.
*
* A directory holds information about the state of a type III network, like
* recommended servers, which servers exist, their keys, etc.
*
* FIXME:
* This whole flex and cup thing was just a nice way to try these tools.
* Eventually this should be rewritten to a simple parser in Java itself.
* Writing it should be pretty straight forward and cut down the
* dependencies.
*
* @see Server
*/
public class Directory {
/**
* directory version we understand.
*/
private static final String DIRECTORY_VERSION = "0.2";
/**
* Hash holding all Servers.
*
* This hash holds all Servers. Since nickname are to be treaded case
* insensitive the hash's keys are the lowercased nicknames of the Servers.
*
* All values in this hash are instances of Server.
*
* @see Server
*/
private Hashtable byName;
/**
* Useable servers.
*
* A server is useable if it understands PacketVersions that we understand
* and has a serverdescriptor that is useable right now.
*/
private Server[] useableServers;
/**
* Recommended servers.
*/
private Server[] recommendedServers;
/*
* matrix of friends.
*
* (a, b) are friends, if a can send messages to b.
*/
/*
private boolean[][] friends;
*/
/**
* Create a directory from an entire DirectoryMessage.
*
* This constructor builds up the directory from the message
* m
. It optionally checks the directory's signature and
* splits the message into single server descriptors to load them.
*
* @param m the directory message
* @param checkDirectorySignature whether or not to check the directory's signature
* @throws Mix3BadDirectorySignatureException if the directory's signature is invalid.
* @throws Mix3BadServerSignatureException if a server descriptor's signature is invalid.
* @throws Mix3BadServerFormatException if a section is syntactially invalid and the error cannot be ignored.
*/
public Directory(DirectoryMessage m, boolean checkDirectorySignature) throws
Mix3BadDirectorySignatureException,
Mix3BadDirectoryFormatException,
Mix3BadServerFormatException,
Mix3BadServerSignatureException
{
byName = new Hashtable();
parseDirectory(m, checkDirectorySignature);
Collection all = byName.values();
ArrayList useable = new ArrayList();
for (Iterator i = all.iterator(); i.hasNext(); ) {
Server s = (Server) i.next();
if (s.isUseable())
useable.add(s);
}
useableServers = (Server[]) useable.toArray( new Server[useable.size()] );
ArrayList recommended = new ArrayList();
for (int i=0; i