diff options
author | Magdalena GrodziĆska <m.grodzinska@gmx.com> | 2019-11-20 16:26:46 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-20 16:26:46 +0100 |
commit | 79c8f56efcf76e7916597c0ef0e554d9fb91f8f4 (patch) | |
tree | 818bcec12c25853fc09e4e1f9f66592e58c6d6c6 /src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java | |
parent | 562b0d451d9a58188a279844efe0efc548ac8f1f (diff) | |
parent | 5a44701aecd6531b3cb6403d359b6c8f6cd89a4e (diff) |
Merge pull request #23 from m-chrzan/client_form_backend
Client form backend
Diffstat (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java')
-rw-r--r-- | src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java | 82 |
1 files changed, 78 insertions, 4 deletions
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"; } |