Parent: [r9] (diff)

Child: [r24] (diff)

Download this file

server.java    111 lines (92 with data), 3.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
/*
* Makes possible the web user interface. Allows users to control the program
* using a web browser.
*/
package webserver;
import definitions.Messages;
import definitions.definition;
import main.core;
import script.Plugin;
import script.log;
import utils.html;
import www.WebRequest;
import www.WebServer;
/**
*
* @author Nuno Brito, 9th of January 2013 in Darmstadt, Germany.
*/
public class server extends Plugin{
// title for this plugin
private final String id = "Web server";
@Override
public void startup(){
// add our node to the tree right after the "Tools" node is added
log.hooks.addAction(Messages.AddingTools, thisFile, "addNode");
startServer();
}
/**
* Add our toolbox node to the treeview.
*/
public void addNode(){
addTreeNode(id, "network-clouds.png", "main");
}
/** Start our instance */
public void startServer(){
String status = settings.read("status", "offline");
// if we are supposed to be offline, don't start the server
if(status.equals("offline")){
// we might have started before and need to now place it offline
WebServer server = (WebServer) core.temp.get("server");
if(server != null){
server.stopServer();
}
return;
}
// let's kickoff the server
WebServer server = new WebServer();
String portNumber = settings.read("port", definition.portDefault);
server.startServer(portNumber);
core.temp.put("server", server);
}
/**
* Save the settings to disk
* @param request
*/
public void save(WebRequest request){
// save the info from our template for future use
templateSave(request);
// Let's see if we need to start our server
startServer();
// all done
String message = html.h3("Settings saved!");
request.setAnswer(utils.html.redirect("/webserver/server", 2, message));
}
/**
* Opens up the web browser from the user on the front page of our software
* @param request the end-user request
*/
public void open(WebRequest request){
String status = settings.read("status", "offline");
// if we are supposed to be offline, we can't use this thing
if(status.equals("offline")){
String message = html.h3("You need to first activate the server..");
request.setAnswer(utils.html.redirect("/webserver/server", 2, message));
return;
}
// get the current port number
String portNumber = settings.read("port", definition.portDefault);
// All checked, let's open the front page
utils.internet.openURL("http://127.0.0.1:" + portNumber
+ "/basic/home");
String message = html.h3("Opening your web browser..");
request.setAnswer(utils.html.redirect("/webserver/server", 2, message));
}
/**
* Show the main page for the server settings
* @param request the request from the end-user
*/
@Override
public void main(WebRequest request){
templateLoad("serverSettings.html", request);
}
}