<commit_before><?php
/*
 * Spring Signage Ltd - http://www.springsignage.com
 * Copyright (C) 2015 Spring Signage Ltd
 * (PlayerAction.php)
 */


namespace Xibo\XMR;


abstract class PlayerAction implements PlayerActionInterface
{
    // The action
    public $action;

    // TTL
    public $createdDt;
    public $ttl;

    /** @var int QOS */
    private $qos;

    // Channel and key
    private $channel;
    private $publicKey;

    /**
     * Set the identity of this Player Action
     * @param string $channel
     * @param string $key
     * @return $this
     * @throws PlayerActionException if the key is invalid
     */
    public final function setIdentity($channel, $key)
    {
        $this->channel = $channel;
        if (!$this->publicKey = openssl_get_publickey($key))
            throw new PlayerActionException('Invalid Public Key');

        return $this;
    }

    /**
     * Set the message TTL
     * @param int $ttl
     * @return $this
     */
    public final function setTtl($ttl = 120)
    {
        $this->ttl = $ttl;
        return $this;
    }

    /**
     * Set the message QOS
     * @param int $qos
     * @return $this
     */
    public final function setQos($qos = 10)
    {
        $this->qos = $qos;
        return $this;
    }

    /**
     * Serialize this object to its JSON representation
     * @param array $include
     * @return string
     */
    public final function serializeToJson($include = [])
    {
        $include = array_merge(['action', 'createdDt', 'ttl'], $include);

        $json = [];
        foreach (get_object_vars($this) as $key => $value) {
            if (in_array($key, $include)) {
                $json[$key] = $value;
            }
        }
        return json_encode($json);
    }

    /**
     * Return the encrypted message and keys
     * @return array
     * @throws PlayerActionException
     */
    public final function getEncryptedMessage()
    {
        $message = null;

        if (!openssl_seal($this->getMessage(), $message, $eKeys, [$this->publicKey]))
            throw new PlayerActionException('Cannot seal message');

        return [
            'key' => base64_encode($eKeys[0]),
            'message' => base64_encode($message)
        ];
    }

    /**
     * Send the action to the specified connection and wait for a reply (acknowledgement)
     * @param string $connection
     * @return string
     * @throws PlayerActionException
     */
    public final function send($connection)
    {
        try {
            // Set the message create date
            $this->createdDt = date('c');

            // Set the TTL if not already set
            if ($this->ttl == 0)
                $this->setTtl();

            // Set the QOS if not already set
            if ($this->qos === null)
                $this->setQos();

            // Get the encrypted message
            $encrypted = $this->getEncryptedMessage();

            // Envelope our message
            $message = [
                'channel' => $this->channel,
                'message' => $encrypted['message'],
                'key' => $encrypted['key'],
                'qos' => $this->qos
            ];

            // Issue a message payload to XMR.
            $context = new \ZMQContext();

            // Connect to socket
            $socket = new \ZMQSocket($context, \ZMQ::SOCKET_REQ);
            $socket->connect($connection);

            // Send the message to the socket
            $socket->send(json_encode($message));

            // Need to replace this with a non-blocking recv() with a retry loop
            $retries = 15;<commit_msg>Set Linger on socket for XMR communication<commit_after><?php
/*
 * Spring Signage Ltd - http://www.springsignage.com
 * Copyright (C) 2015 Spring Signage Ltd
 * (PlayerAction.php)
 */


namespace Xibo\XMR;


abstract class PlayerAction implements PlayerActionInterface
{
    // The action
    public $action;

    // TTL
    public $createdDt;
    public $ttl;

    /** @var int QOS */
    private $qos;

    // Channel and key
    private $channel;
    private $publicKey;

    /**
     * Set the identity of this Player Action
     * @param string $channel
     * @param string $key
     * @return $this
     * @throws PlayerActionException if the key is invalid
     */
    public final function setIdentity($channel, $key)
    {
        $this->channel = $channel;
        if (!$this->publicKey = openssl_get_publickey($key))
            throw new PlayerActionException('Invalid Public Key');

        return $this;
    }

    /**
     * Set the message TTL
     * @param int $ttl
     * @return $this
     */
    public final function setTtl($ttl = 120)
    {
        $this->ttl = $ttl;
        return $this;
    }

    /**
     * Set the message QOS
     * @param int $qos
     * @return $this
     */
    public final function setQos($qos = 10)
    {
        $this->qos = $qos;
        return $this;
    }

    /**
     * Serialize this object to its JSON representation
     * @param array $include
     * @return string
     */
    public final function serializeToJson($include = [])
    {
        $include = array_merge(['action', 'createdDt', 'ttl'], $include);

        $json = [];
        foreach (get_object_vars($this) as $key => $value) {
            if (in_array($key, $include)) {
                $json[$key] = $value;
            }
        }
        return json_encode($json);
    }

    /**
     * Return the encrypted message and keys
     * @return array
     * @throws PlayerActionException
     */
    public final function getEncryptedMessage()
    {
        $message = null;

        if (!openssl_seal($this->getMessage(), $message, $eKeys, [$this->publicKey]))
            throw new PlayerActionException('Cannot seal message');

        return [
            'key' => base64_encode($eKeys[0]),
            'message' => base64_encode($message)
        ];
    }

    /**
     * Send the action to the specified connection and wait for a reply (acknowledgement)
     * @param string $connection
     * @return string
     * @throws PlayerActionException
     */
    public final function send($connection)
    {
        try {
            // Set the message create date
            $this->createdDt = date('c');

            // Set the TTL if not already set
            if ($this->ttl == 0)
                $this->setTtl();

            // Set the QOS if not already set
            if ($this->qos === null)
                $this->setQos();

            // Get the encrypted message
            $encrypted = $this->getEncryptedMessage();

            // Envelope our message
            $message = [
                'channel' => $this->channel,
                'message' => $encrypted['message'],
                'key' => $encrypted['key'],
                'qos' => $this->qos
            ];

            // Issue a message payload to XMR.
            $context = new \ZMQContext();

            // Connect to socket
            $socket = new \ZMQSocket($context, \ZMQ::SOCKET_REQ);
            $socket->setSockOpt(\ZMQ::SOCKOPT_LINGER, 2000);
            $socket->connect($connection);

            // Send the message to the socket
            $socket->send(json_encode($message));

            // Need to replace this with a non-blocking recv() with a retry loop
            $retries = 15;
{'fraction_non_alphanumeric': 0.12258240261509126, 'fraction_numerical': 0.005175701443748297, 'mean_word_length': 1.7678100263852243, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1274093', 'n_tokens_mistral': 2135, 'n_tokens_neox': 2022, 'n_words': 587}