peb — is an open-source php extension to run php as an Erlang cnode. 13 | 14 | It's similar to the traditional php mysql function , you can communicate with Erlang node easy. 15 |
16 | 17 |21 |
35 |
36 |peb_close — Close Erlang node connection
13 | 14 |24 | peb_close() closes the non-persistent connection to 25 | the Erlang node that's associated with the specified link identifier. If 26 | link_identifier 27 | isn't specified, the last opened 28 | link is used. 29 |
30 |36 |
The Erlang node connection. If the 44 | link identifier is not specified, the last link opened by 45 | peb_connect() is assumed.
50 |
51 |57 | Returns TRUE on success or FALSE on failure. 58 |
59 |65 |
Example #1 peb_close() example
67 |
69 | <?php
$link = peb_connect('node@host.domain', 'secret_cookie');
if (!$link) {
die('Could not connect: ' . peb_error());
}
echo 'Connected successfully';
peb_close($link);
?>
70 |
71 | The above example will output:
77 | Connected successfully 78 |
81 |
82 |peb_error — Returns the text of the error message from previous peb operation
13 | 14 |23 | Returns the error text from the last peb function. 24 | Note that this function only returns the 25 | error text from the most recently executed peb function (not 26 | including peb_error() and 27 | peb_errno()), so if you want to use it, make 28 | sure you check the value before calling another peb function. 29 |
30 |36 |
44 |
45 |
46 |52 | Returns the error text from the last peb function, or 53 | '' (empty string) if no error occurred. 54 |
55 |61 |
Example #1 peb_error() example
63 |
65 | <?php
$link = peb_connect('node@host.domain', 'secret_cookie');
if (!$link) {
die('Could not connect: ['
66 | .
67 | peb_errno()
68 | .
69 | ']'
70 | .
71 | peb_error()
72 |
73 | );
}
echo 'Connected successfully';
peb_close($link);
?>
74 |
75 | The above example will output 79 | something similar to:
82 | Could not connect: [2] ei_connect error 83 |
86 |
87 |93 |
96 |
97 |peb_errno — Returns the numerical value of the error message from previous peb operation
13 | 14 |23 | Returns the error number from the last peb function. 24 |
25 |26 | Returns the error text from the last peb function. 27 | Note that this function only returns the 28 | error text from the most recently executed peb function (not 29 | including peb_error() and 30 | peb_errno()), so if you want to use it, make 31 | sure you check the value before calling another peb function. 32 |
33 |39 |
47 |
48 |
49 |55 | Returns the error number from the last peb function, or 56 | 0 (zero) if no error occurred. 57 |
58 |64 |
Example #1 peb_errno() example
66 |
68 | <?php
$link = peb_connect('node@host.domain', 'secret_cookie');
if (!$link) {
die('Could not connect: ['
69 | .
70 | peb_errno()
71 | .
72 | ']'
73 | .
74 | peb_error()
75 |
76 | );
}
echo 'Connected successfully';
peb_close($link);
?>
77 |
78 | The above example will output 82 | something similar to:
85 | Could not connect: [2] ei_connect error 86 |
89 |
90 |96 |
99 |
100 |peb_connect — Open a connection to an Erlang node
14 | 15 |26 | Open a connection to an Erlang node. 27 |
28 |34 |
44 | The Erlang node. e.g. "nodename@host.domain" 45 |
46 |57 | The cookie for communicate with this Erlang node. 58 |
59 |63 |
64 |70 | Returns an Erlang node link identifier on success, or FALSE on failure. 71 |
72 |Example #1 peb_connect() example
79 |80 |
83 | <?php
$link = peb_connect('node@host.domain', 'secret_cookie');
if (!$link) {
die('Could not connect: ' . peb_error());
}
echo 'Connected successfully';
peb_close($link);
?>
84 |
85 | The above example will output:
91 | Connected successfully 92 |
95 |
96 |108 |Note: 102 | 103 | The link to the server will be closed as soon as the execution of 104 | the script ends, unless it's closed earlier by explicitly calling 105 | peb_close(). 106 |
107 |
114 |
118 |
119 |peb_linkinfo — Get information about the link
13 | 14 |24 | Returns an array that contain detailed information about the link. 25 |
26 |32 |
The Erlang node connection. If the 40 | link identifier is not specified, the last link opened by 41 | peb_connect() is assumed.
46 |
47 |53 | Returns information about the link on success, or FALSE on 54 | failure.
55 |61 |
Example #1 peb_error() example
63 |
65 | <?php
$link = peb_connect('sadly-desktop@sadly-desktop', 'secret_cookie');
if (!$link) {
die('Could not connect: ['
66 | .
67 | peb_errno()
68 | .
69 | ']'
70 | .
71 | peb_error()
72 |
73 | );
}
74 |
75 | $ret = peb_linkinfo($link);
76 |
77 | print_r($ret);
78 |
79 |
80 | peb_close($link);
?>
81 |
82 | The above example will output:
88 | Array 89 | ( 90 | [thishostname] => sadly-desktop 91 | [thisnodename] => peb_client_8822_12@sadly-desktop 92 | [thisalivename] => peb_client_8822_12 93 | [connectcookie] => secret_cookie 94 | [creation] => 12 95 | [is_persistent] => 0 96 | ) 97 | 98 |
101 |
102 | 103 |108 |
112 |
113 |peb_pconnect — Open a persistent connection to an Erlang node
13 | 14 |26 | Establishes a persistent connection to an Erlang node. 27 |
28 | 29 |30 | peb_pconnect() acts very much like 31 | peb_connect() with two major differences. 32 |
33 |34 | First, when connecting, the function would first try to find a 35 | (persistent) link that's already open with the same server, 36 | cookie and creation. If one is found, an identifier for it 37 | will be returned instead of opening a new connection. 38 |
39 |40 | Second, the connection to the Erlang node will not be closed when 41 | the execution of the script ends. Instead, the link will remain 42 | open for future use. 43 |
44 |45 | This type of link is therefore called 'persistent'. 46 |
47 |53 |
63 | The Erlang node. e.g. "nodename@host.domain" 64 |
65 |76 | The cookie for communicate with this Erlang node. 77 |
78 |82 |
83 |89 | Returns an Erlang node persistent link identifier on success, or FALSE on 90 | failure. 91 |
92 |Example #1 peb_pconnect() example
99 |100 |
103 | <?php
$link = peb_pconnect('node@host.domain', 'secret_cookie');
if (!$link) {
die('Could not connect: ' . peb_error());
}
echo 'Connected successfully';
?>
104 |
105 | The above example will output:
111 | Connected successfully 112 |
115 |
116 |128 | 129 |Note: 123 | 124 | Note, that these kind of links only work if you are using 125 | a module version of PHP. 126 |
127 |
135 |
139 |
140 |peb_send_byname — Send an Erlang message by process name
13 | 14 |27 | peb_send_byname() send an Erlang message to the Erlang node 28 | that's associated with the specified link identifier. If 29 | link_identifier 30 | isn't specified, the last opened 31 | link is used. 32 |
33 |39 |
The registered process name on the Erlang node. 47 |
An Erlang message term encoded by peb_encode(). 56 |
The Erlang node connection. If the 64 | link identifier is not specified, the last link opened by 65 | peb_connect() is assumed. 66 |
71 |
72 |78 | Returns TRUE on success or FALSE on failure. 79 |
80 |86 |
Example #1 peb_send_byname() example
88 |
91 | <?php
92 |
$link = peb_connect('node@host.domain', 'secret_cookie');
93 |
if (!$link) {
94 |
die('Could not connect: ' . peb_error());
95 |
}
96 |
97 |
$msg = peb_encode('[~a,~a]', array(
98 |
array( 'hello', 'friend' )
99 |
)
100 |
);
101 |
peb_send_byname('pong',$msg,$link);
102 |
103 |
peb_close($link);
104 |
?>
105 |
106 | pong.erl example code
110 |
112 |
113 |
114 | -module( pong ).
115 | -export( [ start/0, pong/0 ] ).
116 |
117 | start() ->
118 | Mypid = spawn( pong, pong, [ ] ),
119 | register( pong, Mypid).
120 |
121 | pong() ->
122 | receive
123 | quit -> ok;
124 | X ->
125 | io:fwrite( "Got ~p.~n", [ X ] ),
126 | pong()
127 | end.
128 |
129 |
130 |
131 | will output (on Erlang side):
135 | Got [hello,friend]. 136 |
139 |
140 |146 |
150 |
151 |peb_decode — Decode an Erlang message term
14 | 15 |25 | Encode an Erlang message term. 26 |
27 |33 |
The resource identifier to an Erlang message received by 41 | peb_receive().
43 |
44 |50 | Returns an array that contain all data in message, or FALSE on failure. 51 |
52 |Example #1 peb_decode() example
59 |60 |
64 | <?php
$link = peb_connect('sadly-desktop@sadly-desktop', 'secret');
if (!$link) {
die('Could not connect: ' . peb_error());
}
$msg = peb_encode('[~p,~a]', array(
array($link,'getinfo')
)
);
//The sender must include a reply address. use ~p to format a link identifier to a valid Erlang pid.
peb_send_byname('pong',$msg,$link);
$message = peb_receive($link);
$rs= peb_decode( $message) ;
print_r($rs);
$serverpid = $rs[0][0];
$message = peb_encode('[~s]', array(
array( 'how are you')
)
);
peb_send_bypid($serverpid,$message,$link);
//just demo for how to use peb_send_bypid
peb_close($link);
?>
65 |
66 |
67 | pong.erl example code
71 |
73 |
74 |
75 | -module( pong ).
76 | -export( [ start/0, pong/0 ] ).
77 |
78 | start() ->
79 | Mypid = spawn( pong, pong, [ ] ),
80 | register( pong, Mypid).
81 |
82 | pong() ->
83 | receive
84 | [Pid,getinfo] -> Pid! [self(),welcome],
85 | io:fwrite( "Got ~p.~n", [Pid] ),
86 | pong();
87 | quit -> ok;
88 | X ->
89 | io:fwrite( "Got ~p.~n", [ X ] ),
90 | pong()
91 | end.
92 |
93 |
94 |
95 | will output (on Erlang side):
100 | Got <7160.0.0>. 101 | Got ["how are you"]. 102 |
will output (on php side):
107 | Array 108 | ( 109 | [0] => Array 110 | ( 111 | [0] => Resource id #5 112 | [1] => welcome 113 | ) 114 | 115 | ) 116 |
121 |
122 |128 |
132 |
133 |peb_receive — receive an Erlang message from Erlang node
13 | 14 |24 | peb_receive() receive an Erlang message from the Erlang node that's associated with the specified link identifier. If 25 | link_identifier 26 | isn't specified, the last opened link is used. 27 |
28 |34 |
The Erlang node connection. If the 42 | link identifier is not specified, the last link opened by 43 | peb_connect() is assumed.
48 |
49 |55 | Returns an Erlang message identifier on success, or FALSE on failure. 56 |
57 |63 |
Example #1 peb_receive() example
65 |
68 | <?php
$link = peb_connect('sadly-desktop@sadly-desktop', 'secret');
if (!$link) {
die('Could not connect: ' . peb_error());
}
$msg = peb_encode('[~p,~a]', array(
array($link,'getinfo')
)
);
//The sender must include a reply address. use ~p to format a link identifier to a valid Erlang pid.
peb_send_byname('pong',$msg,$link);
$message = peb_receive($link);
$rs= peb_decode( $message) ;
print_r($rs);
$serverpid = $rs[0][0];
$message = peb_encode('[~s]', array(
array( 'how are you')
)
);
peb_send_bypid($serverpid,$message,$link);
//just demo for how to use peb_send_bypid
peb_close($link);
?>
69 |
70 | pong.erl example code
74 |
76 |
77 |
78 | -module( pong ).
79 | -export( [ start/0, pong/0 ] ).
80 |
81 | start() ->
82 | Mypid = spawn( pong, pong, [ ] ),
83 | register( pong, Mypid).
84 |
85 | pong() ->
86 | receive
87 | [Pid,getinfo] -> Pid! [self(),welcome],
88 | io:fwrite( "Got ~p.~n", [Pid] ),
89 | pong();
90 | quit -> ok;
91 | X ->
92 | io:fwrite( "Got ~p.~n", [ X ] ),
93 | pong()
94 | end.
95 |
96 |
97 |
98 | will output (on Erlang side):
103 | Got <7160.0.0>. 104 | Got ["how are you"]. 105 |
will output (on php side):
110 | Array 111 | ( 112 | [0] => Array 113 | ( 114 | [0] => Resource id #5 115 | [1] => welcome 116 | ) 117 | 118 | ) 119 |
124 |
125 |peb_send_bypid — Send an Erlang message by process ID
13 | 14 |27 | peb_send_bypid() send an Erlang message to the Erlang node 28 | that's associated with the specified link identifier. If 29 | link_identifier 30 | isn't specified, the last opened 31 | link is used. 32 |
33 |39 |
The process ID on the Erlang node. 47 |
An Erlang message term encoded by peb_encode(). 56 |
The Erlang node connection. If the 64 | link identifier is not specified, the last link opened by 65 | peb_connect() is assumed. 66 |
71 |
72 |78 | Returns TRUE on success or FALSE on failure. 79 |
80 |86 |
Example #1 peb_send_bypid() example
88 |
91 | <?php
$link = peb_connect('sadly-desktop@sadly-desktop', 'secret');
if (!$link) {
die('Could not connect: ' . peb_error());
}
$msg = peb_encode('[~p,~a]', array(
array($link,'getinfo')
)
);
//The sender must include a reply address. use ~p to format a link identifier to a valid Erlang pid.
peb_send_byname('pong',$msg,$link);
$message = peb_receive($link);
$rs= peb_decode( $message) ;
print_r($rs);
$serverpid = $rs[0][0];
$message = peb_encode('[~s]', array(
array( 'how are you')
)
);
peb_send_bypid($serverpid,$message,$link);
//just demo for how to use peb_send_bypid
peb_close($link);
?>
92 |
93 | pong.erl example code
97 |
99 |
100 |
101 | -module( pong ).
102 | -export( [ start/0, pong/0 ] ).
103 |
104 | start() ->
105 | Mypid = spawn( pong, pong, [ ] ),
106 | register( pong, Mypid).
107 |
108 | pong() ->
109 | receive
110 | [Pid,getinfo] -> Pid! [self(),welcome],
111 | io:fwrite( "Got ~p.~n", [Pid] ),
112 | pong();
113 | quit -> ok;
114 | X ->
115 | io:fwrite( "Got ~p.~n", [ X ] ),
116 | pong()
117 | end.
118 |
119 |
120 |
121 | will output (on Erlang side):
126 | Got <7160.0.0>. 127 | Got ["how are you"]. 128 |
will output (on php side):
133 | Array 134 | ( 135 | [0] => Array 136 | ( 137 | [0] => Resource id #5 138 | [1] => welcome 139 | ) 140 | 141 | ) 142 |
147 |
148 |154 |
158 |
159 |peb_encode — Encode an Erlang message term
14 | 15 |26 | Encode an Erlang message term. 27 |
28 |34 |
44 | The format string is composed of one or more directives.
45 |
46 | ~a - an atom 47 | ~s - a string 48 | ~b - a binary (contain 0x0 in string) 49 | ~i - an integer 50 | ~l - a long integer 51 | ~u - an unsigned long integer 52 | ~f - a float 53 | ~d - a double float 54 | ~p - a php erlang bridge (get by peb_connect) 55 | ~P - an erlang pid 56 |57 | 58 |
68 | The data to send to Erlang node. 69 | Initial wrapped with an array, tuple and list data must be wrapped with extra dimension. 70 |
71 |75 |
76 |82 | Returns an Erlang message identifier on success, or FALSE on failure. 83 |
84 |Example #1 peb_encode() example
91 |92 |
97 | <?php
98 |
99 |
$msg = peb_encode('~a', array(
100 |
'hello'
101 |
)
102 |
);
103 |
//encode a simple atom
104 |
105 |
$msg = peb_encode('~d', array(
106 |
3.1415926
107 |
)
108 |
);
109 |
//encode a simple double
110 |
111 |
$msg = peb_encode('[~a]', array(
112 |
array('hello')
113 |
)
114 |
);
115 |
//encode a list with only one element
116 |
117 |
$msg = peb_encode('[~a,~d]', array(
118 |
array( 'hello', 3.1415926)
119 |
)
120 |
121 |
);
122 |
//encode a list with two element
123 |
124 |
125 |
$msg = peb_encode('{~a}', array(
126 |
array( 'hello')
127 |
)
128 |
);
129 |
//encode a tuple with only one element
130 |
131 |
$msg = peb_encode('{~a,~i}', array(
132 |
array( 'hello', 1234 )
133 |
)
134 |
);
135 |
//encode a list with two element
136 |
137 |
$msg = peb_encode('{~i,{~a,[~s,~d]}}', array(
138 |
array( 1234,
139 |
array('pi',
140 |
array('is',3.1415926)
141 |
)
142 |
)
143 |
)
144 |
);
145 |
//a complex data encode
146 |
?>
147 |
148 |
149 | 153 |
154 |160 |
165 |
166 |