PageRenderTime 21ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/network/Server.java

http://inequity.googlecode.com/
Java | 155 lines | 109 code | 25 blank | 21 comment | 1 complexity | 11c5be14909818ad3b83afdd729729c8 MD5 | raw file
  1package network;
  2
  3import elements.CompObj;
  4import elements.Consts;
  5import elements.LobbyNode;
  6import java.awt.Dimension;
  7import java.io.IOException;
  8import java.io.ObjectInputStream;
  9import java.io.ObjectOutputStream;
 10import java.util.logging.Level;
 11import java.util.logging.Logger;
 12import javax.swing.JFrame;
 13import java.net.*;
 14import java.util.ArrayList;
 15import java.util.concurrent.PriorityBlockingQueue;
 16import javax.swing.JScrollPane;
 17import javax.swing.JTextArea;
 18import javax.swing.SwingUtilities;
 19import packets.CommandRequest;
 20
 21/**
 22 * Server
 23 */
 24public class Server extends JFrame
 25{
 26  public ObjectInputStream socketIn;
 27  public ObjectOutputStream socketOut;
 28
 29  private JTextArea systemLog;
 30  private JScrollPane logPane;
 31
 32  /**
 33   * Constructor
 34   * Initializes minimalistic logging GUI
 35   */
 36  public Server()
 37  {
 38    setTitle("InEquity Server");
 39    setSize(new Dimension(500, 200));
 40    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 41    systemLog = new JTextArea();
 42    logPane = new JScrollPane(systemLog);
 43    systemLog.setEditable(false);
 44    add(logPane);
 45  }
 46
 47  /**
 48   * Waits for clients to connect and returns a proxy with their connection objects
 49   * @param serverSocket the listening server socket
 50   * @param srvQ server request queue
 51   * @return proxy containing connection objects for single client
 52   */
 53  public NetQProxy acceptClientConnection(ServerSocket serverSocket,
 54          PriorityBlockingQueue<CommandRequest> srvQ)
 55  {
 56    Socket IOSocket = null;
 57    PriorityBlockingQueue<CommandRequest> clientQ = null;
 58    Net2QRunnable rIn = null;
 59    Q2NetRunnable rOut = null;
 60    Thread tIn = null;
 61    Thread tOut = null;
 62
 63    try
 64    {
 65      IOSocket = serverSocket.accept();
 66
 67      socketOut = new ObjectOutputStream(IOSocket.getOutputStream());
 68      socketIn = new ObjectInputStream(IOSocket.getInputStream());
 69
 70      clientQ = new PriorityBlockingQueue<CommandRequest>(Consts.SERVER_QUEUE_SIZE, new CompObj());
 71
 72      rOut = new Q2NetRunnable(socketOut, clientQ);
 73      rOut.enableLogging(this);
 74
 75      rIn = new Net2QRunnable(socketIn, srvQ);
 76      rIn.setLoggingServer(this);
 77
 78      tOut = new Thread(rOut);
 79      tIn = new Thread(rIn);
 80
 81      tOut.start();
 82      tIn.start();
 83
 84    }
 85    catch(IOException e)
 86    {
 87      e.printStackTrace();
 88    }
 89
 90    return new NetQProxy(tIn, tOut, clientQ);
 91  }
 92
 93  /**
 94   * Posts a log message to the gui
 95   * @param logMsg message to be logged
 96   */
 97  public void postLogMsg(String logMsg)
 98  {
 99    systemLog.append('\n' + logMsg);
100    SwingUtilities.invokeLater(new Runnable()
101    {
102      public void run()
103      {
104        systemLog.setCaretPosition(systemLog.getText().length());
105      }
106    });
107  }
108
109  /**
110   * Requires one port parameter
111   * @param args command-line arguments
112   */
113  public static void main(String[] args)
114  {
115    int port = Consts.DEFAULT_PORT;
116
117    PriorityBlockingQueue<CommandRequest> srvQ =
118      new PriorityBlockingQueue<CommandRequest>
119        (Consts.SERVER_QUEUE_SIZE, new CompObj());
120
121    ArrayList<LobbyNode> lobbyList = new ArrayList<LobbyNode>();
122
123    try
124    {
125      Server srv = new Server();
126      Lobby lobby = new Lobby(srvQ, srv, lobbyList);
127
128      if(args.length >= 1)
129      {
130        port = Integer.parseInt(args[0]);
131      }
132
133      ServerSocket server = new ServerSocket(port);
134
135      srv.setVisible(true);
136
137      srv.postLogMsg("Allowing lobby members");
138
139      lobby.startListener();
140
141      while(true)
142      {
143        lobby.addPlayer(srv.acceptClientConnection(server, srvQ));
144      }
145    }
146    catch (SocketException e)
147    {
148      System.out.println("Socket already taken, exiting.");
149    }
150    catch (Exception ex)
151    {
152      Logger.getLogger(Server.class.getName()).log(Level.SEVERE, "Server", ex);
153    }
154  }
155}