Commit 712a0c14 authored by Spiros Koulouzis's avatar Spiros Koulouzis

Added example project

parent 065e01c8
......@@ -5,4 +5,5 @@
/drip-simple_planner/target/
/drip-provisioner/target/
/target/
/drip-planner2provisioner/target/
\ No newline at end of file
/drip-planner2provisioner/target/
/drip-component_example/target/
\ No newline at end of file
<?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;
/**
*
* @author S. Koulouzis
*/
public class Component {
}
/*
* 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.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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;
private final Component component;
public Consumer(Channel channel) {
super(channel);
this.channel = channel;
this.component = new Component();
}
public Consumer() {
super(null);
this.channel = null;
this.component = new Component();
}
@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");
//We need to extact the call parameters form the json message.
// inputFiles = jacksonUnmarshalExample(message);
//Call the method with the extracted parameters
// List<File> files = panner.plan(inputFiles[0].getAbsolutePath(), inputFiles[1].getAbsolutePath(), tempDir.getAbsolutePath());
//Here we do the same as above with a different API
// inputFiles = simpleJsonUnmarshalExample(message);
//Call the method with the extracted parameters
// files = panner.plan(inputFiles[0].getAbsolutePath(), inputFiles[1].getAbsolutePath(), tempDir.getAbsolutePath());
//Now we need to put the result of the call to a message and respond
//Example 1
// response = jacksonMarshalExample(files);
//Example 2
// response = simpleJsonMarshalExample(files);
} catch (Exception 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);
}
}
// private File[] jacksonUnmarshalExample(String message) throws IOException {
// //Use the Jackson API to convert json to Object
// File[] files = new File[2];
// ObjectMapper mapper = new ObjectMapper();
// Message request = mapper.readValue(message, Message.class);
//
// List<MessageParameter> params = request.getParameters();
//
// //Create tmp input files
// File inputFile = File.createTempFile("input-", Long.toString(System.nanoTime()));
// File exampleFile = File.createTempFile("example-", Long.toString(System.nanoTime()));
// //loop through the parameters in a message to find the input files
// for (MessageParameter param : params) {
// if (param.getName().equals("input")) {
// try (PrintWriter out = new PrintWriter(inputFile)) {
// out.print(param.getValue());
// }
// files[0] = inputFile;
// }
// if (param.getName().equals("example")) {
// try (PrintWriter out = new PrintWriter(exampleFile)) {
// out.print(param.getValue());
// }
// files[1] = exampleFile;
// }
// }
// //Return the array with input files
// return files;
// }
private File[] simpleJsonUnmarshalExample(String message) throws JSONException, FileNotFoundException, IOException {
//Use the JSONObject API to convert json to Object (Message)
File[] files = new File[2];
JSONObject jo = new JSONObject(message);
JSONArray parameters = jo.getJSONArray("parameters");
File inputFile = File.createTempFile("input-", Long.toString(System.nanoTime()));
File exampleFile = File.createTempFile("example-", Long.toString(System.nanoTime()));
for (int i = 0; i < parameters.length(); i++) {
JSONObject param = (JSONObject) parameters.get(i);
String name = (String) param.get("name");
if (name.equals("input")) {
try (PrintWriter out = new PrintWriter(inputFile)) {
out.print(param.get("value"));
}
files[0] = inputFile;
}
if (name.equals("example")) {
try (PrintWriter out = new PrintWriter(exampleFile)) {
out.print(param.get("value"));
}
files[1] = exampleFile;
}
}
return files;
}
// private String jacksonMarshalExample(List<File> files) throws UnsupportedEncodingException, IOException {
// //Use the jackson API to convert Object (Message) to json
// Message responseMessage = new Message();
// List parameters = new ArrayList();
// String charset = "UTF-8";
// for (File f : files) {
// MessageParameter fileParam = new MessageParameter();
// byte[] bytes = Files.readAllBytes(Paths.get(f.getAbsolutePath()));
// fileParam.setValue(new String(bytes, charset));
// fileParam.setEncoding(charset);
// fileParam.setName(f.getName());
// parameters.add(fileParam);
// }
// responseMessage.setParameters(parameters);
// //The creationDate is the only filed that has to be there
// responseMessage.setCreationDate((System.currentTimeMillis()));
//
// ObjectMapper mapper = new ObjectMapper();
// return mapper.writeValueAsString(responseMessage);
// }
private String simpleJsonMarshalExample(List<File> files) throws JSONException, IOException {
//Use the JSONObject API to convert Object (Message) to json
JSONObject jo = new JSONObject();
jo.put("creationDate", (System.currentTimeMillis()));
List parameters = new ArrayList();
String charset = "UTF-8";
for (File f : files) {
Map<String, String> fileArguments = new HashMap<>();
fileArguments.put("encoding", charset);
fileArguments.put("name", f.getName());
byte[] bytes = Files.readAllBytes(Paths.get(f.getAbsolutePath()));
fileArguments.put("value", new String(bytes, charset));
parameters.add(fileArguments);
}
jo.put("parameters", parameters);
return jo.toString();
}
public String handleDelivery(String message) {
return message;
}
}
/*
* 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;
/**
* 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.handleDelivery(new String(encoded, "UTF-8"));
Logger.getLogger(RPCServer.class.getName()).log(Level.INFO, MessageFormat.format("Response: {0}", response));
} catch (IOException 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) {
......
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