Login   Register  
PHP Classes
elePHPant
Icontem

File: docs/api/files/Communication/TRuntimeCommunication.php.txt

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of AlexanderC  >  Threadator  >  docs/api/files/Communication/TRuntimeCommunication.php.txt  >  Download  
File: docs/api/files/Communication/TRuntimeCommunication.php.txt
Role: Documentation
Content type: text/plain
Description: Documentation
Class: Threadator
Create threads and send messages between them
Author: By
Last change:
Date: 4 months ago
Size: 1,719 bytes
 

Contents

Class file image Download
<?php
/**
 * @author AlexanderC <self@alexanderc.me>
 * @date 4/8/14
 * @time 12:37 AM
 */

namespace Threadator\Communication;


use Threadator\Thread;

trait TRuntimeCommunication
{
    /**
     * @param Thread $thread
     * @param mixed $message
     * @return bool
     */
    public function sendMessage(Thread $thread, $message)
    {
        return $this->communication->send(
            $thread->getPid() - $this->getPid() + ICommunicationConstants::COMM_RECEIVER_PAD,
            $message
        );
    }

    /**
     * @param mixed $message
     * @return array
     */
    public function broadcastMessage($message)
    {
        $results = array();

        foreach($this->getIterator() as $thread) {
            $result = $this->communication->send(
                $thread->getPid() - $this->getPid() + ICommunicationConstants::COMM_RECEIVER_PAD,
                $message
            );

            $results[] = [$result, $thread];

            // an issue here (((
            //yield $result => $thread;
        }

        return $results;
    }

    /**
     * Try to get message, but do not block
     *
     * @return \Generator
     */
    public function touchMessage()
    {
        foreach($this->getIterator() as $thread) {
            $result = $this->communication->touch($thread->getPid(), $message);

            yield $result => $message;
        }
    }

    /**
     * Block until the first message arrives
     *
     * @return \Generator
     */
    public function receiveMessage()
    {
        foreach($this->getIterator() as $thread) {
            $result = $this->communication->receive($thread->getPid(), $message);

            yield $result => $message;
        }
    }
}