Skip to content
This repository has been archived by the owner on Oct 29, 2021. It is now read-only.

Commit

Permalink
Add auto-restart functionality to server, untested. Passed as boolean…
Browse files Browse the repository at this point in the history
… arg
  • Loading branch information
TheaMorin committed May 21, 2014
1 parent ba963e2 commit 5183c21
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
3 changes: 1 addition & 2 deletions schedule-generator/ca/uottawa/ui/GenerateTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ private void generateSchedules(ScheduleMessage message, ConnectionToClient clien
server.printCurrentStats();
} catch (IOException e) {
e.printStackTrace();
server.display("Unable to report back generated schedules. Possible connection lost.");
server.display(client.toString());
server.display("Unable to report back generated schedules. Possible connection lost to " + client.getInfo("ip"));
}
}

Expand Down
27 changes: 26 additions & 1 deletion schedule-generator/ca/uottawa/ui/ScheduleGeneratorServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ public class ScheduleGeneratorServer extends AbstractServer {
private ServerStats runStats = null;
private File stats;
private int connections = 0;
private boolean autoRestart;


public ScheduleGeneratorServer(int port, ServerConsole serverUI) {
public ScheduleGeneratorServer(int port, ServerConsole serverUI, boolean autoRestart) {
super(port);
this.serverUI = serverUI;
this.autoRestart = autoRestart;
flCourses = new File("../course-download/db_courses.csv");
flSections = new File("../course-download/db_sections.csv");
flActivities = new File("../course-download/db_activities.csv");
Expand Down Expand Up @@ -345,6 +347,19 @@ protected void serverStarted()
protected void serverStopped()
{
serverUI.display("Server has stopped listening for connections.");
if (autoRestart) {
serverUI.display("Attempt to restart in 10 seconds.");
try {
Thread.sleep(10000);
listen();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

protected void serverClosed() {
Expand All @@ -366,4 +381,14 @@ synchronized protected void clientDisconnected(ConnectionToClient client) {
synchronized protected void clientException(ConnectionToClient client, Throwable exception) {
//serverUI.display(client.getInetAddress() + " has disconnected by exception.");
}


public boolean isAutoRestart() {
return autoRestart;
}


public void setAutoRestart(boolean autoRestart) {
this.autoRestart = autoRestart;
}
}
17 changes: 12 additions & 5 deletions schedule-generator/ca/uottawa/ui/ServerConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public class ServerConsole {
*k
* @param port The port to connect on.
*/
public ServerConsole(int port)
public ServerConsole(int port, boolean autoRestart)
{
server = new ScheduleGeneratorServer(port, this);
server = new ScheduleGeneratorServer(port, this, autoRestart);
try
{
server.listen(); //Start listening for connections
Expand All @@ -42,7 +42,6 @@ public ServerConsole(int port)
System.out.println("Quitting application");
System.exit(0);
}

}


Expand Down Expand Up @@ -97,17 +96,25 @@ public void display(String message)
public static void main(String[] args)
{
int port = 0; //Port to listen on

boolean autoRestart = true;
try
{
port = Integer.parseInt(args[0]); //Get port from command line
}
catch(Throwable t)
{
port = DEFAULT_PORT; //Set port to the default
}
try
{
autoRestart = Boolean.parseBoolean(args[1]); //Get port from command line
}
catch(Throwable t)
{
autoRestart = true;
}
//Create new server console.
ServerConsole server = new ServerConsole(port);
ServerConsole server = new ServerConsole(port, autoRestart);
//Start accepting connections on server console.
server.accept();
}
Expand Down

0 comments on commit 5183c21

Please sign in to comment.