<commit_before>import logging
import time
import sys
import zmq.green as zmq
import socket
import fcntl
import struct
import threading
import dill #for pickling what standard pickle can’t cope with
try:
   import cPickle as pickle
except:
   import pickle

import wishful_framework as msgs

__author__ = "<NAME>"
__copyright__ = "Copyright (c) 2015, Technische Universitat Berlin"
__version__ = "0.1.0"
__email__ = "<EMAIL>"


def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    val = socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', bytes(ifname[:15], 'utf-8'))
        )[20:24])
    s.close()

    return val


class TransportChannel(object):
    def __init__(self, agent):
        self.log = logging.getLogger("{module}.{name}".format(
            module=self.__class__.__module__, name=self.__class__.__name__))

        self.agent = agent
        self.controllerDL = None
        self.controllerUL = None

        self.uplinkSocketLock = threading.Lock()
        self.poller = zmq.Poller()
        self.context = zmq.Context()
        self.dl_socket = self.context.socket(zmq.SUB) # for downlink communication with controller
        self.dl_socket.setsockopt_string(zmq.SUBSCRIBE,  self.agent.uuid)
        self.dl_socket.setsockopt(zmq.LINGER, 100)
        self.ul_socket = self.context.socket(zmq.PUB) # for uplink communication with controller

        #register module socket in poller
        self.poller.register(self.dl_socket, zmq.POLLIN)


    def connect(self, downlink, uplink):
        if self.controllerDL and self.controllerUL:
            try:
                self.ul_socket.disconnect(self.controllerUL)
                self.dl_socket.disconnect(self.controllerDL)
            except:
                pass

        self.controllerDL = downlink
        self.controllerUL = uplink
        self.ul_socket.connect(self.controllerUL)
        self.dl_socket.connect(self.controllerDL)


    def disconnect(self):
        #disconnect
        if self.controllerDL and self.controllerUL:
            try:
                self.ul_socket.disconnect(self.controllerUL)
                self.dl_socket.disconnect(self.controllerDL)
            except:
                pass


    def subscribe_to(self, topic):
        self.log.debug("Agent subscribes to topic: {}".format(topic))
        self.dl_socket.setsockopt_string(zmq.SUBSCRIBE, str(topic))
 

    def set_recv_callback(self, callback):
        self.recv_callback = callback


    def send_uplink(self, msgContainer):
        #TODO: it is quick fix; find better solution with socket per thread
        self.uplinkSocketLock.acquire()
        try:
            self.ul_socket.send_multipart(msgContainer)
        finally:<commit_msg>support for python 2.7/3<commit_after>import logging
import time
import sys
import zmq.green as zmq
import socket
import fcntl
import struct
import threading
import dill #for pickling what standard pickle can’t cope with
try:
   import cPickle as pickle
except:
   import pickle

import wishful_framework as msgs

__author__ = "<NAME>"
__copyright__ = "Copyright (c) 2015, Technische Universitat Berlin"
__version__ = "0.1.0"
__email__ = "<EMAIL>"


def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    if sys.version_info.major >= 3:
        ifname = bytes(ifname[:15], 'utf-8')
    else:
        ifname = ifname[:15]

    val = socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname)
        )[20:24])
    s.close()

    return val


class TransportChannel(object):
    def __init__(self, agent):
        self.log = logging.getLogger("{module}.{name}".format(
            module=self.__class__.__module__, name=self.__class__.__name__))

        self.agent = agent
        self.controllerDL = None
        self.controllerUL = None

        self.uplinkSocketLock = threading.Lock()
        self.poller = zmq.Poller()
        self.context = zmq.Context()
        self.dl_socket = self.context.socket(zmq.SUB) # for downlink communication with controller
        if sys.version_info.major >= 3:
            self.dl_socket.setsockopt_string(zmq.SUBSCRIBE,  self.agent.uuid)
        else:
            self.dl_socket.setsockopt(zmq.SUBSCRIBE,  self.agent.uuid)
        self.dl_socket.setsockopt(zmq.LINGER, 100)
        self.ul_socket = self.context.socket(zmq.PUB) # for uplink communication with controller

        #register module socket in poller
        self.poller.register(self.dl_socket, zmq.POLLIN)


    def connect(self, downlink, uplink):
        if self.controllerDL and self.controllerUL:
            try:
                self.ul_socket.disconnect(self.controllerUL)
                self.dl_socket.disconnect(self.controllerDL)
            except:
                pass

        self.controllerDL = downlink
        self.controllerUL = uplink
        self.ul_socket.connect(self.controllerUL)
        self.dl_socket.connect(self.controllerDL)


    def disconnect(self):
        #disconnect
        if self.controllerDL and self.controllerUL:
            try:
                self.ul_socket.disconnect(self.controllerUL)
                self.dl_socket.disconnect(self.controllerDL)
            except:
                pass


    def subscribe_to(self, topic):
        self.log.debug("Agent subscribes to topic: {}".format(topic))
        if sys.version_info.major >= 3:
            self.dl_socket.setsockopt_string(zmq.SUBSCRIBE, str(topic))
        else:
            self.dl_socket.setsockopt(zmq.SUBSCRIBE, str(topic))
 

    def set_recv_callback(self, callback):
        self.recv_callback = callback


    def send_uplink(self, msgContainer):
        #TODO: it is quick fix; find better solution with socket per thread
        self.uplinkSocketLock.acquire()
        try:
            self.ul_socket.send_multipart(msgContainer)
        finally:
{'fraction_non_alphanumeric': 0.09463775424441083, 'fraction_numerical': 0.009749537737434862, 'mean_word_length': 2.5480023852116878, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '1865330', 'n_tokens_mistral': 1902, 'n_tokens_neox': 1807, 'n_words': 384}