Skip to content

Commit

Permalink
Clean up: renamed modes, unified __command() and command().
Browse files Browse the repository at this point in the history
git-svn-id: http://voip.null.ro/svn/ansql/trunk@152 dbfed7de-b0aa-0410-b6a1-c7e608b77fc9
  • Loading branch information
dana committed Mar 30, 2015
1 parent 145fb68 commit 3ca78ea
Showing 1 changed file with 27 additions and 38 deletions.
65 changes: 27 additions & 38 deletions socketconn.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ class SocketConn

/*
* @param $mode String. Possible values:
* -> "send_close" - will send a command and will not wait for the respond=> send_close($command) will be used
* -> "send_write_close" - send a command, wait for the response and close the socket => send_write($command, $marker_end, $default_tries)
* -> "multiple_send_write" - send multiple command, wait for their responses and then close the socket =>
* command($command, $marker_end , $limited_tries) will be used with different commands and then close($this->socket)
*/

function __construct($ip = null, $port = null, $mode="send_write_close")
* -> "write_close" - will write command and close the socket
* -> "write_read" - write a command and read the response - this is the default mode that is set in the constructor
* -> "write_read_close" - write a command, wait for the response and then close the socket
* If multiple_write_read is needed the default mode can be used, by calling the method command() with the commands needed,
* after that be carreful to close the socket.
*/
function __construct($ip = null, $port = null, $mode="write_read")
{
Debug::func_start(__METHOD__,func_get_args(),"ansql");

Expand Down Expand Up @@ -86,10 +86,10 @@ function __construct($ip = null, $port = null, $mode="send_write_close")
}

/**
* Send one command through the socket
* Write command through the socket
* close the socket
*/
function send_close($command)
private function write_close($command)
{
Debug::func_start(__METHOD__,func_get_args(),"ansql");

Expand All @@ -98,43 +98,22 @@ function send_close($command)
}

/*
* Send commmand through the socket
* Write commmand through the socket
* Read from the socket the answer
* Close the socket
*/
function send_write_close($command, $marker_end, $default_tries)
private function write_read_close($command, $marker_end, $limited_tries)
{
Debug::func_start(__METHOD__,func_get_args(),"ansql");

$response = $this->command($txt_command, $marker_end, $default_tries);
$this->write($command);
$response = $this->read($marker_end,$limited_tries);

$this->close();
return $response;
}


/*
* Implements the mode set in constructor for given commands
*/
function __command($command, $marker_end, $default_tries)
{
Debug::func_start(__METHOD__,func_get_args(),"ansql");

if (!$this->mode)
return;
$response = "";
if ($this->mode == "send_write_close")
$this->send_write_close($command, $marker_end, $default_tries);
else if($this->mode == "send_close")
$this->send_close($command);
else if ($this->mode == "multiple_send_write") {
foreach ($command as $key => $single_command)
$response .= $this->command($single_command, $marker_end, $default_tries);
$this->close();
}

return $response;
}

function write($str)
{
Debug::func_start(__METHOD__,func_get_args(),"ansql");
Expand Down Expand Up @@ -195,10 +174,20 @@ function close()
function command($command, $marker_end = "\r\n", $limited_tries=false)
{
Debug::func_start(__METHOD__,func_get_args(),"ansql");

if ($this->mode == "write_read") {
// if after sending command to yate,
// the page seems to stall it might be because the generated message has not handled or retval was not set
$this->write($command);
return $this->read($marker_end,$limited_tries);

} elseif ($this->mode == "write_close") {
$this->write_close($command);

} elseif ($this->mode == "write_read_close") {
return $this->write_read_close($command, $marker_end, $limited_tries);
}

// if after sending command to yate, the page seems to stall it might be because the generated message has not handled or retval was not set
$this->write($command);
return $this->read($marker_end,$limited_tries);
}
}

Expand Down

0 comments on commit 3ca78ea

Please sign in to comment.