m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/client/Client.java33
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java82
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/client/ContactsString.java20
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/model/PathName.java3
-rw-r--r--src/main/resources/pl/edu/mimuw/cloudatlas/client/templates/contactsForm.html4
5 files changed, 103 insertions, 39 deletions
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/client/Client.java b/src/main/java/pl/edu/mimuw/cloudatlas/client/Client.java
index 96fe14f..b63e4fd 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/client/Client.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/client/Client.java
@@ -1,42 +1,11 @@
package pl.edu.mimuw.cloudatlas.client;
-import pl.edu.mimuw.cloudatlas.api.Api;
-
-import java.rmi.registry.LocateRegistry;
-import java.rmi.RemoteException;
-import java.rmi.registry.Registry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
-/**
- * Main class for a client.
- *
- * Set the following properties on the command line:
- *
- * <br>
- * -Djava.rmi.server.useCodebaseOnly=false
- * <br>
- * -Djava.rmi.server.codebase=file:/path/to/compiled/classes/
- * <br>
- * -Djava.security.policy=client.policy
- * <br>
- *
- * <b>NOTE: MAKE SURE YOU HAVE THE TRAILING / ON THE CODEBASE PATH</b>
- */
-
-// https://github.com/rm5248/Java-RMI-Example/
-
@SpringBootApplication
public class Client {
public static void main(String[] args) {
- try {
- Registry registry = LocateRegistry.getRegistry("localhost");
- Api api = (Api) registry.lookup("Api");
-
- SpringApplication.run(Client.class, args);
- } catch (Exception e) {
- System.err.println("Client exception:");
- e.printStackTrace();
- }
+ SpringApplication.run(Client.class, args);
}
}
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java b/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java
index 62f77ee..31d1c06 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java
@@ -1,8 +1,21 @@
package pl.edu.mimuw.cloudatlas.client;
+import com.google.gson.Gson;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
+import pl.edu.mimuw.cloudatlas.api.Api;
+import pl.edu.mimuw.cloudatlas.model.PathName;
+import pl.edu.mimuw.cloudatlas.model.ValueContact;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.rmi.registry.LocateRegistry;
+import java.rmi.registry.Registry;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
/*
should enable reading attribute values stored by the agent
@@ -17,6 +30,17 @@ plotting the attributes with numeric values as real-time graphs.
@Controller
public class ClientController {
+ private Api api;
+
+ ClientController() {
+ try {
+ Registry registry = LocateRegistry.getRegistry("localhost");
+ this.api = (Api) registry.lookup("Api");
+ } catch (Exception e) {
+ System.err.println("Client exception:");
+ e.printStackTrace();
+ }
+ }
@GetMapping("/")
public String homePage(Model model) {
@@ -31,19 +55,69 @@ public class ClientController {
}
@PostMapping("/query")
- public String submitQuery(@ModelAttribute Query queryObject, Model model) {
- model.addAttribute("homeMessage", "Query submitted successfully");
+ public String submitQuery(@ModelAttribute Query queryObject, Model model) {
+ boolean success = true;
+
+ try {
+ this.api.installQuery(queryObject.getName(), queryObject.getValue());
+ } catch (Exception e) {
+ success = false;
+ System.err.println("Client exception:");
+ e.printStackTrace();
+ }
+
+ if (success) {
+ model.addAttribute(
+ "homeMessage",
+ "Query submitted successfully");
+ } else {
+ model.addAttribute(
+ "homeMessage",
+ "Query submission failed with a remote exception");
+ }
+
return "home";
}
@GetMapping("/contacts")
public String contactPage(Model model) {
+ model.addAttribute("contactsObject" , new ContactsString());
return "contactsForm";
}
@PostMapping("/contacts")
- public String contactPage(@ModelAttribute String contactsObject, Model model) {
- model.addAttribute("homeMessage", "Contact list submitted successfully");
+ public String contactPage(@ModelAttribute ContactsString contactsObject, Model model) {
+ boolean success = true;
+ Gson gson = new Gson();
+ Map<String, ArrayList> contactStrings = gson.fromJson(contactsObject.getString(), Map.class);
+ Set<ValueContact> contactObjects = new HashSet<ValueContact>();
+ ArrayList<Double> cAddr;
+ byte[] inetArray = new byte[4];
+
+ try {
+ for (Map.Entry<String, ArrayList> cursor : contactStrings.entrySet()) {
+ cAddr = cursor.getValue(); // gson always reads numerical values as doubles
+ for (int i = 0; i < 4; i++) {
+ inetArray[i] = (byte) cAddr.get(i).doubleValue();
+ }
+ contactObjects.add(new ValueContact(
+ new PathName(cursor.getKey()),
+ InetAddress.getByAddress(inetArray))
+ );
+ }
+ this.api.setFallbackContacts(contactObjects);
+ } catch (Exception e) {
+ success = false;
+ System.err.println("Client exception:");
+ e.printStackTrace();
+ }
+
+ if (success) {
+ model.addAttribute("homeMessage", "Contact list submitted successfully");
+ } else {
+ model.addAttribute("homeMessage", "Contact list submission failed");
+ }
+
return "home";
}
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/client/ContactsString.java b/src/main/java/pl/edu/mimuw/cloudatlas/client/ContactsString.java
new file mode 100644
index 0000000..7cdd82e
--- /dev/null
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/client/ContactsString.java
@@ -0,0 +1,20 @@
+package pl.edu.mimuw.cloudatlas.client;
+
+public class ContactsString {
+ private String string;
+
+ public String getString() {
+ return string;
+ }
+
+ public void setString(String string) {
+ this.string = string;
+ }
+
+ @Override
+ public String toString() {
+ return "ContactString{" +
+ "string='" + string + '\'' +
+ '}';
+ }
+}
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/PathName.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/PathName.java
index 7f3a9ac..e56b62a 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/model/PathName.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/PathName.java
@@ -24,6 +24,7 @@
package pl.edu.mimuw.cloudatlas.model;
+import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -33,7 +34,7 @@ import java.util.List;
/**
* Represent a fully qualified name of a zone, also known as a global name or a path name. Objects of this class are immutable.
*/
-public class PathName {
+public class PathName implements Serializable {
/**
* Object representing the name of the root zone (/).
*/
diff --git a/src/main/resources/pl/edu/mimuw/cloudatlas/client/templates/contactsForm.html b/src/main/resources/pl/edu/mimuw/cloudatlas/client/templates/contactsForm.html
index 027f831..40af9b4 100644
--- a/src/main/resources/pl/edu/mimuw/cloudatlas/client/templates/contactsForm.html
+++ b/src/main/resources/pl/edu/mimuw/cloudatlas/client/templates/contactsForm.html
@@ -13,8 +13,8 @@
<div id="queryForm">
<form action="#" th:action="@{/contacts}" th:object="${contactsObject}" method="post">
<div class="form-group">
- <label for="Textarea1">Enter contacts as a Json list</label>
- <textarea class="form-control" id="Textarea1" rows="3"></textarea>
+ <label for="Textarea1">Enter contacts as a Json map with string key and integer list as value</label>
+ <textarea class="form-control" id="Textarea1" rows="3" th:field="*{string}"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>