<commit_before>#!/usr/bin/env python

"""
@package ion.agents.platform.port_agent_helper
@file    ion/agents/platform/port_agent_helper.py
@author  <NAME>
@brief   Support for port agent coordination. Preliminary.
"""

__author__ = '<NAME>'
__license__ = 'Apache 2.0'

#
# See https://confluence.oceanobservatories.org/display/syseng/CIAD+MI+Port+Agent+Design
#

from pyon.public import log
import logging
from pyon.util.containers import DotDict

from ion.agents.platform.port_agent_packet import PortAgentPacket, HEADER_SIZE

from gevent import socket, select, Greenlet


class PortAgentHelper(object):
    """
    Preliminary sketch for supporting platform-port agent coordination.

    Initially, the connection is only to the cmd_port in the given
    comm_config. All packets received on that connection are logged out to a
    file under logs/ with a name indicating the platform ID.
    No commanding of the port agent included.
    """

    def __init__(self, platform_id, instrument_id, comms_config):
        """
        @param platform_id:
        @param instrument_id:
        @param comms_config:
        @return:
        """

        log.debug("%r/%r: PortAgentHelper called. comms_config=%s",
                  platform_id, instrument_id, comms_config)

        assert 'addr' in comms_config
        assert 'port' in comms_config
        assert 'cmd_port' in comms_config

        self._platform_id   = platform_id
        self._instrument_id = instrument_id
        self._comms_config  = DotDict(comms_config)

        self._outfilename = None
        self._outfile = None
        self._pp = None

    def _start_outfile(self):
        if log.isEnabledFor(logging.DEBUG):
            self._outfilename = "logs/port_agent_client_%s.txt" % self._platform_id
            try:
                self._outfile = file(self._outfilename, 'a')
                self._outfile.write("%r/%r: port agent client connected\n" % (
                    self._platform_id, self._instrument_id))
                self._outfile.flush()

                log.debug("%r/%r: will write packets to %s",
                          self._platform_id, self._instrument_id, self._outfilename)

            except Exception as ex:
                log.warn("%r/%r: could not open output file: %s: %s",
                         self._platform_id, self._instrument_id, self._outfilename, ex)

    def _end_outfile(self):
        if log.isEnabledFor(logging.DEBUG) and self._outfile:
            try:
                self._outfile.write("%r/%r: port agent client disconnected\n\n" % (
                    self._platform_id, self._instrument_id))
                self._outfile.close()

            except Exception as ex:
                log.warn("%r/%r: could not close output file: %s: %s",
                         self._platform_id, self._instrument_id, self._outfilename, ex)
            finally:
                self._outfile = None

    def _connect_socket(self, host, port):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect((host, port))
        return sock

    def _handle_packet(self, pkt):
        if log.isEnabledFor(logging.DEBUG):
            self._outfile.write("%r/%r: %s: %s\n" % (
                self._platform_id, self._instrument_id,
                pkt.get_timestamp(), pkt.get_as_dict()))
            self._outfile.flush()

        # TODO do something concrete with received packet (notify platform agent, ...)

    def init_comms(self):

        log.debug("%r/%r: init_comms", self._platform_id, self._instrument_id)

        host     = self._comms_config.addr
        cmd_port = self._comms_config.cmd_port

        self._sock = self._connect_socket(host, cmd_port)

        self._start_outfile()

        self._receiver = _Receiver(self._platform_id, self._instrument_id,
                                   self._sock, self._handle_packet)
        self._receiver.start()
        log.info("init_comms(): started receiver")

    def stop_comms(self):<commit_msg>disable port agent helper for the moment<commit_after>#!/usr/bin/env python

"""
@package ion.agents.platform.port_agent_helper
@file    ion/agents/platform/port_agent_helper.py
@author  <NAME>
@brief   Support for port agent coordination. Preliminary.
"""

__author__ = '<NAME>'
__license__ = 'Apache 2.0'

#
# See https://confluence.oceanobservatories.org/display/syseng/CIAD+MI+Port+Agent+Design
#

from pyon.public import log
import logging
from pyon.util.containers import DotDict

from ion.agents.platform.port_agent_packet import PortAgentPacket, HEADER_SIZE

from gevent import socket, select, Greenlet


class PortAgentHelper(object):
    """
    Preliminary sketch for supporting platform-port agent coordination.

    Initially, the connection is only to the cmd_port in the given
    comm_config. All packets received on that connection are logged out to a
    file under logs/ with a name indicating the platform ID.
    No commanding of the port agent included.
    """

    # TODO enable this module once it's more complete
    _disabled = True

    def __init__(self, platform_id, instrument_id, comms_config):
        """
        @param platform_id:
        @param instrument_id:
        @param comms_config:
        @return:
        """
        if self._disabled:
            return

        log.debug("%r/%r: PortAgentHelper called. comms_config=%s",
                  platform_id, instrument_id, comms_config)

        assert 'addr' in comms_config
        assert 'port' in comms_config
        assert 'cmd_port' in comms_config

        self._platform_id   = platform_id
        self._instrument_id = instrument_id
        self._comms_config  = DotDict(comms_config)

        self._outfilename = None
        self._outfile = None
        self._pp = None

    def _start_outfile(self):
        if log.isEnabledFor(logging.DEBUG):
            self._outfilename = "logs/port_agent_client_%s.txt" % self._platform_id
            try:
                self._outfile = file(self._outfilename, 'a')
                self._outfile.write("%r/%r: port agent client connected\n" % (
                    self._platform_id, self._instrument_id))
                self._outfile.flush()

                log.debug("%r/%r: will write packets to %s",
                          self._platform_id, self._instrument_id, self._outfilename)

            except Exception as ex:
                log.warn("%r/%r: could not open output file: %s: %s",
                         self._platform_id, self._instrument_id, self._outfilename, ex)

    def _end_outfile(self):
        if log.isEnabledFor(logging.DEBUG) and self._outfile:
            try:
                self._outfile.write("%r/%r: port agent client disconnected\n\n" % (
                    self._platform_id, self._instrument_id))
                self._outfile.close()

            except Exception as ex:
                log.warn("%r/%r: could not close output file: %s: %s",
                         self._platform_id, self._instrument_id, self._outfilename, ex)
            finally:
                self._outfile = None

    def _connect_socket(self, host, port):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect((host, port))
        return sock

    def _handle_packet(self, pkt):
        if log.isEnabledFor(logging.DEBUG):
            self._outfile.write("%r/%r: %s: %s\n" % (
                self._platform_id, self._instrument_id,
                pkt.get_timestamp(), pkt.get_as_dict()))
            self._outfile.flush()

        # TODO do something concrete with received packet (notify platform agent, ...)

    def init_comms(self):
        if self._disabled:
            return

        log.debug("%r/%r: init_comms", self._platform_id, self._instrument_id)

        host     = self._comms_config.addr
        cmd_port = self._comms_config.cmd_port

        self._sock = self._connect_socket(host, cmd_port)

        self._start_outfile()

        self._receiver = _Receiver(self._platform_id, self._instrument_id,
                                   self._sock, self._handle_packet)
        self._receiver.start()
        log.info("init_comms(): started receiver")

    def stop_comms(self):
        if self._disabled:
            return
{'fraction_non_alphanumeric': 0.09717335921387844, 'fraction_numerical': 0.00048526022079340043, 'mean_word_length': 2.258498023715415, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '118579', 'n_tokens_mistral': 2435, 'n_tokens_neox': 2330, 'n_words': 568}