All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.sdnwiselab.sdnwise.node.SinkNode Maven / Gradle / Ivy

There is a newer version: 2.0.13
Show newest version
/*
 * 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 static final Logger LOGGER = Logger.getLogger(SinkNode.class.getName());

    public SinkNode(byte net_id, NodeAddress node,
            int port,
            String addrController,
            int portController,
            String neighboursPath,
            String logPath,
            String logLevel,
            String dpid,
            String mac,
            long sPort) {

        super(node, net_id, port, neighboursPath, logPath, logLevel);
        LOGGER.setLevel(Level.parse(logLevel));
        FileHandler fh;  
        try {
            fh = new FileHandler(addr + ".log");
            fh.setFormatter(new SimpleFormatter());
            LOGGER.addHandler(fh);
            LOGGER.setUseParentHandlers(false);
        } catch (IOException | SecurityException ex) {
            LOGGER.log(Level.SEVERE, null, ex);
        }
        
        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(new Window()
                .setOperator(SDN_WISE_NOT_EQUAL)
                .setSize(SDN_WISE_SIZE_1)
                .setLhsLocation(SDN_WISE_PACKET)
                .setLhs(SDN_WISE_TYPE)
                .setRhsLocation(SDN_WISE_CONST)
                .setRhs(0));
        toSink.addAction(new ForwardUnicastAction()
                .setNextHop(addr));
        toSink.getStats().setPermanent();
        flowTable.set(0, toSink);

        startListening();
        
        InetSocketAddress iAddr;
        try {
            iAddr = new InetSocketAddress(InetAddress.getLocalHost(),port);
            RegProxyPacket rpp = new RegProxyPacket(1, addr, dpid, mac, sPort, iAddr);
            controllerTX(rpp);
            System.out.println(rpp);
        } catch (UnknownHostException ex) {
            Logger.getLogger(SinkNode.class.getName()).log(Level.SEVERE, null, ex);
        }    
    }

    @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, "[N{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, "[N{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 - 2024 Weber Informatics LLC | Privacy Policy