<commit_before>from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import CONFIG_DISPATCHER
from ryu.controller.handler import MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_parser
from ryu.ofproto import ofproto_v1_0
from ryu.ofproto import ofproto_v1_0_parser
from ryu.ofproto import ofproto_common
#from ryu.controller.controller import *
from ryu.controller import *
from ryu import utils
from ryu.lib import hub
from ryu.app.ofctl import api
import threading
import zmq
import time
from ryu.app.ofctl import event
import struct
import sys
import logging
import sys
import thread
import json

sys.path.insert(0,'../../../Engine/libraries/netip/python/')

from netip import *

ide_socket = None
message_netip = []
datapaths = []

n = "-1"

def start():
   context = zmq.Context()

#Introduce this in functions
#publisher = context.socket (zmq.PUB)
#publisher.bind("tcp://localhost:5558")
#publisher.send (message)

def send_msg(message):
   context = zmq.Context()
   publisher = context.socket(zmq.PUSH)
   publisher.bind("tcp://127.0.0.1:5558")
   publisher.send(message)

def port_stats_reply_handler(msg, netide_datapath):
   #ofp = msg.datapath.ofproto
   body = msg.body
   #print(body[0])
   #self.counter = self.counter + 10
   #throughput = body.stat[4]*8/self.counter
   #print body
   #print(msg)
   #print(type(msg))

   
   ports = []
   stat_message = {"PortStats": []}
   #For each port
   for stat in body:
      ports.append('dpid=%d port_no=%d '
	               'rx_packets=%d tx_packets=%d '
	               'rx_bytes=%d tx_bytes=%d '
	               'rx_dropped=%d tx_dropped=%d '
	               'rx_errors=%d tx_errors=%d '
	               'rx_frame_err=%d rx_over_err=%d rx_crc_err=%d '
	               'collisions=%d ' %
	               (netide_datapath, stat.port_no,
	                stat.rx_packets, stat.tx_packets,
	                stat.rx_bytes, stat.tx_bytes,
	                stat.rx_dropped, stat.tx_dropped,
	                stat.rx_errors, stat.tx_errors,
	                stat.rx_frame_err, stat.rx_over_err,
	                stat.rx_crc_err, stat.collisions))
      json_stats = {"Type":"Port Stats", "dpid": netide_datapath, "port":stat.port_no, "rx_packets": stat.rx_packets, "tx_packets": stat.tx_packets, "rx_bytes": stat.rx_bytes, "tx_bytes": stat.tx_bytes, "rx_dropped": stat.rx_dropped, "tx_dropped": stat.tx_dropped, "rx_errors": stat.rx_errors, "tx_errors": stat.tx_errors, "rx_frame_err": stat.rx_frame_err, "rx_over_err": stat.rx_over_err, "rx_crc_err": stat.rx_crc_err, "collisions": stat.collisions}
      stats = json.dumps(json_stats)
      stat_message["PortStats"].append(json_stats)
      #IDE_connection(stats)
   #print (ports[0].tx_bytes)

   print'\033[1;32m PortStats: %s \033[1;m'%(ports)
   print('\n')
   IDE_connection(json.dumps(stat_message))
   
   



def flow_stats_reply_handler(msg, netide_datapath):
    body = msg.body
    flows = []
    stat_message = {"FlowStats": []} 

    for stat in body:
        flows.append('table_id=%s match=%s '
                     'duration_sec=%d duration_nsec=%d '
                     'priority=%d '
                     'idle_timeout=%d hard_timeout=%d '
                     'cookie=%d packet_count=%d byte_count=%d '
                     'actions=%s' %
                     (stat.table_id, stat.match, stat.duration_sec, stat.duration_nsec, stat.priority, stat.idle_timeout, stat.hard_timeout,
                     stat.cookie, stat.packet_count, stat.byte_count, stat.actions))
        json_stats = {"Type":"Flow Stats", "dpid": netide_datapath, "table_id": stat.table_id, "duration_sec": stat.duration_sec, "duration_nsec": stat.duration_nsec, "priority": stat.priority, "idle_timeout": stat.idle_timeout, "hard_timeout": stat.hard_timeout, "cookie": stat.cookie, "packet_count": stat.packet_count, "byte_count": stat.byte_count}
        stats = json.dumps(json_stats)
        stat_message["FlowStats"].append(json_stats)

    print'\033[1;36m FlowStats: %s \033[1;m'%(flows)
    print('\n')
    IDE_connection(json.dumps(stat_message))



def aggregate_stats_reply_handler(msg, netide_datapath):
    body = msg.body
    stat_message = {"AggregatedStats": []}
    json_stats = {"Type":"Aggregate Stats", "dpid": netide_datapath, "packet_count": body[0].packet_count, "byte_count": body[0].byte_count, "flow_count": body[0].flow_count}
    stats = json.dumps(json_stats)
    stat_message["AggregatedStats"].append(json_stats)

    print'\033[1;34m AggregateStats: packet_count=%d, byte_count=%d, flow_count=%d\033[1;m'%(body[0].packet_count, body[0].byte_count, body[0].flow_count)
    print('\n')
    IDE_connection(json.dumps(stats))



def table_stats_reply_handler(msg):
    body = msg.body
    tables = []
    for stat in body:
        tables.append('table_id=%d name=%s wildcards=0x%02x '
                      'max_entries=%d active_count=%d '
                      'lookup_count=%d matched_count=%d' %
                      (stat.table_id, stat.name, stat.wildcards,
                       stat.max_entries, stat.active_count,
                       stat.lookup_count, stat.matched_count))
    print'\033[1;37m TableStats: %s \033[1;m'%(tables)
    print('\n')



def queue_stats_reply_handler(msg):
    body = msg.body<commit_msg>send correct aggregated stats message<commit_after>from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import CONFIG_DISPATCHER
from ryu.controller.handler import MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_parser
from ryu.ofproto import ofproto_v1_0
from ryu.ofproto import ofproto_v1_0_parser
from ryu.ofproto import ofproto_common
#from ryu.controller.controller import *
from ryu.controller import *
from ryu import utils
from ryu.lib import hub
from ryu.app.ofctl import api
import threading
import zmq
import time
from ryu.app.ofctl import event
import struct
import sys
import logging
import sys
import thread
import json

sys.path.insert(0,'../../../Engine/libraries/netip/python/')

from netip import *

ide_socket = None
message_netip = []
datapaths = []

n = "-1"

def start():
   context = zmq.Context()

#Introduce this in functions
#publisher = context.socket (zmq.PUB)
#publisher.bind("tcp://localhost:5558")
#publisher.send (message)

def send_msg(message):
   context = zmq.Context()
   publisher = context.socket(zmq.PUSH)
   publisher.bind("tcp://127.0.0.1:5558")
   publisher.send(message)

def port_stats_reply_handler(msg, netide_datapath):
   #ofp = msg.datapath.ofproto
   body = msg.body
   #print(body[0])
   #self.counter = self.counter + 10
   #throughput = body.stat[4]*8/self.counter
   #print body
   #print(msg)
   #print(type(msg))

   
   ports = []
   stat_message = {"PortStats": []}
   #For each port
   for stat in body:
      ports.append('dpid=%d port_no=%d '
	               'rx_packets=%d tx_packets=%d '
	               'rx_bytes=%d tx_bytes=%d '
	               'rx_dropped=%d tx_dropped=%d '
	               'rx_errors=%d tx_errors=%d '
	               'rx_frame_err=%d rx_over_err=%d rx_crc_err=%d '
	               'collisions=%d ' %
	               (netide_datapath, stat.port_no,
	                stat.rx_packets, stat.tx_packets,
	                stat.rx_bytes, stat.tx_bytes,
	                stat.rx_dropped, stat.tx_dropped,
	                stat.rx_errors, stat.tx_errors,
	                stat.rx_frame_err, stat.rx_over_err,
	                stat.rx_crc_err, stat.collisions))
      json_stats = {"Type":"Port Stats", "dpid": netide_datapath, "port":stat.port_no, "rx_packets": stat.rx_packets, "tx_packets": stat.tx_packets, "rx_bytes": stat.rx_bytes, "tx_bytes": stat.tx_bytes, "rx_dropped": stat.rx_dropped, "tx_dropped": stat.tx_dropped, "rx_errors": stat.rx_errors, "tx_errors": stat.tx_errors, "rx_frame_err": stat.rx_frame_err, "rx_over_err": stat.rx_over_err, "rx_crc_err": stat.rx_crc_err, "collisions": stat.collisions}
      stats = json.dumps(json_stats)
      stat_message["PortStats"].append(json_stats)
      #IDE_connection(stats)
   #print (ports[0].tx_bytes)

   print'\033[1;32m PortStats: %s \033[1;m'%(ports)
   print('\n')
   IDE_connection(json.dumps(stat_message))
   
   



def flow_stats_reply_handler(msg, netide_datapath):
    body = msg.body
    flows = []
    stat_message = {"FlowStats": []} 

    for stat in body:
        flows.append('table_id=%s match=%s '
                     'duration_sec=%d duration_nsec=%d '
                     'priority=%d '
                     'idle_timeout=%d hard_timeout=%d '
                     'cookie=%d packet_count=%d byte_count=%d '
                     'actions=%s' %
                     (stat.table_id, stat.match, stat.duration_sec, stat.duration_nsec, stat.priority, stat.idle_timeout, stat.hard_timeout,
                     stat.cookie, stat.packet_count, stat.byte_count, stat.actions))
        json_stats = {"Type":"Flow Stats", "dpid": netide_datapath, "table_id": stat.table_id, "duration_sec": stat.duration_sec, "duration_nsec": stat.duration_nsec, "priority": stat.priority, "idle_timeout": stat.idle_timeout, "hard_timeout": stat.hard_timeout, "cookie": stat.cookie, "packet_count": stat.packet_count, "byte_count": stat.byte_count}
        stats = json.dumps(json_stats)
        stat_message["FlowStats"].append(json_stats)

    print'\033[1;36m FlowStats: %s \033[1;m'%(flows)
    print('\n')
    IDE_connection(json.dumps(stat_message))



def aggregate_stats_reply_handler(msg, netide_datapath):
    body = msg.body
    stat_message = {"AggregatedStats": []}
    json_stats = {"Type":"Aggregate Stats", "dpid": netide_datapath, "packet_count": body[0].packet_count, "byte_count": body[0].byte_count, "flow_count": body[0].flow_count}
    stats = json.dumps(json_stats)
    stat_message["AggregatedStats"] = json_stats

    print'\033[1;34m AggregateStats: packet_count=%d, byte_count=%d, flow_count=%d\033[1;m'%(body[0].packet_count, body[0].byte_count, body[0].flow_count)
    print('\n')
    IDE_connection(json.dumps(stat_message))



def table_stats_reply_handler(msg):
    body = msg.body
    tables = []
    for stat in body:
        tables.append('table_id=%d name=%s wildcards=0x%02x '
                      'max_entries=%d active_count=%d '
                      'lookup_count=%d matched_count=%d' %
                      (stat.table_id, stat.name, stat.wildcards,
                       stat.max_entries, stat.active_count,
                       stat.lookup_count, stat.matched_count))
    print'\033[1;37m TableStats: %s \033[1;m'%(tables)
    print('\n')



def queue_stats_reply_handler(msg):
    body = msg.body
{'fraction_non_alphanumeric': 0.1299113392440504, 'fraction_numerical': 0.013999066728884742, 'mean_word_length': 3.2222222222222223, 'pattern_counts': {'":': 68, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2641764', 'n_tokens_mistral': 3788, 'n_tokens_neox': 3646, 'n_words': 670}