package dk.socketmud; import java.util.ArrayList; import java.util.Iterator; import dk.socketmud.action.ActionFactory; import dk.socketmud.action.CommandSet; import dk.socketmud.action.IllegalActionException; import dk.socketmud.action.SMAction; import dk.socketmud.io.SMClient; import dk.socketmud.io.SMServer; import dk.socketmud.util.Log; public class JSocketMUD { public static void main(String[] args) { if (args.length < 1) { System.out.println("Which port do you wish to run the MUD on?"); System.exit(-1); } int port = 9009; try { port = Integer.parseInt(args[0]); } catch (NumberFormatException ex) { System.out.println("That is not a number!"); System.exit(-1); } if (port < 1024) { System.out.println("Ports below 1024 are restricted."); System.exit(-1); } try { SMServer server = new SMServer(port); long timestamp; long sleep; while (true) { timestamp = System.currentTimeMillis(); // see if there are any new connections to accept SMClient newClient = server.acceptNewConnections(); if (newClient != null) { newClient.write("#RWelcome #Yto #u#GSocketMUD#n #Bfor #CJava#n\r\n\n"); newClient.write("What name do you want? "); } ArrayList<CommandSet> cmds = server.readCommands(); Iterator<CommandSet> i = cmds.iterator(); while (i.hasNext()) { CommandSet cmd = i.next(); try { SMAction action = ActionFactory.getInstance(cmd.command); action.execute(cmd.ch, cmd.argument); } catch (IllegalActionException ex) { cmd.ch.writeDuh(); Log.logException(ex); } } // flush all output server.flush(); // sleep up to 0.25 second each loop sleep = 250 - (System.currentTimeMillis() - timestamp); if (sleep > 0) Thread.sleep(sleep); } } catch (Exception ex) { Log.logException(ex); } } }