Commit be8c12ee authored by aninhacostaribeiro's avatar aninhacostaribeiro
Browse files

Implemented the handle for turn down signal

parent cd1b81dc
package org.fogbowcloud.green.agent.core;
import java.util.Properties;
import java.util.logging.Logger;
import org.dom4j.tree.DefaultElement;
import org.jamppa.client.XMPPClient;
import org.jamppa.client.plugin.xep0077.XEP0077;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketFilter;
import org.xmpp.packet.IQ;
import org.xmpp.packet.Packet;
import org.xmpp.packet.IQ.Type;
public class AgentCommunicationComponent {
......@@ -22,29 +26,52 @@ public class AgentCommunicationComponent {
Integer.parseInt(this.prop.getProperty("xmpp.port")));
}
public void sendIamAliveSignal(){
public void init() {
XEP0077 register = new XEP0077();
try {
XEP0077 register = new XEP0077();
this.client.registerPlugin(register);
client.connect();
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");
client.process(false);
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");
}
client.getConnection().addPacketListener(new PacketListener() {
@Override
public void processPacket(Packet packet) {
new TurnOff().hibernate();
}
}, new PacketFilter() {
@Override
public boolean accept(Packet packet) {
if (!packet.getFrom().toString().equals(
prop.getProperty("xmpp.component"))) {
return false;
}
String ns = packet.getElement().element("query").getNamespaceURI();
if (!ns.equals("org.fogbowcloud.green.GoToBed")) {
return false;
}
return true;
}
});
}
public void sendIamAliveSignal(){
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"));
}
}
......@@ -3,6 +3,8 @@ package org.fogbowcloud.green.agent.core;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
......@@ -19,7 +21,8 @@ public class Main {
AgentCommunicationComponent acc = new AgentCommunicationComponent(prop);
acc.sendIamAliveSignal();
} catch (Exception e) {
System.err.println("You must provide as parameter the"
Logger logger = Logger.getLogger("green.agent");
logger.log(Level.SEVERE,"You must provide as parameter the"
+ " path for the configuration file");
}
}
......
......@@ -9,15 +9,15 @@ public class TurnOff {
String operatingSystem = System.getProperty("os.name");
if ("Linux".equals(operatingSystem)
|| "Mac OS X".equals(operatingSystem)) {
hibernateCommand = "pm-hibernate";
//Suspend or hibernate?
hibernateCommand = "pm-suspend";
}
//Suspend or hibernate?
ProcessBuilder pb =
new ProcessBuilder("sudo","-S", hibernateCommand);
try {
pb.start();
} catch (IOException e) {
System.out.println("It was not possible to turn down");
System.err.println("It was not possible to turn down");
}
}
......
......@@ -19,7 +19,7 @@ public class IAmAliveHandler extends AbstractQueryHandler {
//expected content format: IP plus MAC Address plus host name (eg. "123.456.789 a1:2b:3c:d4:45:67 bobo")
String ip = query.getElement().element("query").elementText("ip");
String hostName = query.getElement().element("query").elementText("hostName");
String jid = query.getFrom().toString();
String jid = query.getFrom().toFullJID();
String macAddress = query.getElement().element("query").elementText("macAddress");
gs.setAgentAddress(hostName, jid, ip, macAddress);
......
......@@ -4,9 +4,12 @@ import java.io.IOException;
import java.util.LinkedList;
import java.util.Properties;
import org.dom4j.tree.DefaultElement;
import org.fogbowcloud.green.server.communication.Agent;
import org.fogbowcloud.green.server.core.greenStrategy.GreenStrategy;
import org.jamppa.component.XMPPComponent;
import org.xmpp.packet.IQ;
import org.xmpp.packet.IQ.Type;
public class ServerCommunicationComponent extends XMPPComponent {
......@@ -32,11 +35,15 @@ public class ServerCommunicationComponent extends XMPPComponent {
ProcessBuilder pb = new ProcessBuilder("powerwake", macAddress);
pb.start();
} catch (IOException e) {
System.out.println("It was not possible to wake " + macAddress);
System.err.println("It was not possible to wake " + macAddress);
}
}
public void sendIdleHostToBed(String host) {
IQ iq = new IQ(Type.set);
iq.setTo("asdas");
iq.getElement().addElement("query", "org.fogbowcloud.green.GoToBed");
send(iq);
}
private void addHandlers() {
......
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