diff options
Diffstat (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/client')
3 files changed, 99 insertions, 36 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 + '\'' + + '}'; + } +} |