def test_message_integrity_and_if_nebpublisher_reads_from_the_queue(self):

        mq = MessageQueue(123456)
        with patch.object(ConnectionAdapter, '__init__') as mock_method:
            message = "Everything is Dust in the Wind"
            mq.send(message)
            mock_method.return_value = None
            mock_sender = ConnectionAdapter([('127.0.0.1', 61613)], 0)
            mock_sender.send = MagicMock()
            mock_sender.__connect = MagicMock(return_value=True)
            ipc2activemq.main()
            time.sleep(0.1)
            mock_sender.send.assert_called_with(message, None, None)
            assertEqual(0, mq.current_messages)
Beispiel #2
0
def datetime_request(queue, pid):
    """
    Handles a time request asynchronously
    :param queue: the queue to send the date back into
    :param pid: the request client pid
    """
    time = asctime().encode()
    queue.send(time, type=(int(pid) + 3))
    print(f"date {time} sent to client {pid}")


if __name__ == "__main__":
    # Create IPC with key 0x80 if it doesn't exist
    KEY = 0x80
    message_q = MessageQueue(KEY, IPC_CREX)

    with concurrent.futures.ThreadPoolExecutor(max_workers=8) as pool:
        while True:
            message, message_type = message_q.receive()

            # 1 : date request
            if message_type == 1:
                # Do it async
                pool.submit(datetime_request, message_q, message)
            # 2 : stop request
            elif message_type == 2:
                print("terminating")
                message_q.remove()
                break
Beispiel #3
0
"""
IPC client
"""

# pylint: disable=no-name-in-module
import sys
from sysv_ipc import MessageQueue, ExistentialError

if __name__ == "__main__":
    KEY = 128

    try:
        mq = MessageQueue(KEY)
    except ExistentialError:
        print("IPC queue doesn't exist")
        sys.exit(1)

    while True:
        message_type = input("1 for time 2 to stop : ")
        if message_type.isdigit() and message_type in ["1", "2"]:

            mq.send("", type=int(message_type))
            print(f"send type {message_type}")
            if message_type == "1":
                message_type, t = mq.receive(type=3)
                print(message_type)
            else:
                print("server stopped")
                break
        else:
            print("bad input")
from psutil import Process
from os import getpid
import psutil


def eprint(*args, **kwargs):
    print(*args, file=stderr, **kwargs)


# slice msg
msg = input()
msg1 = msg[0:int(len(msg) / 2)]
msg2 = msg[int(len(msg) / 2):len(msg)]

# args
queue = MessageQueue(int(argv[1]))
master_name = argv[2]

# master process must be the nearest parent with the exact given name
master_proc = Process(getpid())
while master_proc.name() != master_name:
    master_proc = master_proc.parent()

with Semaphore(int(argv[1])):
    try:
        # send-receive echo
        # type=1 : from me to other
        # type=2 : to me from other
        queue.send(msg1, type=1)
        # will block until a process claims the rest of the message
        (echo1, echo_type) = queue.receive(type=2)
Beispiel #5
0
def market(weatherAttributes, prices, exitFlag):
    print("[%d] Market (main) : Init" % getpid())

    # create handler with a closure, reroute signals, and launch external process
    externalEvents = [0, 0]

    def market_handler(sig, frame):
        if sig == SIGUSR1:
            externalEvents[0] = 1
        elif sig == SIGUSR2:
            externalEvents[1] = 1

    signal(SIGUSR1, market_handler)
    signal(SIGUSR2, market_handler)
    externalProcess = Process(target=external, args=(getpid(), exitFlag))
    externalProcess.start()

    # create lock that will be passed to many threads to treat each request
    sharedLock = Lock()

    # create message queues to Homes
    queueToHome = MessageQueue(marketKey, IPC_CREAT)
    # if queue was not properly deleted, it's still there and we have to do it ourselves
    if queueToHome.current_messages > 0:
        queueToHome.remove()
        queueToHome = MessageQueue(marketKey, IPC_CREAT)

    print("[%d] Market (main) : messageQueue created" % getpid())
    buying = []
    selling = []

    idx = 0
    price = -1
    gamma = 0.90  # long-term attenuation coefficient
    alpha = [
        -0.052, 0.075, 0.23, -0.07
    ]  # modulating coefficient for internal factors: Temperature, Rain, bought, sold
    internalEvents = [0, 0, 0, 0]
    beta = [-7, 30
            ]  # modulating coefficient for external factors: SIGUSR1, SIGUSR2

    # print("[%d] Market (main) : all external processes launched" % getpid())
    while not exitFlag.value:
        sleep(period)
        # upon receiving a buying message, we launch a thread
        try:
            while queueToHome.current_messages > 0:
                message, _ = queueToHome.receive(block=False, type=1)
                (Thread(target=marketThread,
                        args=(
                            message,
                            buying,
                            sharedLock,
                        ))).start()
        except (NotAttachedError, BusyError):
            pass
        # upon receiving a selling message, we launch a thread
        try:
            while queueToHome.current_messages > 0:
                message, _ = queueToHome.receive(block=False, type=2)
                (Thread(target=marketThread,
                        args=(
                            message,
                            selling,
                            sharedLock,
                        ))).start()
        except (NotAttachedError, BusyError):
            pass

        print(
            "[%d] external = %s \tweather = [ %.2f, %.2f ]\tbuying = %s\tselling = %s"
            % (getpid(), externalEvents, weatherAttributes[0],
               weatherAttributes[1], buying, selling))
        internalEvents = [
            weatherAttributes[0], weatherAttributes[1],
            sum(buying),
            sum(selling)
        ]
        if price == -1:
            print("[%d] Initiating energy price" % getpid())
            price = sum([
                alpha[k] * internalEvents[k]
                for k in range(len(internalEvents))
            ]) + sum([
                beta[k] * externalEvents[k] for k in range(len(externalEvents))
            ])
        else:
            price = gamma * price + sum([
                alpha[k] * internalEvents[k]
                for k in range(len(internalEvents))
            ]) + sum([
                beta[k] * externalEvents[k] for k in range(len(externalEvents))
            ])
        if price <= 0:
            price = 0.05
        prices[idx] = price
        idx = (idx + 1) % 500
        #print("In market : "+str(prices[0:20]))
        print("[%d] Energy price = %.2f €/kWh" % (getpid(), price))
        externalEvents = [0, 0]
        buying = []
        selling = []

    queueToHome.remove()
    print("[%d] Market (main) : messageQueue removed" % getpid())
    print("[%d] Market (main) : Exit" % getpid())
Beispiel #6
0
#!/usr/bin/env python3
"""
Create a new private System-V IPC queue.
Return key + id

The IPC object should be available only to the creating
process or its child processes (e.g. those created with fork()).
"""

from __future__ import print_function
from sys import stderr, stdout
from sysv_ipc import MessageQueue, Semaphore, IPC_CREX


def eprint(*args, **kwargs):
    print(*args, file=stderr, **kwargs)


# create queue and semaphore
queue = MessageQueue(None, IPC_CREX)
sema1 = Semaphore(queue.key, IPC_CREX, initial_value=1)
sema2 = Semaphore(queue.key + 1, IPC_CREX, initial_value=1)
print(
    str(queue.key) + " " + str(queue.id) + " " + str(sema1.id) + " " +
    str(sema2.id))
Beispiel #7
0
def home(exitFlag,
         CR=int(130 * random()),
         PR=int(70 * random()),
         ETP=int(3 * random())):
    print("[%d] Home : Init" % getpid())
    print("[%d] Home : CR = %d\tPR = %d\tETP = %d" % (getpid(), CR, PR, ETP))

    try:
        queueToMarket = MessageQueue(marketKey)
        print("[%d] Home : connected to messageQueue" % getpid())
    except ExistentialError:
        print(
            "[%d] Home : can't connect to messageQueue, try launching Market before Home"
            % getpid())
        exitFlag.value = 1

    while not exitFlag.value:
        sleep(period)

        # besoin d'énergie: il en cherche auprès des maisons, sinon il achète au marché
        if CR > PR:
            energyNeeded = CR - PR

            sleep(
                timeDelay
            )  # petit délai pour que les receive() arrivent après les send()
            while energyNeeded > 0:
                try:
                    message, _ = queueToMarket.receive(block=False, type=3)
                    pid, quantity = message.decode().split(':')
                    energyNeeded -= int(quantity)
                    print("[%d] Home : received %d of energy" %
                          (getpid(), int(quantity)))

                    response = str(getpid()) + ":ACK"
                    queueToMarket.send(response.encode(), type=int(pid))
                    print("[%d] Home : ACK sent to %s" % (getpid(), pid))
                except (NotAttachedError, BusyError):
                    break
            if energyNeeded > 0:
                message = str(getpid()) + ':' + str(energyNeeded)
                queueToMarket.send(message.encode(), type=1)
                print("[%d] Home : bought %d of energy to the market" %
                      (getpid(), energyNeeded))
            else:
                print("[%d] Home : got %d of free energy" %
                      (getpid(), -energyNeeded))

        # trop d'énergie : il fait selon ETP
        elif PR > CR:
            energyBonus = PR - CR
            if ETP == 0:
                message = str(getpid()) + ':' + str(energyBonus)
                queueToMarket.send(message.encode(), type=3)
                print("[%d] Home : gave %d of energy" %
                      (getpid(), energyBonus))
            elif ETP == 1:
                message = str(getpid()) + ':' + str(energyBonus)
                queueToMarket.send(message.encode(), type=2)
                print("[%d] Home : sold %d of energy to the market" %
                      (getpid(), energyBonus))
            else:
                try:
                    message = str(getpid()) + ':' + str(energyBonus)
                    queueToMarket.send(message.encode(), type=3)
                    print(
                        "[%d] Home : gave %d of energy, waiting for response" %
                        (getpid(), energyBonus))
                    sleep(2 * timeDelay)
                    message, t = queueToMarket.receive(block=False,
                                                       type=getpid())
                    print("[%d] Home : response received" % getpid())
                except (NotAttachedError, BusyError):
                    message = str(getpid()) + ':' + str(energyBonus)
                    queueToMarket.send(message.encode(), type=2)
                    print(
                        "[%d] Home : no response, sold %d of energy to the market"
                        % (getpid(), energyBonus))
        else:
            print("[%d] Home : i'm autonomous !" % getpid())

    print("[%d] Home : Exit" % getpid())
Beispiel #8
0
    def __init__(
        self,
        shared_variables: SharedVariables,
        politics: int,
        economy: int,
        nb_houses: int,
        ipc_house: int,
        time_interval: int,
    ):
        super().__init__(shared_variables)

        # Political climate, rated from 0 to 100
        self.politics = Value("i")
        with self.politics.get_lock():
            self.politics.value = politics

        # Economics climate, rated from 0 to 100
        self.economy = Value("i")
        with self.economy.get_lock():
            self.economy.value = economy

        self.nb_houses = nb_houses  # Number of houses
        self.mq_house = MessageQueue(
            ipc_house)  # Message queue to communicate with houses
        self.daily_consumption = Value(
            "d")  # Total consumption of the houses on this day
        self.surplus = Value("d")  # Surplus of production
        self.waiting_houses = collections.deque()  # Free energy waiting queue
        self.waiting_lock = multiprocessing.Lock()  # Lock to access this queue

        # Set default values
        with self.daily_consumption.get_lock():
            self.daily_consumption.value = 0
        with self.surplus.get_lock():
            self.surplus.value = 0

        self.workers = 5

        # Coefficients for energy price
        self.gamma = 0.98
        self.alpha = [0.0001, 0.0001, 0.000001]
        self.beta = [0.025, 0.025, 0.025]

        # Politics : score between 0 and 100.
        # SIGUSR1 : politics situation deteriorates
        # SIGUSR2 : economics situation deteriorates
        self.market_pid = os.getpid()
        self.politics_process = ExternalFactor(
            ppid=self.market_pid,
            name="politics",
            signal_code=signal.SIGUSR1,
            delay=time_interval * 6,
        )
        self.economics_process = ExternalFactor(
            ppid=self.market_pid,
            name="economics",
            signal_code=signal.SIGUSR2,
            delay=time_interval * 7,
        )
        self.economics_process.start()
        self.politics_process.start()

        # Listen for signals
        signal.signal(signal.SIGUSR1, self.signal_handler)
        signal.signal(signal.SIGUSR2, self.signal_handler)