Semaphore

Hi,
I’m trying to use IPS_SemaphoreEnter and IPS_SemaphoreLeave but I am having trouble using it. I have two functions one is send and the other is received and I want to use semaphore to prevent send to be called untill received has received an answer. Is it possible to have IPS_SemaphoreEnter in one function and IPS_SemaphoreLeave in an other?

Yes, that should be possible. Semaphores are „Named“. Therefore, as long as you know the name you, you can „Leave“ a semaphore in the other thread.

paresy

Hi paresy,

Thanks for your answer but I think I need additional help, any chansen you could provide me with a code example (everything I have tried has failed)?

I am trying to send a command to my Sharp Aquos TV (I am using I/O Client Socket (AquosTV #33450)) and I am trying to create a module. In my module.php I have two functions;

private function buildAndSendCommand($Command)

public function ReceiveData($JSONString)

What I need is a semaphore that prevents buildAndSendCommand (and a new command sent) to be called again before I have a reply from ReceiveData.

eg. I send 'POWR1 ’ to turn the TV on and the TV replies ‚OK‘ for success and ‚ERR‘ if it fails.

Can this be achieved with semaphore ?

Thanks in advance!

yoggi

You can set a semaphore and a Buffer via SetBuffer. In ReceiveData then check the Buffer via GetBuffer and then release the semaphore again if the condition is meet.

Perhaps you can post what you have tried already and what failed so far.

Hi Fonzo,

Basically I can’t get semaphore to work att all, I have tried simple examples (two scripts, one that sets semaphore and on that releases it) but I can never get semaphore to prevent any action at all. I must be misunderstanding how the function works.

Hi IP Symcon creators,

I am about to give up, but before I do that I would like to ask the people behind IP Symcon, is there a good way to emulate a TCP Client Socket connection in blocking mode?

I have tried everything I can come up with but I can’t get it to work!

From what I understand blocking mode is normally the default (see quote and link www.scottklement.com/rpg/socktut/nonblocking.html)

„By default, TCP sockets are in „blocking“ mode. For example, when you call recv() to read from a stream, control isn’t returned to your program until at least one byte of data is read from the remote site. This process of waiting for data to appear is referred to as „blocking“. The same is true for the write() API, the connect() API, etc. When you run them, the connection „blocks“ until the operation is complete.“

So I ask, is there any chansen you could add blocking mode with timeout to the build in I/O instances, Client Socket?

Thanks in advance

yoggi

After this discussion I am using the following functions to set/unset/check IPS_Semaphore:


//---- Funktionen
function SetIPSsem ($Sem, $Repeat = 100) {
    $i = 10;
    do {
        $i ++;   // nothing
        if ($i > $Repeat)
            die ("
Abbruch: " . IPS_GetScriptFile ($_IPS['SELF']) . ': Time-Out von Semaphore ' . $Sem . " (" . IPS_GetLocation ($Sem) . ")");
    } while (IPS_SemaphoreEnter ((string) $Sem, $i) != true);
}

function unSetIPSsem ($Sem) {
    if (IPS_SemaphoreEnter ((string) $Sem, 1)) {
        IPS_SemaphoreLeave ((string) $Sem);
        return;
    }
    @IPS_SemaphoreLeave ((string) $Sem);
}

function GetIPSsem ($Sem) {
    if (IPS_SemaphoreEnter ((string) $Sem, 1)) {
        IPS_SemaphoreLeave ((string) $Sem);
        return false;
    } else {
        return true;
    }
}


Usage:


<?php

// Datenliste vorbereiten
$ListeNr = 47111;     // Object prevented to be accessed from other scripts (which must also use SetIPSsem (47111)
SetIPSsem ($ListeNr);   // abriegeln 
$Liste = unserialize (GetValueString ($ListeNr));


//...

    SetValueString ($ListeNr, serialize ($Liste));
IPS_SemaphoreLeave ($ListeNr);

?>

Harald

Hi,
I was hoping for an answer from the creators of IPS to my qestion (two or three post up).

Is it possible to emulate a blocking socket in a reliable way?

Could you consider adding blocking to the build I/O instances, Client Socket? A keep connection alive (a call to keep the connection alive and a timer) would also be nice.

Thanks in advance

Yoggi

It is not possible.

paresy

Hi paresy,

When you say that it is not possible, are you referring to emulate blocking socket call or if it could be added I/O instances, Client Socket in the future?

yoggi

We have no intend to add this feature. You can try to use blocking sockets within PHP: PHP: Sockets - Manual

What we might add is proper „signaling“. As far as i have understood you want to block the sending thread until the receiving thread signals that the data is available. Is that right?

paresy

Hi paresy,

I am looking at php socket right now but it is above my current understanding of php (but I am trying to learn).

You are correct, I need to block sending untill I have received message from previous sent command.

I think I can make it work if you add proper signaling function. It would be much appreciated if you could add this.

Will this required a lot of work on your behalf to add this function? I ask as I am curious about the potential timeline.

Thanks
yoggi