/*
 * Copyright (C) 2006  Mike Murray
 *
 *  All rights reserved.
 *  This document may not be reused without
 *  express written permission from Mike Murray.
 *
 */

package net.sf.j3d.network.behavior;

import java.io.IOException;
import java.io.PrintStream;

import net.sf.j3d.network.Server;
import net.sf.j3d.network.ServerBehavior;

public class RandomPeerJoin implements ServerBehavior {
	public void behave(Server s, PrintStream out) {
		try {
			int i = (int) (s.getPeers().length * Math.random());
			String peers[] = s.getPeerList(i);
			out.println("RandomPeerJoin: Got peer list for server " + i +
						" (" + peers.length + " peers).");
			if (peers.length <= 0) return;
			
			int j = (int) (s.getPeers().length * Math.random());
			out.println("RandomPeerJoin: Attempting to open " + peers[j]);
			
			if (!s.open(peers[j])) {
				out.println("RandomPeerJoin: Unable to open " + peers[j]);
			} else {
				s.close(i);
			}
		} catch (IOException e) {
			System.out.println("RandomPeerJoin: " + e);
		}
	}
}
