Initial commit

This commit is contained in:
2020-10-13 14:19:16 +02:00
commit 23aee75962
44 changed files with 2142 additions and 0 deletions
+80
View File
@@ -0,0 +1,80 @@
package dslab;
import java.io.InputStream;
import java.io.PrintStream;
import dslab.mailbox.IMailboxServer;
import dslab.mailbox.MailboxServer;
import dslab.monitoring.IMonitoringServer;
import dslab.monitoring.MonitoringServer;
import dslab.transfer.ITransferServer;
import dslab.transfer.TransferServer;
import dslab.util.Config;
/**
* The component factory provides methods to create the core components of the application. You can edit the method body
* if the component instantiation requires additional logic.
*
* Do not change the existing method signatures!
*/
public final class ComponentFactory {
private ComponentFactory() {
// static utility class
}
/**
* Creates a new {@link IMonitoringServer} instance.
*
* @param componentId the component id
* @param in the input stream used for accepting cli commands
* @param out the output stream to print to
* @return a new MonitoringServer instance
*/
public static IMonitoringServer createMonitoringServer(String componentId, InputStream in, PrintStream out)
throws Exception {
/*
* TODO: Here you can modify the code (if necessary) to instantiate your components
*/
Config config = new Config(componentId);
return new MonitoringServer(componentId, config, in, out);
}
/**
* Creates a new {@link IMailboxServer} instance.
*
* @param componentId the component id
* @param in the input stream used for accepting cli commands
* @param out the output stream to print to
* @return a new MailboxServer instance
*/
public static IMailboxServer createMailboxServer(String componentId, InputStream in, PrintStream out)
throws Exception {
/*
* TODO: Here you can modify the code (if necessary) to instantiate your components
*/
Config config = new Config(componentId);
return new MailboxServer(componentId, config, in, out);
}
/**
* Creates a new {@link ITransferServer} instance.
*
* @param componentId the component id
* @param in the input stream used for accepting cli commands
* @param out the output stream to print to
* @return a new TransferServer instance
*/
public static ITransferServer createTransferServer(String componentId, InputStream in, PrintStream out)
throws Exception {
/*
* TODO: Here you can modify the code (if necessary) to instantiate your components
*/
Config config = new Config(componentId);
return new TransferServer(componentId, config, in, out);
}
}
@@ -0,0 +1,23 @@
package dslab.mailbox;
/**
* The mailbox server receives mails via DMTP from transfer servers, and makes them available to users via the DMAP
* protocol.
*
* Do not change the existing method signatures!
*/
public interface IMailboxServer extends Runnable {
/**
* Starts the server.
*/
@Override
void run();
/**
* CLI command to shut down the server. After this method, all resources should be closed, and the application
* should terminate.
*/
void shutdown();
}
@@ -0,0 +1,37 @@
package dslab.mailbox;
import java.io.InputStream;
import java.io.PrintStream;
import dslab.ComponentFactory;
import dslab.util.Config;
public class MailboxServer implements IMailboxServer, Runnable {
/**
* Creates a new server instance.
*
* @param componentId the id of the component that corresponds to the Config resource
* @param config the component config
* @param in the input stream to read console input from
* @param out the output stream to write console output to
*/
public MailboxServer(String componentId, Config config, InputStream in, PrintStream out) {
// TODO
}
@Override
public void run() {
// TODO
}
@Override
public void shutdown() {
// TODO
}
public static void main(String[] args) throws Exception {
IMailboxServer server = ComponentFactory.createMailboxServer(args[0], System.in, System.out);
server.run();
}
}
@@ -0,0 +1,33 @@
package dslab.monitoring;
/**
* The monitoring service accepts incoming monitoring packets via UDP. It provides CLI commands to access the
* information.
*
* Do not change the existing method signatures!
*/
public interface IMonitoringServer extends Runnable {
/**
* Starts the server.
*/
@Override
void run();
/**
* CLI command to shut down the server. After this method, all resources should be closed, and the application
* should terminate.
*/
void shutdown();
/**
* CLI command to report usage statistics for transfer servers.
*/
void servers();
/**
* CLI command to report usage statistics for individual senders.
*/
void addresses();
}
@@ -0,0 +1,48 @@
package dslab.monitoring;
import java.io.InputStream;
import java.io.PrintStream;
import dslab.ComponentFactory;
import dslab.util.Config;
public class MonitoringServer implements IMonitoringServer {
/**
* Creates a new server instance.
*
* @param componentId the id of the component that corresponds to the Config resource
* @param config the component config
* @param in the input stream to read console input from
* @param out the output stream to write console output to
*/
public MonitoringServer(String componentId, Config config, InputStream in, PrintStream out) {
// TODO
}
@Override
public void run() {
// TODO
}
@Override
public void addresses() {
// TODO
}
@Override
public void servers() {
// TODO
}
@Override
public void shutdown() {
// TODO
}
public static void main(String[] args) throws Exception {
IMonitoringServer server = ComponentFactory.createMonitoringServer(args[0], System.in, System.out);
server.run();
}
}
@@ -0,0 +1,22 @@
package dslab.transfer;
/**
* The transfer server is responsible for accepting mails sent by users, and forward them to mailbox servers via DMTP.
* It also reports usage statistics to the monitoring server.
*
* Do not change the existing method signatures!
*/
public interface ITransferServer extends Runnable {
/**
* Starts the server.
*/
@Override
void run();
/**
* CLI command to shut down the server. After this method, all resources should be closed, and the application
* should terminate.
*/
void shutdown();
}
@@ -0,0 +1,38 @@
package dslab.transfer;
import java.io.InputStream;
import java.io.PrintStream;
import dslab.ComponentFactory;
import dslab.util.Config;
public class TransferServer implements ITransferServer, Runnable {
/**
* Creates a new server instance.
*
* @param componentId the id of the component that corresponds to the Config resource
* @param config the component config
* @param in the input stream to read console input from
* @param out the output stream to write console output to
*/
public TransferServer(String componentId, Config config, InputStream in, PrintStream out) {
// TODO
}
@Override
public void run() {
// TODO
}
@Override
public void shutdown() {
// TODO
}
public static void main(String[] args) throws Exception {
ITransferServer server = ComponentFactory.createTransferServer(args[0], System.in, System.out);
server.run();
}
}
+79
View File
@@ -0,0 +1,79 @@
package dslab.util;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
/**
* Reads the configuration from a {@code .properties} file.
*/
public final class Config {
private final ResourceBundle bundle;
private Map<String, Object> properties = new HashMap<>();
/**
* Creates an instance of Config which reads configuration data form {@code .properties} file with given name found
* in classpath.
*
* @param name the name of the .properties file
*/
public Config(String name) {
if (name.endsWith(".properties")) {
this.bundle = ResourceBundle.getBundle(name.substring(0, name.length() - 11));
} else {
this.bundle = ResourceBundle.getBundle(name);
}
}
/**
* Returns the value as String for the given key.
*
* @param key the property's key
* @return String value of the property
* @see ResourceBundle#getString(String)
*/
public String getString(String key) {
if (properties.containsKey(key)) {
return properties.get(key).toString();
}
return this.bundle.getString(key);
}
/**
* Returns the value as {@code int} for the given key.
*
* @param key the property's key
* @return int value of the property
* @throws NumberFormatException if the String cannot be parsed to an Integer
*/
public int getInt(String key) {
return Integer.parseInt(getString(key));
}
public boolean containsKey(String key) {
return properties.containsKey(key) || bundle.containsKey(key);
}
/**
* Sets the value for the given key.
*
* @param key the property's key
* @param value the value of the property
*/
public void setProperty(String key, Object value) {
properties.put(key, value);
}
/**
* Returns all keys of this configuration.
*
* @return the keys
*/
public Set<String> listKeys() {
Set<String> keys = bundle.keySet();
keys.addAll(properties.keySet());
return keys;
}
}