Skip to content
This repository has been archived by the owner on Feb 17, 2019. It is now read-only.

Commit

Permalink
enhanced console
Browse files Browse the repository at this point in the history
added possibility to disable or enable socket upload and webserver
  • Loading branch information
sergix44 committed Sep 26, 2016
1 parent 17157a8 commit 2ca2d93
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 70 deletions.
125 changes: 86 additions & 39 deletions src/main/java/it/ksuploader/main/KSUploaderServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@


import it.ksuploader.main.sockets.SocketListener;
import it.ksuploader.main.web.UndertowServer;
import it.ksuploader.main.web.WebListener;
import it.ksuploader.utils.Configuration;
import org.apache.log4j.*;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
Expand All @@ -20,7 +19,7 @@ public class KSUploaderServer {
public static Logger logger = Logger.getLogger("KSULogger");

public static SocketListener socketListener;
public static UndertowServer webServer;
public static WebListener webServer;

public static void main(String[] args) {

Expand All @@ -42,37 +41,35 @@ public static void main(String[] args) {
}
System.out.println("--------------------------------------------------------------------------------");

// listen connections
try {
socketListener = new SocketListener();
socketListener.start();

webServer = new UndertowServer();
webServer.start();
} catch (IOException e) {
logger.log(Level.FATAL, "Error setting up the listener", e);
System.exit(2);
socketListener = new SocketListener();
webServer = new WebListener();

if (config.isSocketEnabled()) {
socketListener.startListen();
}

// open console
console();
if (config.isWebserverEnabled()) {
webServer.startListen();
}

System.exit(0);
// open consoleLoop
consoleLoop();
}

private static void bootstrap(String[] args) throws Exception {
config = new Configuration();
if (args.length != 0) {
if (args.length == 6) {
KSUploaderServer.config.setPort(args[0]);
KSUploaderServer.config.setPass(args[1]);
KSUploaderServer.config.setFolder(args[2]);
KSUploaderServer.config.setFolderSize(args[3]);
KSUploaderServer.config.setMaxFileSize(args[4]);
KSUploaderServer.config.setWeb_url(args[5]);
KSUploaderServer.config.setPort(args[2]);
KSUploaderServer.config.setPass(args[3]);
KSUploaderServer.config.setFolder(args[4]);
KSUploaderServer.config.setFolderSize(args[5]);
KSUploaderServer.config.setMaxFileSize(args[6]);
KSUploaderServer.config.setWeb_url(args[7]);
logger.log(Level.INFO, "Configuration created!");
} else {
throw new IllegalArgumentException("Correct args are: port, password, folder, folder size, max file size, web url");
throw new IllegalArgumentException("Correct args are: port, web_port, password, folder, folder size, max file size, web url");
}
} else {
if (
Expand All @@ -84,6 +81,7 @@ private static void bootstrap(String[] args) throws Exception {
} else {
System.out.println("Password: " + KSUploaderServer.config.getPass());
System.out.println("Port: " + KSUploaderServer.config.getPort());
System.out.println("Web Port: " + KSUploaderServer.config.getWebPort());
System.out.println("Folder: " + KSUploaderServer.config.getFolder());
System.out.println("Folder Max size: " + KSUploaderServer.config.getFolderSize());
System.out.println("File Max size: " + KSUploaderServer.config.getMaxFileSize());
Expand Down Expand Up @@ -111,38 +109,87 @@ public static void setupLogger() {
Logger.getRootLogger().addAppender(logFile);
}

private static void console() {
String cmd = "";
private static void consoleLoop() {
String[] cmd;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while (!cmd.equals("shutdown")) {
while (true) {
try {
cmd = in.readLine();
switch (cmd.split(" ")[0]) {
case "change-password":
if (config.setPass(cmd.split(" ")[1])) {
System.out.println("Password changed.");
} else {
System.out.println("Error.");
cmd = in.readLine().split(" ");
switch (cmd[0]) {
case "password":
switch (cmd[1]) {
case "get":
System.out.println(config.getPass());
break;
case "change":
if (cmd.length == 3 && config.setPass(cmd[2])) {
System.out.println("Password changed.");
} else {
System.out.println("Error. Usage: 'password change <pass>pas'");
}
break;
}
break;
case "socket":
switch (cmd[1]) {
case "stop":
if (socketListener.isListening()) {
socketListener.stopListen();
}
break;
case "start":
if (socketListener.isListening()) {
socketListener.stopListen();
}
socketListener = new SocketListener();
socketListener.startListen();
break;
case "status":
System.out.println("Socket listening: " + socketListener.isListening());
break;
}
break;
case "get-password":
System.out.println(config.getPass());
case "web":
switch (cmd[1]) {
case "start":
if (webServer.isListening()) {
webServer.stopListen();
}
webServer.startListen();
break;
case "stop":
if (webServer.isListening()) {
webServer.stopListen();
}
break;
case "status":
System.out.println("Webserver listening: " + webServer.isListening());
break;
}
break;
case "help":
case "?":
System.out.println("change-password <pass> - Change the server password");
System.out.println("get-password - Print current password");
System.out.println("password [get|change] - Print or change current password.");
System.out.println("socket [start|stop|status] - SocketListener operations.");
System.out.println("web [start|stop|status] - Webserver operations.");
System.out.println("shutdown - Stop the server");
break;
case "shutdown":
System.out.println("Shutting down...");
if (socketListener.isListening()) {
socketListener.stopListen();
}
if (webServer.isListening()) {
webServer.stopListen();
}
System.out.println("Bye!");
System.exit(0);
break;
default:
System.out.println("Invalid command.");
break;
}
} catch (IOException e) {
logger.log(Level.WARN, "Invalid command.");
} catch (Exception e) {
logger.log(Level.WARN, "Command error: Invalid.", e);
}
}
}
Expand Down
57 changes: 37 additions & 20 deletions src/main/java/it/ksuploader/main/sockets/SocketListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,64 @@
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.ServerSocketChannel;

public class SocketListener extends Thread {
private boolean listen;

private boolean listening;
private Logger logger = Logger.getLogger(this.getClass());
private ServerSocketChannel serverSocketChannel;

public SocketListener() throws IOException {

File uploadFolder = new File(KSUploaderServer.config.getFolder());

if (!uploadFolder.exists()) {
uploadFolder.mkdirs();
}
this.listen = true;
this.buildSocket();

public SocketListener() {
}

private void buildSocket() throws IOException {
try {
this.serverSocketChannel = ServerSocketChannel.open();
this.serverSocketChannel.socket().bind(new InetSocketAddress(KSUploaderServer.config.getPort()));

this.logger.log(Level.INFO, "Socket listening on port " + KSUploaderServer.config.getPort() + ".");

} catch (IOException exc) {
throw new IOException("Can't init the listening socket!");
}
}

public void setListen(boolean listen) {
this.listen = listen;

public void stopListen() {
try {
this.serverSocketChannel.close();
} catch (IOException e) {
this.logger.log(Level.WARN, "Cannot closing socket.", e);
}
this.interrupt();
this.listening = false;
}


public void startListen() {
try {
this.buildSocket();
} catch (IOException e) {
this.logger.log(Level.ERROR, "Cannot build socket.", e);
return;
}
this.start();
this.listening = true;
}

public boolean isListening() {
return this.listening;
}

public void run() {
while (this.listen) {
while (true) {
try {
new SocketRequestHandler(this.serverSocketChannel.accept()).start();
} catch (ClosedChannelException e) {
this.logger.log(Level.WARN, "Stopping listening...");
break;
} catch (IOException e) {
this.logger.log(Level.ERROR, "Error during handling request", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@
/**
* Created by Sergio on 25/09/2016.
*/
public class UndertowServer {
public class WebListener {

private Logger logger = Logger.getLogger(this.getClass());

private boolean listening;
private Undertow server;

public UndertowServer() {
public WebListener() {
try {
DeploymentInfo servletBuilder = Servlets.deployment()
.setClassLoader(UndertowServer.class.getClassLoader())
.setClassLoader(WebListener.class.getClassLoader())
.setDeploymentName("HttpRequestHandler")
.setContextPath("/")
.addServlets(
Expand Down Expand Up @@ -60,22 +61,27 @@ public UndertowServer() {
}
}

public void start() {
public void startListen() {
try {
this.server.start();
logger.log(Level.INFO, "Webserver listening on port " + config.getWebPort() + ".");
this.listening = true;
} catch (Exception e) {
logger.log(Level.ERROR, "Cannot start webserver.");
logger.log(Level.ERROR, "Cannot start webserver.", e);
}
}

public void stop() {
public void stopListen() {
try {
this.server.stop();
logger.log(Level.INFO, "Webserver listening on port " + config.getWebPort() + ".");
this.logger.log(Level.WARN, "Stopping listening...");
this.listening = false;
} catch (Exception e) {
logger.log(Level.ERROR, "Cannot start webserver.");
logger.log(Level.ERROR, "Cannot stop webserver.", e);
}
}

public boolean isListening() {
return this.listening;
}
}
32 changes: 29 additions & 3 deletions src/main/java/it/ksuploader/utils/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class Configuration extends Properties {
private long maxFileSize;
private int port;
private int web_port;
private boolean socketEnabled;
private boolean webserverEnabled;

public Configuration() {
InputStream inputStream;
Expand All @@ -40,7 +42,7 @@ public Configuration() {
}
this.folder = this.getProperty("folder");

// UndertowServer url
// web url
if (this.getProperty("web_url") == null || this.getProperty("web_url").isEmpty()) {
this.setProperty("web_url", "http://domain.com/");
correct_config = true;
Expand Down Expand Up @@ -87,7 +89,23 @@ public Configuration() {
logger.log(Level.INFO, "[Configuration] Setting default max_file_size(MB)");
}
this.maxFileSize = Long.parseLong(this.getProperty("max_file_size(MB)")) * 1048576;


// socket enabled
if (this.getProperty("socket_enabled") == null || this.getProperty("socket_enabled").isEmpty()) {
this.setProperty("socket_enabled", "true");
correct_config = true;
System.out.println("[LoadConfig] Setting default socket_enabled");
}
this.socketEnabled = Boolean.valueOf(this.getProperty("socket_enabled"));

// webserver enabled
if (this.getProperty("webserver_enabled") == null || this.getProperty("webserver_enabled").isEmpty()) {
this.setProperty("webserver_enabled", "true");
correct_config = true;
System.out.println("[LoadConfig] Setting default webserver_enabled");
}
this.webserverEnabled = Boolean.valueOf(this.getProperty("webserver_enabled"));

if (correct_config)
this.store(new FileOutputStream("server.properties"), null);

Expand Down Expand Up @@ -123,7 +141,15 @@ public long getFolderSize() {
public long getMaxFileSize() {
return maxFileSize;
}


public boolean isSocketEnabled() {
return socketEnabled;
}

public boolean isWebserverEnabled() {
return webserverEnabled;
}

private boolean save() {
try {
this.store(new FileOutputStream("server.properties"), null);
Expand Down

0 comments on commit 2ca2d93

Please sign in to comment.