m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pl/edu/mimuw
diff options
context:
space:
mode:
authorMagdalena GrodziƄska <m.grodzinska@gmx.com>2019-11-20 16:26:46 +0100
committerGitHub <noreply@github.com>2019-11-20 16:26:46 +0100
commit79c8f56efcf76e7916597c0ef0e554d9fb91f8f4 (patch)
tree818bcec12c25853fc09e4e1f9f66592e58c6d6c6 /src/main/java/pl/edu/mimuw
parent562b0d451d9a58188a279844efe0efc548ac8f1f (diff)
parent5a44701aecd6531b3cb6403d359b6c8f6cd89a4e (diff)
Merge pull request #23 from m-chrzan/client_form_backend
Client form backend
Diffstat (limited to 'src/main/java/pl/edu/mimuw')
-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
4 files changed, 101 insertions, 37 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 (/).
*/