Skip to content

Instantly share code, notes, and snippets.

@oshothebig
Created September 3, 2011 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oshothebig/1190955 to your computer and use it in GitHub Desktop.
Save oshothebig/1190955 to your computer and use it in GitHub Desktop.
public static void main(String[] args) {
// リピータハブとして動作
MessageListener hub = new MessageListener() {
public void messageReceived(Channel channel, OFMessage msg) {
if (msg.getType() == OFType.PACKET_IN) {
OFPacketIn in = (OFPacketIn)msg;
OFPacketOut out = (OFPacketOut)factory.getMessage(OFType.PACKET_OUT);
out.setBufferId(in.getBufferId());
out.setInPort(in.getInPort());
// フラッディングで出力
OFActionOutput action = new OFActionOutput();
action.setPort(OFPort.OFPP_FLOOD.getValue());
out.setActions(Collections.singletonList((OFAction)action));
out.setActionsLength((short)OFActionOutput.MINIMUM_LENGTH);
// PACKET_INで受信したパケットの内容を出力パケットに設定
if (in.getBufferId() == 0xffffffff) {
byte[] packetData = in.getPacketData();
out.setLength((short)(OFPacketOut.MINIMUM_LENGTH + out.getActionsLength() + packetData.length));
out.setPacketData(packetData);
// バッファリングされている場合には、パケットの内容を設定しなくてもよい
} else {
out.setLength((short)(OFPacketOut.MINIMUM_LENGTH + out.getActionsLength()));
}
channel.write(out);
log.debug("Message sent: message({}), switch({})", out.getType(), channel.getRemoteAddress());
}
}
};
SimpleController controller = new SimpleController();
controller.addMessageListener(hub);
// 引数が与えられた場合は、指定されたポート番号を使用
int port = CONTROLLER_DEFAULT_PORT;
if (args.length >= 1) {
port = Integer.parseInt(args[0]);
}
controller.start(port);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment