From 47ab1236e7cf82dd8e28cab711e0f59c546190e0 Mon Sep 17 00:00:00 2001 From: Gregor Riegler Date: Thu, 13 Jun 2013 11:23:47 +0200 Subject: [PATCH] [XVSM] LoadBalancer skeleton --- .../valesriegler/xvsm/LoadBalancerXVSM.java | 54 +++++++++++++++++++ .../xvsm/loadbalancer/LoadBalancer.java | 36 +++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/LoadBalancerXVSM.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/loadbalancer/LoadBalancer.java diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/LoadBalancerXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/LoadBalancerXVSM.java new file mode 100644 index 0000000..ff66065 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/LoadBalancerXVSM.java @@ -0,0 +1,54 @@ +package at.ac.tuwien.sbc.valesriegler.xvsm; + + +import at.ac.tuwien.sbc.valesriegler.common.Util; +import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceAction; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.Serializable; +import java.util.List; +import java.util.Set; + +public class LoadBalancerXVSM extends AbstractXVSMConnector { + private static final Logger log = LoggerFactory.getLogger(LoadBalancerXVSM.class); + + private int loadBalancerId; + private Set pizzeriaIdentifiers; + + public LoadBalancerXVSM(int id, int port) { + super(port); + + this.loadBalancerId = id; + + this.groupAgentInfoContainer = useContainerOfSpaceWithPort(Util.GROUP_AGENT_INFO, Util.GROUP_AGENT_PORT); + } + + public void listenForPizzerias() { + getDefaultBuilder("listenForPizzerias").setLookaround(true).setCref(groupAgentInfoContainer).setSpaceAction(new SpaceAction() { + @Override + public void onEntriesWritten(List entries) throws Exception { + + final List pizzeriaRegistrations = castEntries(entries); + for (PizzeriaRegistration registration : pizzeriaRegistrations) { + final int pizzeriaId = registration.pizzeriaSpacePort; + if (! pizzeriaIdentifiers.contains(pizzeriaId)) { + pizzeriaIdentifiers.add(pizzeriaId); + listenToPizzeria(pizzeriaId); + } + } + + } + }).createSpaceListenerImpl(); + } + + private void listenToPizzeria(int pizzeriaId) { + getDefaultBuilder("listenForPizzeriaDeliveries").setLookaround(true).setCref(useContainerOfSpaceWithPort(Util.DELIVERY_ORDER_TAKEN, pizzeriaId)).setSpaceAction(new SpaceAction() { + @Override + public void onEntriesWritten(List entries) throws Exception { + // TODO: do something when pizzeria gets new deliveries + } + }).createSpaceListenerImpl(); + } + +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/loadbalancer/LoadBalancer.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/loadbalancer/LoadBalancer.java new file mode 100644 index 0000000..dfc29ca --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/loadbalancer/LoadBalancer.java @@ -0,0 +1,36 @@ +package at.ac.tuwien.sbc.valesriegler.xvsm.loadbalancer; + + +import at.ac.tuwien.sbc.valesriegler.common.Tuple; +import at.ac.tuwien.sbc.valesriegler.common.Util; +import at.ac.tuwien.sbc.valesriegler.xvsm.LoadBalancerXVSM; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class LoadBalancer { + private static final String USAGE = "LoadBalancer needs exactly two integer parameters: LOADBALANCERID, LOADBALANCER-SPACE-PORT"; + private static final Logger log = LoggerFactory.getLogger(LoadBalancer.class); + + private final int pizzeriaPort; + private final int id; + private LoadBalancerXVSM xvsm; + + public static void main(String[] args) { + final Tuple loadBalancerIdAndSpacePort = Util.parseIdAndSpacePort(args, USAGE); + + LoadBalancer loadBalancer = new LoadBalancer(loadBalancerIdAndSpacePort.fst, loadBalancerIdAndSpacePort.snd); + loadBalancer.start(); + } + + private void start() { + xvsm = new LoadBalancerXVSM(id, pizzeriaPort); + + xvsm.listenForPizzerias(); + } + + public LoadBalancer(int id, int pizzeriaPort) { + this.id = id; + this.pizzeriaPort = pizzeriaPort; + log.info("I AM A Loadbalancer WITH ID {}", id); + } +} -- 2.43.0