package so.tio.inlearn.server;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import so.tio.inlearn.common.testing.TestItem;
import so.tio.inlearn.server.StarterUi.OnStartListener;
import so.tio.inlearn.server.session.Session.SessionCallback;
import so.tio.inlearn.server.session.SessionHandler;
import so.tio.inlearn.server.session.TestAnswersHandler;
import so.tio.inlearn.server.session.User;
import so.tio.inlearn.server.testing.TestsFacility;
public class Starter {
public static class config {
final public static int port = 2000; // TODO: temporary port, use
// another to avoid conflict.
final public static int bufferSize = 1 * 1024 * 1024;// Buffer
// size(bytes) for
// receiving
// packets
final public static String dataDir = "data"; // Directory for data
// files(tests, topics)
final public static long minutesToTestEnd = 60 * 4; // Test will be
// active N minutes
// after first
// testslist
// request.
};
private static ServerSocket socket;
public static void main(String[] args) {
init(TestsFacility.getTests());
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Server shuting down...");
try {
socket.close();
} catch (IOException | NullPointerException e) {
System.out.println("Listening socket is alerady closed");
}
}
}));
}
private static void serveTests(final long[] testIds, final long[] testsTime, final SessionCallback cb) {
new Thread(new Runnable() {
@Override
public void run() {
try {
socket = new ServerSocket(config.port);
System.out.println("Listening port " + config.port);
while (true) {
Socket clientSocket = socket.accept();
cb.onClientConnected();
System.out.println("New client connected");
SessionHandler worker = new SessionHandler(clientSocket, testIds, testsTime);
worker.setSessionCallback(cb);
new Thread(worker).start();
}
} catch (IOException e) {
// Nothing to do. Socket is dead
}
}
}).start();
}
private static void init(final List<TestItem> tests) {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, you can set the GUI to another look
// and feel.
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
final StarterUi starterGui = new StarterUi();
starterGui.setVisible(true);
starterGui.setTests(tests);
starterGui.setOnStartListener(new OnStartListener() {
@Override
public void onStart(long[] selectedTests, long[] testsTimeOut) {
// TODO Auto-generated method stub
System.out.println(selectedTests[0]);
starterGui.setVisible(false);
final ServingUi servingUi = new ServingUi();
servingUi.setVisible(true);
servingUi.startTimer(testsTimeOut[0]);
serveTests(selectedTests, testsTimeOut, new SessionCallback() {
@Override
public void onClientConnected() {
servingUi.increaseOnline(1);
};
@Override
public void onClientDisconnected() {
servingUi.increaseOnline(-1);
};
@Override
public void onClientStartTesting(long testId) {
servingUi.increaseInTesting(1);
};
@Override
public void onClientDoneTesting(User user, TestAnswersHandler handler) {
servingUi.increaseInTesting(-1);
if(user != null){
servingUi.increasePassed(1);
}
servingUi.userDoneTest(user, handler);
};
});
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}