
com.github.sdnwiselab.sdnwise.node.SinkNode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sdn-wise-node Show documentation
Show all versions of sdn-wise-node Show documentation
The stateful Software Defined Networking solution for WIreless SEnsor Networks
/*
* Copyright (C) 2015 SDN-WISE
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package com.github.sdnwiselab.sdnwise.node;
import com.github.sdnwiselab.sdnwise.flowtable.*;
import static com.github.sdnwiselab.sdnwise.flowtable.Window.*;
import com.github.sdnwiselab.sdnwise.packet.*;
import static com.github.sdnwiselab.sdnwise.packet.NetworkPacket.*;
import com.github.sdnwiselab.sdnwise.util.NodeAddress;
import java.io.*;
import java.net.*;
import java.util.logging.*;
/**
*
* @author Sebastiano Milardo
*/
public class SinkNode extends Node {
private final String addrController;
private final int portController;
private Socket tcpSocket;
private DataOutputStream inviaOBJ;
private DataInputStream riceviOBJ;
private final Logger LOGGER;
public SinkNode(byte net_id, NodeAddress node,
String ipAddress,
int port,
String addrController,
int portController,
String neighboursPath,
String logLevel,
String dpid,
String mac,
long sPort) {
super(node, net_id, port, neighboursPath, logLevel);
this.battery = new FakeBattery();
this.addrController = addrController;
this.portController = portController;
FlowTableEntry toSink = new FlowTableEntry();
toSink.addWindow(new Window()
.setOperator(SDN_WISE_EQUAL)
.setSize(SDN_WISE_SIZE_2)
.setLhsLocation(SDN_WISE_PACKET)
.setLhs(SDN_WISE_DST_H)
.setRhsLocation(SDN_WISE_CONST)
.setRhs(this.addr.intValue()));
toSink.addWindow(Window.fromString("P.TYPE > 127"));
toSink.addAction(new ForwardUnicastAction().setNextHop(addr));
toSink.getStats().setPermanent();
flowTable.set(0, toSink);
LOGGER = Logger.getLogger(addr.toString());
startListening();
InetSocketAddress iAddr;
iAddr = new InetSocketAddress(ipAddress, port);
RegProxyPacket rpp = new RegProxyPacket(1, addr, dpid, mac, sPort, iAddr);
controllerTX(rpp);
}
@Override
public final void initSdnWise() {
super.initSdnWise();
setNum_hop_vs_sink(0);
setRssi_vs_sink(255);
this.setSemaphore(1);
}
@Override
public final void controllerTX(NetworkPacket pck) {
try {
inviaOBJ = new DataOutputStream(tcpSocket.getOutputStream());
inviaOBJ.write(pck.toByteArray());
LOGGER.log(Level.FINE, "[{0}]: CTX {1}",
new Object[]{addr.toString(), pck});
} catch (IOException | NullPointerException ex) {
LOGGER.log(Level.SEVERE, null, ex);
}
}
private void startListening() {
try {
tcpSocket = new Socket(addrController, portController);
Thread th = new Thread(new TcpListener());
th.start();
} catch (IOException ex) {
Logger.getLogger(SinkNode.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void SDN_WISE_Callback(DataPacket packet) {
controllerTX(packet);
}
@Override
public void rxConfig(ConfigPacket packet) {
NodeAddress dest = packet.getDst();
NodeAddress src = packet.getSrc();
if (!dest.equals(addr)) {
runFlowMatch(packet);
} else {
if (!src.equals(addr)) {
controllerTX(packet);
} else {
if (marshalPacket(packet) != 0) {
controllerTX(packet);
}
}
}
}
@Override
public NodeAddress getActualSinkAddress() {
return addr;
}
private class TcpListener implements Runnable {
@Override
public void run() {
try {
riceviOBJ = new DataInputStream(tcpSocket.getInputStream());
while (true) {
int len = riceviOBJ.read();
if (len > 0) {
byte[] packet = new byte[len];
packet[0] = (byte) len;
riceviOBJ.read(packet, 1, len - 1);
NetworkPacket np = new NetworkPacket(packet);
LOGGER.log(Level.FINE, "[{0}]: CRX {1}", new Object[]{addr.toString(), np});
flowTableQueue.put(np);
}
}
} catch (IOException | InterruptedException ex) {
Logger.getLogger(SinkNode.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy