/Users/mw17785/Projects/gamefinder/gamefinder/trunk/src/example/Server.java
/*
 * Server.java
 */

package example;
import cs101.comm.Announcer;
import cs101.comm.GameInfo;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

/**
 * An example use of cs101.comm.Announcer... with some audio to boot!
 */
public class Server {

    /** Creates a new instance of Server */
    public Server() {
        // set up the audio clip (an audio channel)
        Clip clip = null;
        try {
            // to get a resource file that resides in the same directory as
            // this class, use the getResource method of the Class to get an
            // appropriate URL, no matter where this class exists.
            AudioInputStream sound =
                    AudioSystem.getAudioInputStream(getClass().getResource("CLANG.AU"));
            clip = AudioSystem.getClip();
            clip.open(sound);
        } catch (Exception e) {
            e.printStackTrace();
        }
            
        try {
            // set up the server on some random port.  If you want a specific
            // port number, use the number instead of 0.
            ServerSocket ss = new ServerSocket(0);
            // now create a GameInfo describing our server.
            // the null in the third argument means that the GameInfo object
            // figures out the address of the local machine on its own.
            GameInfo gi = new GameInfo("SocketTest", "demo for class", null, 
                    ss.getLocalPort());
            // start announcing this game.  Typically, you just let the
            // Announcer object do its thing.  You can also hold on to the
            // object and close() it when you want to stop announcing.
            Announcer announcer = new Announcer(gi);
            System.out.println("Using port "+ss.getLocalPort());

            // handle connections
            while(true) {
                // get a single connection
                Socket sock = ss.accept();
                if (clip != null) {
                    // always rewind the audio before playing.
                    clip.setFramePosition(0);
                    clip.loop(0);
                }
                // create a handler to communicate with the client.
                new Handler(sock);
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    
    
    public static void main(String[] args) {
        new Server();
    }
}