Two days on the weekend, I wrote a php_cerl module one day to complete the phpclient of cerl, and a pycerl module one day to complete the pythonclient of cerl. The C ++ server can be called using php or python. For the HashServer we previously wrote with CERL (for a very simple example, see Erlangvs.
Two days on the weekend, I wrote a php_cerl module one day to complete the php client of cerl. I wrote a pycerl module one day to complete the python client of cerl. The C ++ server can be called using php or python.
For the HashServer we previously wrote with CERL (for a very simple example, see Erlang vs. CERL-CERL introduction), the PHP Client sample is as follows:
View plaincopy to clipboardprint?
Define ('hashsever _ put', 1 );
Define ('hashsever _ get', 2 );
If (! Extension_loaded ('php _ cerl ')){
If (! Dl ('php _ cerl. '. PHP_SHLIB_SUFFIX ))
Die ("ERROR: Cannot load php_cerl library! \ N ");
}
Cerl_thread_init ();
$ Server = cerl_connect ('localhost: 100', 'hashserver ');
If (! $ Server)
Die ("ERROR: Connect HashSever failed! \ N ");
Printf (
"+ ----------------------------------------------------------- \ N ".
"| Put -Put (key, value) \ n ".
"| Get -Get value of a key \ n ".
"| Q-Quit \ n ".
"+ ----------------------------------------------------------- \ N ");
For (;;)
{
Echo "\ n> ";
List ($ cmd, $ key, $ value) = fscanf (STDIN, '% s % d % d ');
If ($ cmd = 'get' | $ cmd = 'g ')
{
$ Ret = $ server-> call (HashSever_get, $ key );
If ($ ret [0] = code_ OK)
Echo "result: $ ret [1] \ n ";
Else
Echo "error: $ ret [0] \ n ";
}
Else if ($ cmd = 'put' | $ cmd = 'p ')
{
$ Ret = $ server-> call (HashSever_put, $ key, $ value );
If ($ ret [0] = code_ OK)
Echo "OK! \ N ";
Else
Echo "error: $ ret [0] \ n ";
}
Else if ($ cmd = 'Q ')
{
Break;
}
Else
{
Echo "unknown command! \ N ";
}
}
Cerl_thread_term ();
?>
Define ('hashsever _ put', 1 );
Define ('hashsever _ get', 2 );
If (! Extension_loaded ('php _ cerl ')){
If (! Dl ('php _ cerl. '. PHP_SHLIB_SUFFIX ))
Die ("ERROR: Cannot load php_cerl library! \ N ");
}
Cerl_thread_init ();
$ Server = cerl_connect ('localhost: 100', 'hashserver ');
If (! $ Server)
Die ("ERROR: Connect HashSever failed! \ N ");
Printf (
"+ ----------------------------------------------------------- \ N ".
"| Put -Put (key, value) \ n ".
"| Get -Get value of a key \ n ".
"| Q-Quit \ n ".
"+ ----------------------------------------------------------- \ N ");
For (;;)
{
Echo "\ n> ";
List ($ cmd, $ key, $ value) = fscanf (STDIN, '% s % d % d ');
If ($ cmd = 'get' | $ cmd = 'g ')
{
$ Ret = $ server-> call (HashSever_get, $ key );
If ($ ret [0] = code_ OK)
Echo "result: $ ret [1] \ n ";
Else
Echo "error: $ ret [0] \ n ";
}
Else if ($ cmd = 'put' | $ cmd = 'p ')
{
$ Ret = $ server-> call (HashSever_put, $ key, $ value );
If ($ ret [0] = code_ OK)
Echo "OK! \ N ";
Else
Echo "error: $ ret [0] \ n ";
}
Else if ($ cmd = 'Q ')
{
Break;
}
Else
{
Echo "unknown command! \ N ";
}
}
Cerl_thread_term ();
?>
The following is a sample Client written in Python:
View plaincopy to clipboardprint?
HashSever_put = 1
HashSever_get = 2
Import sys, pycerl
Pycerl. thread_init ()
Server = pycerl. connect ("localhost: 8889", "HashServer ")
If not server:
Print "ERROR: Connect HashSever failed! "