Commit cd1b81dc authored by aninhacostaribeiro's avatar aninhacostaribeiro
Browse files

Implemented part of the Agent Communication Component

parent 18e808f1
xmpp.jid = agent@example.com
xmpp.password = tellNoOne
xmpp.host = localhost
xmpp.port = 1023
xmpp.component = server.test.com
host.ip = 123.456.789
host.macAddress = A1:B2:C3:D4:E5:67
host.name = agent
\ No newline at end of file
......@@ -2,7 +2,7 @@ green.cloudtechnology = openstack
xmpp.jid = green-sitter.example.com
xmpp.password = tellNoOne
xmpp.host = 123.456.789
xmpp.host = localhost
xmpp.port = 1023
openstack.endpoint = http://novacompute.endpoint.com/v2.0
......
package org.fogbowcloud.green.agent.core;
import java.util.Properties;
import org.dom4j.tree.DefaultElement;
import org.jamppa.client.XMPPClient;
import org.jamppa.client.plugin.xep0077.XEP0077;
import org.jivesoftware.smack.XMPPException;
import org.xmpp.packet.IQ;
import org.xmpp.packet.IQ.Type;
public class AgentCommunicationComponent {
Properties prop;
XMPPClient client;
public AgentCommunicationComponent(Properties prop) {
this.prop = prop;
client = new XMPPClient(this.prop.getProperty("xmpp.jid"),
this.prop.getProperty("xmpp.password"),
this.prop.getProperty("xmpp.host"),
Integer.parseInt(this.prop.getProperty("xmpp.port")));
}
public void sendIamAliveSignal(){
try {
XEP0077 register = new XEP0077();
this.client.registerPlugin(register);
client.connect();
register.createAccount(this.prop.getProperty("xmpp.jid"),
this.prop.getProperty("xmpp.password"));
client.login();
client.process(false);
IQ iq = new IQ(Type.get);
iq.setTo(this.prop.getProperty("xmpp.component"));
iq.getElement().addElement("query", "org.fogbowcloud.green.IAmAlive");
DefaultElement query = (DefaultElement) iq.getElement().elements("query").get(0);
query.addElement("ip").setText(this.prop.getProperty("host.ip"));
query.addElement("macAddress").setText(this.prop.getProperty("host.macAddress"));
query.addElement("hostName").setText(this.prop.getProperty("host.name"));
} catch (XMPPException e) {
System.out.println("It was not possible to send 'I am alive'"
+ " signal to the server, try again later");
}
}
}
package org.fogbowcloud.green.agent.core;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class Main {
public static Properties getProp(String path) throws IOException {
Properties props = new Properties();
FileInputStream file = new FileInputStream(path);
props.load(file);
return props;
}
public static void main(String[] args) {
try {
Properties prop = getProp(args[0]);
AgentCommunicationComponent acc = new AgentCommunicationComponent(prop);
acc.sendIamAliveSignal();
} catch (Exception e) {
System.err.println("You must provide as parameter the"
+ " path for the configuration file");
}
}
}
......@@ -11,6 +11,7 @@ public class TurnOff {
|| "Mac OS X".equals(operatingSystem)) {
hibernateCommand = "pm-hibernate";
}
//Suspend or hibernate?
ProcessBuilder pb =
new ProcessBuilder("sudo","-S", hibernateCommand);
try {
......
package org.fogbowcloud.green.agent.core;
import java.io.IOException;
public class testeMain {
public static void main(String[] args) throws RuntimeException, IOException {
}
}
......@@ -6,7 +6,7 @@ import org.xmpp.packet.IQ;
public class IAmAliveHandler extends AbstractQueryHandler {
private static final String NAMESPACE = "org.fogbowcloud.green.IAmAlive";
private static final String NAMESPACE = "org.fogbowcloud.green.IAmAlive"; //Is this name actually good?
private final GreenStrategy gs;
public IAmAliveHandler(GreenStrategy gs) {
......
......@@ -8,36 +8,35 @@ import org.fogbowcloud.green.server.communication.Agent;
import org.fogbowcloud.green.server.core.greenStrategy.GreenStrategy;
import org.jamppa.component.XMPPComponent;
public class GreenSitterCommunicationComponent extends XMPPComponent {
public class ServerCommunicationComponent extends XMPPComponent {
private GreenStrategy gs;
private LinkedList<Agent> listAgent = new LinkedList<Agent>();
private LinkedList<Agent> listAgent = new LinkedList<Agent>();
public GreenSitterCommunicationComponent(Properties prop, GreenStrategy gs) {
super(prop.getProperty("xmpp.jid"), prop.getProperty("xmpp.password"),
prop.getProperty("xmpp.host"), Integer.parseInt(prop.getProperty("xmpp.port")));
public ServerCommunicationComponent(Properties prop, GreenStrategy gs) {
super(prop.getProperty("xmpp.jid"), prop.getProperty("xmpp.password"),
prop.getProperty("xmpp.host"), Integer.parseInt(prop
.getProperty("xmpp.port")));
this.gs = gs;
addHandlers();
}
public void setAgentAddress(String hostName, String jid, String ip, String macAddress) {
public void setAgentAddress(String hostName, String jid, String ip,
String macAddress) {
Agent agent = new Agent(hostName, jid, ip, macAddress);
listAgent.add(agent);
}
public void wakeUpHost(String macAddress){
ProcessBuilder pb =
new ProcessBuilder("powerwake", macAddress);
try {
public void wakeUpHost(String macAddress) {
try {
ProcessBuilder pb = new ProcessBuilder("powerwake", macAddress);
pb.start();
} catch (IOException e) {
System.out.println("It was not possible to wake "+macAddress);
System.out.println("It was not possible to wake " + macAddress);
}
}
public void sendIdleHostToBed(String host){
public void sendIdleHostToBed(String host) {
}
private void addHandlers() {
......
......@@ -4,10 +4,9 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import org.fogbowcloud.green.server.communication.GreenSitterCommunicationComponent;
import org.fogbowcloud.green.server.communication.ServerCommunicationComponent;
import org.fogbowcloud.green.server.core.greenStrategy.DefaultGreenStrategy;
public class Main {
public static Properties getProp(String path) throws IOException {
......@@ -21,10 +20,11 @@ public class Main {
try {
Properties prop = getProp(args[0]);
DefaultGreenStrategy gs = new DefaultGreenStrategy(prop);
GreenSitterCommunicationComponent gscc = new GreenSitterCommunicationComponent(prop, gs);
gs.setCommunicationComponent(gscc);
ServerCommunicationComponent scc = new ServerCommunicationComponent(
prop, gs);
gs.setCommunicationComponent(scc);
gs.start();
gscc.process(true);
scc.process(true);
} catch (Exception e) {
System.err.println("You must provide as parameter the"
+ " path for the configuration file");
......
......@@ -9,7 +9,7 @@ import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.fogbowcloud.green.server.communication.GreenSitterCommunicationComponent;
import org.fogbowcloud.green.server.communication.ServerCommunicationComponent;
import org.fogbowcloud.green.server.core.plugins.CloudInfoPlugin;
import org.fogbowcloud.green.server.core.plugins.openstack.OpenStackInfoPlugin;
......@@ -19,7 +19,7 @@ public class DefaultGreenStrategy implements GreenStrategy {
private List<? extends Host> allHosts;
private List<Host> nappingHosts = new LinkedList<Host>();
private List<Host> sleepingHosts = new LinkedList<Host>();
private GreenSitterCommunicationComponent gscc;
private ServerCommunicationComponent scc;
private Date lastUpdatedTime;
......@@ -79,7 +79,7 @@ public class DefaultGreenStrategy implements GreenStrategy {
* napping than put it in sleeping host list
*/
if (nowTime - host.getUpdateTime() > this.graceTime) {
gscc.sendIdleHostToBed(host.getName());
scc.sendIdleHostToBed(host.getName());
this.getSleepingHosts().add(host);
this.getNappingHosts().remove(host);
}
......@@ -90,12 +90,12 @@ public class DefaultGreenStrategy implements GreenStrategy {
}
public void setCommunicationComponent(GreenSitterCommunicationComponent gscc) {
this.gscc = gscc;
public void setCommunicationComponent(ServerCommunicationComponent gscc) {
this.scc = gscc;
}
public void setAgentAddress(String hostName, String jid, String ip, String macAddress) {
gscc.setAgentAddress(hostName, jid, ip, macAddress);
this.scc.setAgentAddress(hostName, jid, ip, macAddress);
}
public void wakeUpSleepingHost(int minCPU, int minRAM) {
......@@ -103,7 +103,7 @@ public class DefaultGreenStrategy implements GreenStrategy {
for (Host host : this.getSleepingHosts()) {
if (host.getAvailableCPU() >= minCPU) {
if (host.getAvailableRAM() >= minRAM) {
gscc.wakeUpHost(host.getName());
this.scc.wakeUpHost(host.getName());
this.sleepingHosts.remove(host);
return;
}
......
package org.fogbowcloud.green.server.core.greenStrategy;
import org.fogbowcloud.green.server.communication.GreenSitterCommunicationComponent;
import org.fogbowcloud.green.server.communication.ServerCommunicationComponent;
public interface GreenStrategy {
......@@ -10,6 +10,6 @@ public interface GreenStrategy {
public void setAgentAddress(String Name, String JID, String IP, String macAddress);
public void setCommunicationComponent(GreenSitterCommunicationComponent gscc);
public void setCommunicationComponent(ServerCommunicationComponent gscc);
}
......@@ -4,7 +4,7 @@ import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import org.fogbowcloud.green.server.communication.GreenSitterCommunicationComponent;
import org.fogbowcloud.green.server.communication.ServerCommunicationComponent;
import org.fogbowcloud.green.server.core.greenStrategy.DefaultGreenStrategy;
import org.fogbowcloud.green.server.core.greenStrategy.Host;
import org.fogbowcloud.green.server.core.plugins.openstack.OpenStackInfoPlugin;
......@@ -44,7 +44,7 @@ public class TestDefaultGreenStrategy {
hosts.add(h1);
Date date = this.createDateMock(3600001);
GreenSitterCommunicationComponent gscc = Mockito.mock(GreenSitterCommunicationComponent.class);
ServerCommunicationComponent gscc = Mockito.mock(ServerCommunicationComponent.class);
Mockito.doNothing().when(gscc).wakeUpHost("host1");
OpenStackInfoPlugin osip = this.createOpenStackInfoPluginMock(hosts);
DefaultGreenStrategy dgs = new DefaultGreenStrategy(osip, 1800000);
......@@ -83,7 +83,7 @@ public class TestDefaultGreenStrategy {
Date date = this.createDateMock(3600001);
OpenStackInfoPlugin osip = this.createOpenStackInfoPluginMock(hosts);
DefaultGreenStrategy dgs = new DefaultGreenStrategy(osip, 1800000);
GreenSitterCommunicationComponent gscc = Mockito.mock(GreenSitterCommunicationComponent.class);
ServerCommunicationComponent gscc = Mockito.mock(ServerCommunicationComponent.class);
Mockito.doNothing().when(gscc).wakeUpHost("wake");
dgs.setCommunicationComponent(gscc);
......@@ -122,7 +122,7 @@ public class TestDefaultGreenStrategy {
hosts.add(mustWake2);
Date date = this.createDateMock(3600001);
GreenSitterCommunicationComponent gscc = Mockito.mock(GreenSitterCommunicationComponent.class);
ServerCommunicationComponent gscc = Mockito.mock(ServerCommunicationComponent.class);
Mockito.doNothing().when(gscc).wakeUpHost("wake");
OpenStackInfoPlugin osip = this.createOpenStackInfoPluginMock(hosts);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment