Commit 32f7b7c2 authored by skoulouzis's avatar skoulouzis Committed by GitHub

Merge pull request #43 from skoulouzis/DRIP_0.1

Drip 0.1
parents 03b53d27 7d846333
......@@ -5,4 +5,6 @@
/drip-simple_planner/target/
/drip-provisioner/target/
/target/
/drip-planner2provisioner/target/
\ No newline at end of file
/drip-planner2provisioner/target/
/drip-component_example/target/
/deleteme/target/
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>nl.uva.sne.drip</groupId>
<artifactId>drip</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>deleteme</artifactId>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
\ No newline at end of file
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package nl.uva.sne.drip.deleteme;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Base64;
/**
*
* @author alogo
*/
public class Main {
public static void main(String[] args) throws IOException {
byte[] bytes = Files.readAllBytes(Paths.get("/home/alogo/workspace/DRIP/docs/images/DRIP_arch.png"));
byte[] encoded = Base64.getEncoder().encode(bytes);
String cont = new String(encoded, "US-ASCII");
System.out.println(cont);
byte[] decoded = Base64.getDecoder().decode(cont);
OpenOption[] options = new OpenOption[1];
options[0] = StandardOpenOption.CREATE_NEW;
Files.write(Paths.get("/home/alogo/Downloads/DRIP_arch.png"), decoded, options);
}
}
This diff is collapsed.
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<!--
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<netbeans.hint.licensePath>${project.basedir}/../licenseheader.txt</netbeans.hint.licensePath>
</properties>
</project-shared-configuration>
<?xml version="1.0" encoding="UTF-8"?>
<actions>
<action>
<actionName>run</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>process-classes</goal>
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
</goals>
<properties>
<exec.args>-classpath %classpath nl.uva.sne.drip.drip.component_example.RPCServer test /home/alogo/workspace/DRIP/docs/json_samples/messageExample.json</exec.args>
<exec.executable>java</exec.executable>
</properties>
</action>
<action>
<actionName>debug</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>process-classes</goal>
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
</goals>
<properties>
<exec.args>-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -classpath %classpath nl.uva.sne.drip.drip.component_example.RPCServer test /home/alogo/workspace/DRIP/docs/json_samples/messageExample.json</exec.args>
<exec.executable>java</exec.executable>
<jpda.listen>true</jpda.listen>
</properties>
</action>
<action>
<actionName>profile</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>process-classes</goal>
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
</goals>
<properties>
<exec.args>-classpath %classpath nl.uva.sne.drip.drip.component_example.RPCServer test /home/alogo/workspace/DRIP/docs/json_samples/messageExample.json</exec.args>
<exec.executable>java</exec.executable>
</properties>
</action>
</actions>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>nl.uva.sne.drip</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>drip-component_example</artifactId>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>4.0.2</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
<type>jar</type>
</dependency>
</dependencies>
</project>
\ No newline at end of file
/*
* Copyright 2017 S. Koulouzis, Wang Junchao, Huan Zhou, Yang Hu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.uva.sne.drip.drip.component_example;
import java.io.File;
/**
* This is an example components. It is a dumy components to demonstrate a
* simple application logic
*
* @author S. Koulouziss
*/
public class Component {
private final int input;
private final File inputTextFile;
private final File inputBinFile;
private final ExamplePOJO book;
public Component(int input, File inputTextFile, File inputBinFile, ExamplePOJO book) {
this.input = input;
this.inputTextFile = inputTextFile;
this.inputBinFile = inputBinFile;
this.book = book;
}
String run() throws Exception {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
/*
* Copyright 2017 S. Koulouzis, Wang Junchao, Huan Zhou, Yang Hu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.uva.sne.drip.drip.component_example;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Base64;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
*
* This is an example of a Message consumer
*
*
* @author S. Koulouzis
*/
public class Consumer extends DefaultConsumer {
private final Channel channel;
public Consumer(Channel channel) {
super(channel);
this.channel = channel;
}
public Consumer() {
super(null);
this.channel = null;
}
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//Create the reply properties which tells us where to reply, and which id to use.
//No need to change anything here
AMQP.BasicProperties replyProps = new AMQP.BasicProperties.Builder()
.correlationId(properties.getCorrelationId())
.build();
String response = "";
try {
//The queue only moves bytes so we need to convert them to stting
String message = new String(body, "UTF-8");
response = invokeComponent(message);
} catch (JSONException | IOException ex) {
response = ex.getMessage();
Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
} finally {
//We send the response back. No need to change anything here
channel.basicPublish("", properties.getReplyTo(), replyProps, response.getBytes("UTF-8"));
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
/**
* This method is extracts the parammeters from incoming JSON message.
*
* @param message
* @return
* @throws JSONException
* @throws IOException
*/
public String invokeComponent(String message) throws JSONException, IOException {
JSONObject messageJson = new JSONObject(message);
Date creationDate = getMessageCreationDate(messageJson);
Logger.getLogger(Consumer.class.getName()).log(Level.INFO, "Message was created at: {0}", creationDate);
JSONArray parameters = messageJson.getJSONArray("parameters");
int input = getInputInteger(parameters);
Logger.getLogger(Consumer.class.getName()).log(Level.INFO, "Input parameter is: {0}", input);
File inputTextFile = getInputFile(parameters, System.getProperty("java.io.tmpdir") + File.separator + "delete-me.txt");
Logger.getLogger(Consumer.class.getName()).log(Level.INFO, "Input file is at: {0}. With size: {1}", new Object[]{inputTextFile.getAbsolutePath(), inputTextFile.length()});
File inputBinFile = getInputImageFile(parameters, System.getProperty("java.io.tmpdir"));
Logger.getLogger(Consumer.class.getName()).log(Level.INFO, "Input image file is at: {0}. With size: {1}", new Object[]{inputBinFile.getAbsolutePath(), inputBinFile.length()});
ExamplePOJO book = getExamplePOJO(parameters);
Integer wordcount = book.getContent().trim().split("\\s+").length;
Logger.getLogger(Consumer.class.getName()).log(Level.INFO, "Created book object. Author: {0}. Langunage: {1}. Number of words: {2}", new Object[]{book.getAuthor(), book.getLanguage(), wordcount});
Component component = new Component(input, inputTextFile, inputBinFile, book);
String response;
try {
response = component.run();
} catch (Exception ex) {
response = ex.getMessage();
Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
}
return response;
}
private Date getMessageCreationDate(JSONObject messageJson) throws JSONException {
return new Date(messageJson.getLong("creationDate"));
}
private Integer getInputInteger(JSONArray parameters) throws JSONException {
for (int i = 0; i < parameters.length(); i++) {
JSONObject param = (JSONObject) parameters.get(i);
if (param.has("name") && param.getString("name").equals("input")) {
return param.getInt("value");
}
}
return null;
}
private File getInputFile(JSONArray parameters, String filePath) throws JSONException, IOException {
for (int i = 0; i < parameters.length(); i++) {
JSONObject param = (JSONObject) parameters.get(i);
if (param.has("name") && param.getString("name").equals("input-file")) {
OpenOption[] options = new OpenOption[1];
options[0] = StandardOpenOption.CREATE_NEW;
String value = param.getString("value");
String encoding = param.getString("encoding");
File file = new File(filePath);
file.delete();
byte[] bytes = null;
if (encoding.equals("UTF-8")) {
bytes = value.getBytes();
}
if (encoding.equals("Base64")) {
bytes = Base64.getDecoder().decode(value);
}
Files.write(Paths.get(file.getAbsolutePath()), bytes, options);
return file;
}
}
return null;
}
private File getInputImageFile(JSONArray parameters, String filePath) throws IOException, JSONException {
for (int i = 0; i < parameters.length(); i++) {
JSONObject param = (JSONObject) parameters.get(i);
if (param.has("name") && param.getString("name").equals("input_image")) {
OpenOption[] options = new OpenOption[1];
options[0] = StandardOpenOption.CREATE_NEW;
String value = param.getString("value");
String encoding = param.getString("encoding");
JSONObject attributes = param.getJSONObject("attributes");
String fileName = attributes.getString("filename");
File file = new File(filePath + File.separator + fileName);
file.delete();
byte[] bytes = null;
if (encoding.equals("UTF-8")) {
bytes = value.getBytes();
}
if (encoding.equals("Base64")) {
bytes = Base64.getDecoder().decode(value);
}
Files.write(Paths.get(file.getAbsolutePath()), bytes, options);
return file;
}
}
return null;
}
private ExamplePOJO getExamplePOJO(JSONArray parameters) throws JSONException, MalformedURLException, IOException {
for (int i = 0; i < parameters.length(); i++) {
JSONObject param = (JSONObject) parameters.get(i);
if (param.has("name") && param.getString("name").equals("text")) {
URL url = new URL(param.getString("url"));
String content = getContent(url);
JSONObject attributes = param.getJSONObject("attributes");
String author = attributes.getString("Author");
String translator = attributes.getString("Translator");
String language = attributes.getString("Language");
ExamplePOJO book = new ExamplePOJO(author, content, translator, language);
return book;
}
}
return null;
}
private String getContent(URL url) throws IOException {
URLConnection conn = url.openConnection();
StringBuilder cont = null;
try (BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream()))) {
String inputLine;
cont = new StringBuilder();
while ((inputLine = br.readLine()) != null) {
cont.append(inputLine);
}
}
return cont.toString();
}
}
/*
* Copyright 2017 S. Koulouzis, Wang Junchao, Huan Zhou, Yang Hu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.uva.sne.drip.drip.component_example;
/**
*
* @author S. Koulouzis
*/
public class ExamplePOJO {
private final String author;
private final String content;
private final String translator;
private final String language;
public ExamplePOJO(String author, String content, String translator, String language) {
this.author = author;
this.content = content;
this.translator = translator;
this.language = language;
}
/**
* @return the author
*/
public String getAuthor() {
return author;
}
/**
* @return the content
*/
public String getContent() {
return content;
}
/**
* @return the translator
*/
public String getTranslator() {
return translator;
}
/**
* @return the language
*/
public String getLanguage() {
return language;
}
}
/*
* Copyright 2017 S. Koulouzis, Wang Junchao, Huan Zhou, Yang Hu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.uva.sne.drip.drip.component_example;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.MessageFormat;
import java.util.Properties;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;
/**
* This class is responsible for receiving and sending message to the DRIP
* manager via RabbitMQ.
*
* @author S. Koulouzis
*/
public class RPCServer {
/**
* The name of the queue to send and receive massages
*/
private static final String RPC_QUEUE_NAME = "planner_queue";
/**
* The IP or host name of the RabbitMQ server
*/
private static String HOST = "127.0.0.1";
public static void main(String[] args) {
if (args.length > 1 && args[0].equals("test")) {
try {
Consumer c = new Consumer();
byte[] encoded = Files.readAllBytes(Paths.get(args[1]));
String response = c.invokeComponent(new String(encoded, "UTF-8"));
Logger.getLogger(RPCServer.class.getName()).log(Level.INFO, MessageFormat.format("Response: {0}", response));
} catch (IOException | JSONException ex) {
Logger.getLogger(RPCServer.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
Properties prop = new Properties();
if (args.length >= 1 && !args[0].equals("test")) {
try {
prop.load(new FileInputStream(args[0]));
} catch (IOException ex) {
Logger.getLogger(RPCServer.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
String resourceName = "provisioner.properies";
ClassLoader loader = Thread.currentThread().getContextClassLoader();
try (InputStream resourceStream = loader.getResourceAsStream(resourceName)) {
prop.load(resourceStream);
} catch (IOException ex) {
Logger.getLogger(RPCServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
HOST = prop.getProperty("rabbitmq.host", "127.0.0.1");
Logger.getLogger(RPCServer.class.getName()).log(Level.INFO, MessageFormat.format("rabbitmq.host: {0}", HOST));
start();
}
}
private static void start() {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(HOST);
factory.setPassword("guest");
factory.setUsername("guest");
factory.setPort(AMQP.PROTOCOL.PORT);
try (Connection connection = factory.newConnection()) {
Channel channel = connection.createChannel();
//We define the queue name
channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null);
//Set our own customized consummer
Consumer c = new Consumer(channel);
//Start listening for messages
channel.basicConsume(RPC_QUEUE_NAME, false, c);
//Block so we don't close the channel
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException _ignore) {
}
}
} catch (IOException | TimeoutException ex) {
Logger.getLogger(RPCServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>nl.uva.sne.drip</groupId>
<artifactId>drip</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>nl.uva.sne.drip</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>drip-provisioner</artifactId>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
......
<?xml version="1.0" encoding="UTF-8"?>
<actions>
<action>
<actionName>run</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>process-classes</goal>
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
</goals>
<properties>
<exec.args>-classpath %classpath nl.uva.sne.drip.drip.planner.RPCServer test /home/alogo/workspace/DRIP/docs/json_samples/messageExample.json</exec.args>
<exec.executable>java</exec.executable>
</properties>
</action>
<action>
<actionName>debug</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>process-classes</goal>
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
</goals>
<properties>
<exec.args>-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -classpath %classpath nl.uva.sne.drip.drip.planner.RPCServer test /home/alogo/workspace/DRIP/docs/json_samples/messageExample.json</exec.args>
<exec.executable>java</exec.executable>
<jpda.listen>true</jpda.listen>
</properties>
</action>
<action>
<actionName>profile</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>process-classes</goal>
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
</goals>
<properties>
<exec.args>-classpath %classpath nl.uva.sne.drip.drip.planner.RPCServer test /home/alogo/workspace/DRIP/docs/json_samples/messageExample.json</exec.args>
<exec.executable>java</exec.executable>
</properties>
</action>
</actions>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>nl.uva.sne.drip</groupId>
<artifactId>drip</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>nl.uva.sne.drip</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>drip-simple_planner</artifactId>
<packaging>jar</packaging>
<properties>
......
......@@ -25,12 +25,20 @@ import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
*This class is responsible for receiving and sending message to the DRIP manager
* via RabbitMQ.
*
* @author S. Koulouzis
*/
public class RPCServer {
/**
*The name of the queue to send and receive massages
*/
private static final String RPC_QUEUE_NAME = "planner_queue";
/**
* The IP or host name of the RabbitMQ server
*/
private static final String HOST = "172.17.0.3";
public static void main(String[] argv) {
......
......@@ -12,5 +12,6 @@
<modules>
<module>drip-api</module>
<module>drip-planner2provisioner</module>
<module>deleteme</module>
</modules>
</project>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment