Cerl: PHP or Python server that calls C ++ implementation

Source: Internet
Author: User

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:

<? Php <br/> define ('hashsever _ put', 1); <br/> define ('hashsever _ get', 2 ); </P> <p> If (! Extension_loaded ('php _ cerl ') {<br/> If (! DL ('php _ cerl. '. php_shlib_suffix) <br/> die ("error: cannot load php_cerl library! /N "); <br/>}</P> <p> cerl_thread_init (); </P> <p> $ Server = cerl_connect ('localhost: 100 ', 'hashserver'); <br/> If (! $ Server) <br/> die ("error: connect hashsever failed! /N "); </P> <p> printf (<br/>" + ---------------------------------------------------------/N ". <br/> "| put <key> <value>-put (Key, value)/n ". <br/> "| get <key>-Get value of a key/N ". <br/> "| Q-Quit/N ". <br/> "+ ---------------------------------------------------------/N"); </P> <p> for (;) <br/>{< br/> echo "/n> "; <br/> List ($ cmd, $ key, $ value) = fscanf (stdin, '% S % d % D '); <br/> if ($ cmd = 'get' | | $ Cmd = 'G') <br/>{< br/> $ ret = $ server-> call (hashsever_get, $ key ); <br/> if ($ RET [0] = code_ OK) <br/> echo "Result: $ RET [1]/n "; <br/> else <br/> echo "error: $ RET [0]/n "; <br/>}< br/> else if ($ cmd = 'put' | $ cmd = 'P ') <br/>{< br/> $ ret = $ server-> call (hashsever_put, $ key, $ value ); <br/> if ($ RET [0] = code_ OK) <br/> echo "OK! /N "; <br/> else <br/> echo" error: $ RET [0]/n "; <br/>}< br/> else if ($ cmd = 'q') <br/>{< br/> break; <br/>}< br/> else <br/> {<br/> echo "Unknown command! /N "; <br/>}</P> <p> cerl_thread_term (); <br/>?> <Br/>

 

The following is a sample client written in Python:

Hashsever_put = 1 <br/> hashsever_get = 2 </P> <p> Import sys, pycerl </P> <p> pycerl. thread_init () </P> <p> Server = pycerl. connect ("localhost: 8889", "hashserver") <br/> if not server: <br/> Print "error: connect hashsever failed! "</P> <p> else: <br/> Print" + ------------------------------------------------------- "<br/> Print" | put <key> <value>-put (Key, value) "<br/> Print" | get <key>-Get value of a key "<br/> Print" | Q-Quit "<br/> Print" + ----------------------------------------------- "<br/> Print" | /P> <p> while true: </P> <p> sys. stdout. write ("/n>") </P> <p> input = sys. stdin. readline (). split () <br/> cmd = input [0] </P> <p> If cmd = "get" or cmd = "G": <br/> If Len (input) <2: <br/> continue <br/> key = int (input [1], 10) <br/> ret = server. call (hashsever_get, key) <br/> If RET [0] = pycerl. code_ OK: <br/> Print "Result:", RET [1] <br/> else: <br/> Print "error :", RET [0] </P> <p> Elif cmd = "put" or cmd = "p": <br/> If Len (input) <3: <br/> continue <br/> key = int (input [1], 10) <br/> value = int (input [2], 10) <br/> ret = server. c All (hashsever_put, key, value) <br/> If RET [0] = pycerl. code_ OK: <br/> Print "OK! "<Br/> else: <br/> Print" error: ", RET [0] </P> <p> Elif cmd =" Q ": <br/> Break </P> <p> else: <br/> Print "Unknown command! "</P> <p> pycerl. thread_term () <br/>

 

It is worth noting that we have not used a compiler to generate PHP/Python proxy code for SDL translation like C/C ++. This is the advantage of Dynamic Language. Even if there is no proxy, its code still looks very elegant (but speaking back, C ++ can actually do it, But it slightly loses some performance ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.