Go to the documentation of this file.00001
00009 package org.rosbuilding.wol;
00010
00011 import java.net.DatagramPacket;
00012 import java.net.DatagramSocket;
00013 import java.net.InetAddress;
00014
00015 import org.ros.message.MessageListener;
00016 import org.ros.namespace.GraphName;
00017 import org.ros.node.AbstractNodeMain;
00018 import org.ros.node.ConnectedNode;
00019 import org.ros.node.topic.Subscriber;
00020
00027 public class WolNode extends AbstractNodeMain
00028 implements MessageListener<std_msgs.String> {
00029
00030 private static final String SUB_CMD = "wol";
00031 private static final int PORT = 9;
00032
00033 private transient Subscriber<std_msgs.String> subscriberWol;
00034
00035 private transient String prefix;
00036 private transient ConnectedNode connectedNode;
00037 private transient String network;
00038
00039 @Override
00040 public GraphName getDefaultNodeName() {
00041 return GraphName.of("/system_wol");
00042 }
00043
00044 @Override
00045 public void onStart(ConnectedNode connectedNode) {
00046 super.onStart(connectedNode);
00047 this.connectedNode = connectedNode;
00048
00049 this.loadParameters();
00050
00051 this.subscriberWol = this.connectedNode.newSubscriber(
00052 SUB_CMD,
00053 std_msgs.String._TYPE);
00054 this.subscriberWol.addMessageListener(this);
00055 }
00056
00057 @Override
00058 public void onNewMessage(std_msgs.String msg) {
00059 try {
00060 byte[] macBytes = getMacBytes(msg.getData());
00061 byte[] bytes = new byte[6 + 16 * macBytes.length];
00062 for (int i = 0; i < 6; i++) {
00063 bytes[i] = (byte) 0xff;
00064 }
00065 for (int i = 6; i < bytes.length; i += macBytes.length) {
00066 System.arraycopy(macBytes, 0, bytes, i, macBytes.length);
00067 }
00068
00069 InetAddress address = InetAddress.getByName(this.network);
00070 DatagramPacket packet = new DatagramPacket(
00071 bytes,
00072 bytes.length,
00073 address,
00074 PORT);
00075 DatagramSocket socket = new DatagramSocket();
00076 socket.send(packet);
00077 socket.close();
00078
00079 this.connectedNode.getLog().info("Wake-on-LAN packet sent. For : " + msg.getData());
00080 } catch (Exception e) {
00081 this.connectedNode.getLog().error("Failed to send Wake-on-LAN packet: " + e.getMessage() );
00082 }
00083 }
00084
00088 private void loadParameters() {
00089 this.prefix = this.connectedNode.getParameterTree()
00090 .getString("~tf_prefix", "");
00091
00092 if (!this.prefix.equals("")) {
00093 this.prefix = String.format("/%s/", this.prefix);
00094 }
00095
00096 this.network = this.connectedNode.getParameterTree()
00097 .getString("~network", "255.255.255.255");
00098 }
00099
00100 private static byte[] getMacBytes(String macStr) throws IllegalArgumentException {
00101 byte[] bytes = new byte[6];
00102 String[] hex = macStr.split("(\\:|\\-)");
00103 if (hex.length != 6) {
00104 throw new IllegalArgumentException("Invalid MAC address.");
00105 }
00106 try {
00107 for (int i = 0; i < 6; i++) {
00108 bytes[i] = (byte) Integer.parseInt(hex[i], 16);
00109 }
00110 } catch (NumberFormatException e) {
00111 throw new IllegalArgumentException(
00112 "Invalid hex digit in MAC address.");
00113 }
00114 return bytes;
00115 }
00116
00117 }