1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- This document was generated using DocBuilder 3.3.3 -->
<HTML>
<HEAD>
<TITLE>ei_connect</TITLE>
<SCRIPT type="text/javascript" src="../../../../doc/erlresolvelinks.js">
</SCRIPT>
<STYLE TYPE="text/css">
<!--
.REFBODY { margin-left: 13mm }
.REFTYPES { margin-left: 8mm }
-->
</STYLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#FF00FF"
ALINK="#FF0000">
<!-- refpage -->
<CENTER>
<A HREF="http://www.erlang.se">
<IMG BORDER=0 ALT="[Ericsson AB]" SRC="min_head.gif">
</A>
<H1>ei_connect</H1>
</CENTER>
<H3>C LIBRARY</H3>
<DIV CLASS=REFBODY>
ei_connect
</DIV>
<H3>C LIBRARY SUMMARY</H3>
<DIV CLASS=REFBODY>
Communicate with distributed erlang
</DIV>
<H3>DESCRIPTION</H3>
<DIV CLASS=REFBODY>
<P> This module enables C programs to communicate with erlang nodes,
using the erlang distribution over TCP/IP.
<P> A C node appears to Erlang as a
<STRONG>hidden node</STRONG>.
That is, Erlang processes that know the name of the
C node are able to communicate with it in a normal manner, but
the node name will not appear in the listing provided by the
Erlang function <CODE>nodes/0</CODE>.
</DIV>
<H3>Timeout functions</H3>
<DIV CLASS=REFBODY>
<P> Most functions appear in a version with the suffix
<CODE>_tmo</CODE> appended to the function name. Those function take
an additional argument, a timeout in <STRONG>milliseconds</STRONG>. The
semantics is this; for each communication primitive involved in
the operation, if the primitive does not complete within the time
specified, the function will return an error and
<CODE>erl_errno</CODE> will be set to <CODE>ETIMEDOUT</CODE>. With
communication primitive is ment an operation on the socket, like
<CODE>connect</CODE>, <CODE>accept</CODE>, <CODE>recv</CODE> or <CODE>send</CODE>.
<P>Obviously the timeouts are for implementing fault tolerance,
not to keep hard realtime promises. The <CODE>_tmo</CODE> functions
are for detecting non-responsive peers and to avoid blocking on
socket operations.
<P>A timeout value of <CODE>0</CODE> (zero), means that timeouts are
disabled. Calling a <CODE>_tmo</CODE>-function with the last argument as
<CODE>0</CODE> is therefore exactly the same thing as calling the
function without the <CODE>_tmo</CODE> suffix.
<P>As with all other ei functions, you are <STRONG>not</STRONG> expected
to put the socket in non blocking mode yourself in the program. Every
use of non blocking mode is embedded inside the timeout
functions. The socket will always be back in blocking mode after
the operations are completed (regardless of the result). To
avoid problems, leave the socket options alone. Ei will handle
any socket options that need modification.
<P>In all other senses, the <CODE>_tmo</CODE> functions inherit all
the return values and the semantics from the functions without
the <CODE>_tmo</CODE> suffix.
</DIV>
<H3>EXPORTS</H3>
<P><A NAME="ei_connect_init/4"><STRONG><CODE>int ei_connect_init(ei_cnode* ec, const char*
this_node_name, const char *cookie, short creation)</CODE></STRONG></A><BR>
<A NAME="ei_connect_xinit/7"><STRONG><CODE>int ei_connect_xinit(ei_cnode* ec, const char *thishostname,
const char *thisalivename, const char *thisnodename,
Erl_IpAddr thisipaddr, const char *cookie,
short creation)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P> These function initializes the <CODE>ec</CODE> structure, to
identify the node name and cookie of the server. One of them
has to be called before other functions that works on the
type <CODE>ei_cnode</CODE> or a file descriptor accociated with a
connection to another node are used.
<P> <CODE>ec</CODE> is a structure containing information about the
C-node. It is used in other <CODE>ei</CODE> functions for
connecting and receiving data.
<P> <CODE>this_node_name</CODE> is the registered name of the process
(the name before '@').
<P> <CODE>cookie</CODE> is the cookie for the node.
<P> <CODE>creation</CODE> identifies a specific instance of a C
node. It can help prevent the node from receiving messages
sent to an earlier process with the same registered name.
<P> <CODE>thishostname</CODE> is the name of the machine we're running
on. If long names are to be used, it should be fully
qualified (i.e. <CODE>durin.erix.ericsson.se</CODE> instead of
<CODE>durin</CODE>).
<P> <CODE>thisalivename</CODE> is the registered name of the process.
<P> <CODE>thisnodename</CODE> is the full name of the node,
i.e. <CODE>einode@durin</CODE>.
<P> <CODE>thispaddr</CODE> if the IP address of the host.
<P> A C node acting as a server will be assigned a creation
number when it calls <CODE>ei_publish()</CODE>.
<P> A connection is closed by simply closing the socket. Refer
to system documentation to close the socket gracefully (when
there are outgoing packets before close).
<P> This function return a negative value indicating that an error
occurred.
<P> Example 1:
<PRE>
int n = 0;
struct in_addr addr;
ei_cnode ec;
addr = inet_addr("150.236.14.75");
if (ei_connect_xinit(&ec,
"chivas",
"madonna",
"madonna@chivas.du.etx.ericsson.se",
&addr;
"cookie..."),
n++) < 0) {
fprintf(stderr,"ERROR when initializing: %d",erl_errno);
exit(-1);
}
</PRE>
<P> Example 2:
<PRE>
if (ei_connect_init(&ec, "madonna", "cookie...", n++) < 0) {
fprintf("ERROR when initializing: %d",erl_errno);
exit(-1);
}
</PRE>
</DIV>
<P><A NAME="ei_connect/2"><STRONG><CODE>int ei_connect(ei_cnode* ec, char *nodename)</CODE></STRONG></A><BR>
<A NAME="ei_xconnect/3"><STRONG><CODE>int ei_xconnect(ei_cnode* ec, Erl_IpAddr adr, char *alivename)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P> These functions set up a connection to an Erlang node.
<P> <CODE>ei_xconnect()</CODE> requires the IP address of the remote
host and the alive name of the remote node
to be specified. <CODE>ei_connect()</CODE> provides an alternative
interface, and determines the information from the node name
provided.
<P> <CODE>addr</CODE> is the 32-bit IP address of the remote host.
<P> <CODE>alive</CODE> is the alivename of the remote node.
<P> <CODE>node</CODE> is the name of the remote node.
<P> These functions return an open file descriptor on success, or
a negative value indicating that an error occurred --- in
which case they will set <CODE>erl_errno</CODE> to one of:
<P>
<DL>
<DT>
<CODE>EHOSTUNREACH</CODE>
</DT>
<DD>
The remote host <CODE>node</CODE> is unreachable
</DD>
<DT>
<CODE>ENOMEM</CODE>
</DT>
<DD>
No more memory available.
</DD>
<DT>
<CODE>EIO</CODE>
</DT>
<DD>
I/O error.
</DD>
</DL>
<P> Additionally, <CODE>errno</CODE> values from
<CODE>socket</CODE><STRONG>(2)</STRONG> and <CODE>connect</CODE><STRONG>(2)</STRONG>
system calls may be propagated into <CODE>erl_errno</CODE>.
<P> Example:
<PRE>
#define NODE "madonna@chivas.du.etx.ericsson.se"
#define ALIVE "madonna"
#define IP_ADDR "150.236.14.75"
/*** Variant 1 ***/
int fd = ei_connect(&ec, NODE);
/*** Variant 2 ***/
struct in_addr addr;
addr = inet_addr(IP_ADDR);
fd = ei_xconnect(&ec, &addr, ALIVE);
</PRE>
</DIV>
<P><A NAME="ei_connect_tmo/3"><STRONG><CODE>int ei_connect_tmo(ei_cnode* ec, char
*nodename, unsigned timeout_ms)</CODE></STRONG></A><BR>
<A NAME="ei_xconnect_tmo/4"><STRONG><CODE>int ei_xconnect_tmo(ei_cnode* ec, Erl_IpAddr
adr, char *alivename, unsigned timeout_ms)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P> ei_connect and ei_xconnect with an optional timeout argument,
see the description at the beginning of this document.
</DIV>
<P><A NAME="ei_receive/3"><STRONG><CODE>int ei_receive(int fd, unsigned
char* bufp, int bufsize)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P> This function receives a message consisting of a sequence
of bytes in the Erlang external format.
<P> <CODE>fd</CODE> is an open descriptor to an Erlang connection. It
is obtained from a previous <CODE>ei_connect</CODE> or
<CODE>ei_accept</CODE>.
<P> <CODE>bufp</CODE> is a buffer large enough to hold the expected
message.
<P> <CODE>bufsize</CODE> indicates the size of <CODE>bufp</CODE>.
<P> If a <STRONG>tick</STRONG> occurs, i.e., the Erlang node on the
other end of the connection has polled this node to see if it
is still alive, the function will return <CODE>ERL_TICK</CODE> and
no message will be placed in the buffer. Also,
<CODE>erl_errno</CODE> will be set to <CODE>EAGAIN</CODE>.
<P> On success, the message is placed in the specified buffer
and the function returns the number of bytes actually read. On
failure, the function returns <CODE>ERL_ERROR</CODE> and will set
<CODE>erl_errno</CODE> to one of:
<P>
<DL>
<DT>
<CODE>EAGAIN</CODE>
</DT>
<DD>
Temporary error: Try again.
</DD>
<DT>
<CODE>EMSGSIZE</CODE>
</DT>
<DD>
Buffer too small.
</DD>
<DT>
<CODE>EIO</CODE>
</DT>
<DD>
I/O error.
</DD>
</DL>
</DIV>
<P><A NAME="ei_receive_tmo/4"><STRONG><CODE>int ei_receive_tmo(int fd, unsigned
char* bufp, int bufsize, unsigned timeout_ms)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P> ei_receive with an optional timeout argument,
see the description at the beginning of this document.
</DIV>
<P><A NAME="ei_receive_msg/3"><STRONG><CODE>int ei_receive_msg(int fd, erlang_msg* msg,
ei_x_buff* x)</CODE></STRONG></A><BR>
<A NAME="ei_xreceive_msg/3"><STRONG><CODE>int ei_xreceive_msg(int fd, erlang_msg* msg,
ei_x_buff* x)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P> These functions receives a message to the buffer in
<CODE>x</CODE>. <CODE>ei_xreceive_msg</CODE> allows the buffer in
<CODE>x</CODE> to grow, but <CODE>ei_receive_msg</CODE> fails if the
message is bigger than the preallocated buffer in <CODE>x</CODE>.
<P> <CODE>fd</CODE> is an open descriptor to an Erlang connection.
<P> <CODE>msg</CODE> is a pointer to an <CODE>erlang_msg</CODE> structure
and contains information on the message received.
<P> <CODE>x</CODE> is buffer obtained from <CODE>ei_x_new</CODE>.
<P> On success, the function returns <CODE>ERL_MSG</CODE> and the
<CODE>msg</CODE> struct will be initialized. <CODE>erlang_msg</CODE>
is defined as follows:
<PRE>
typedef struct {
long msgtype;
erlang_pid from;
erlang_pid to;
char toname[MAXATOMLEN+1];
char cookie[MAXATOMLEN+1];
erlang_trace token;
} erlang_msg;
</PRE>
<P> <CODE>msgtype</CODE> identifies the type of message, and is one of
<CODE>ERL_SEND</CODE>, <CODE>ERL_REG_SEND</CODE>, <CODE>ERL_LINK</CODE>,
<CODE>ERL_UNLINK</CODE> and <CODE>ERL_EXIT</CODE>.
<P> If <CODE>msgtype</CODE> is <CODE>ERL_SEND</CODE> this indicates that an
ordinary send operation has taken place, and <CODE>msg->to</CODE>
contains the Pid of the recipient (the C-node). If
<CODE>type</CODE> is <CODE>ERL_REG_SEND</CODE> then a registered send
operation took place, and <CODE>msg->from</CODE> contains the Pid
of the sender.
<P> If <CODE>msgtype</CODE> is <CODE>ERL_LINK</CODE> or <CODE>ERL_UNLINK</CODE>, then
<CODE>msg->to</CODE> and <CODE>msg->from</CODE> contain the pids of the
sender and receipient of the link or unlink.
<P> If <CODE>msgtype</CODE> is <CODE>ERL_EXIT</CODE>, then this indicates that
a link has been broken. In this case, <CODE>msg->to</CODE> and
<CODE>msg->from</CODE> contain the pids of the linked processes.
<P> The return value is the same as for <CODE>ei_receive</CODE>, see
above.
</DIV>
<P><A NAME="ei_receive_msg_tmo/4"><STRONG><CODE>int ei_receive_msg_tmo(int fd, erlang_msg* msg,
ei_x_buff* x, unsigned imeout_ms)</CODE></STRONG></A><BR>
<A NAME="ei_xreceive_msg_tmo/4"><STRONG><CODE>int ei_xreceive_msg_tmo(int fd, erlang_msg* msg,
ei_x_buff* x, unsigned timeout_ms)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P> ei_receive_msg and ei_xreceive_msg with an optional timeout argument,
see the description at the beginning of this document.
</DIV>
<P><A NAME="ei_receive_encoded/5"><STRONG><CODE>int
ei_receive_encoded(int fd, char **mbufp, int *bufsz,
erlang_msg *msg, int *msglen)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P>This function is retained for compatibility with code
generated by the interface compiler and with code following
examples in the same application.
<P>In essence the function performs the same operation as
<CODE>ei_xreceive_msg</CODE>, but instead of using an ei_x_buff, the
function expects a pointer to a character pointer
(<CODE>mbufp</CODE>), where the character pointer should point to a
memory area allocated by <CODE>malloc</CODE>. The argument
<CODE>bufsz</CODE> should be a pointer to an integer containing the
exact size (in bytes) of the memory area. The function may
reallocate the memory area and will in such cases put the new
size in <CODE>*bufsz</CODE> and update <CODE>*mbufp</CODE>.
<P>Furthermore the function returns either ERL_TICK or the
<CODE>msgtype</CODE> field of the <CODE>erlang_msg *msg</CODE>. The actual
length of the message is put in <CODE>*msglen</CODE>. On error it
will return a value <CODE>< 0</CODE>.
<P>It is recommended to use ei_xreceive_msg instead when
possible, for the sake of readability. The function will
however be retained in the interface for compatibility and
will <STRONG>not</STRONG> be removed not be removed in future releases
without notice.
</DIV>
<P><A NAME="ei_receive_encoded_tmo/6"><STRONG><CODE>int
ei_receive_encoded_tmo(int fd, char **mbufp, int *bufsz,
erlang_msg *msg, int *msglen, unsigned timeout_ms)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P> ei_receive_encoded with an optional timeout argument,
see the description at the beginning of this document.
</DIV>
<P><A NAME="ei_send/4"><STRONG><CODE>int ei_send(int fd, erlang_pid* to, char* buf, int len)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P> This function sends an Erlang term to a process.
<P> <CODE>fd</CODE> is an open descriptor to an Erlang connection.
<P> <CODE>to</CODE> is the Pid of the intended recipient of the
message.
<P> <CODE>buf</CODE> is the buffer containing the term in binary
format.
<P> <CODE>len</CODE> is the length of the message in bytes.
<P> The function returns 0 if successful, otherwise -1, in the
latter case it will set <CODE>erl_errno</CODE> to <CODE>EIO</CODE>.
</DIV>
<P><A NAME="ei_send_tmo/5"><STRONG><CODE>int ei_send_tmo(int fd, erlang_pid* to, char*
buf, int len, unsigned timeout_ms)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P> ei_send with an optional timeout argument,
see the description at the beginning of this document.
</DIV>
<P><A NAME="ei_send_encoded/4"><STRONG><CODE>int ei_send_encoded(int fd, erlang_pid* to, char* buf, int len)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P>Works exactly as ei_send, the alternative name reained for
backward compatibility. The function will <STRONG>not</STRONG> be
removed without notice.
</DIV>
<P><A NAME="ei_send_encoded_tmo/5"><STRONG><CODE>int ei_send_encoded_tmo(int fd, erlang_pid* to, char*
buf, int len, unsigned timeout_ms)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P> ei_send_encoded with an optional timeout argument,
see the description at the beginning of this document.
</DIV>
<P><A NAME="ei_reg_send/5"><STRONG><CODE>int ei_reg_send(ei_cnode* ec, int fd,
char* server_name, char* buf, int len)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P> This function sends an Erlang term to a registered process.
<P> This function sends an Erlang term to a process.
<P> <CODE>fd</CODE> is an open descriptor to an Erlang connection.
<P> <CODE>server_name</CODE> is the registered name of the intended
recipient.
<P> <CODE>buf</CODE> is the buffer containing the term in binary
format.
<P> <CODE>len</CODE> is the length of the message in bytes.
<P> The function returns 0 if successful, otherwise -1, in the
latter case it will set <CODE>erl_errno</CODE> to <CODE>EIO</CODE>.
<P> Example, send the atom "ok" to the process "worker":
<PRE>
ei_x_buff x;
ei_x_new_with_version(&x);
ei_x_encode_atom(&x, "ok");
if (ei_reg_send(&ec, fd, x.buff, x.index) < 0)
handle_error();
</PRE>
</DIV>
<P><A NAME="ei_reg_send_tmo/6"><STRONG><CODE>int ei_reg_send_tmo(ei_cnode* ec, int fd,
char* server_name, char* buf, int len, unsigned timeout_ms)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P> ei_reg_send with an optional timeout argument,
see the description at the beginning of this document.
</DIV>
<P><A NAME="ei_send_reg_encoded/5"><STRONG><CODE>int ei_send_reg_encoded(int fd, const
erlang_pid *from, const char *to, const char *buf, int len)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P>This function is retained for compatibility with code
generated by the interface compiler and with code following
examples in the same application.
<P>The function works as <CODE>ei_reg_send</CODE> with one
exception. Instead of taking the <CODE>ei_cnode</CODE> as a first
argument, it takes a secod argument, an <CODE>erlang_pid</CODE>
which should be the process identifier of the sending process
(in the erlang distribution protocol).
<P>A suitable <CODE>erlang_pid</CODE> can be constructed from the
<CODE>ei_cnode</CODE> structure by the following example code:
<PRE>
ei_cnode ec;
erlang_pid *self;
int fd; /* the connection fd */
...
self = ei_self(&ec);
self->num = fd;
</PRE>
</DIV>
<P><A NAME="ei_send_reg_encoded_tmo/5"><STRONG><CODE>int ei_send_reg_encoded_tmo(int fd, const
erlang_pid *from, const char *to, const char *buf, int len)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P> ei_send_reg_encoded with an optional timeout argument,
see the description at the beginning of this document.
</DIV>
<P><A NAME="ei_rpc/7"><STRONG><CODE>int ei_rpc(ei_cnode *ec, int fd, char *mod,
char *fun, const char *argbuf, int argbuflen, ei_x_buff *x)</CODE></STRONG></A><BR>
<A NAME="ei_rpc_to/6"><STRONG><CODE>int ei_rpc_to(ei_cnode *ec, int fd, char *mod,
char *fun, const char *argbuf, int argbuflen)</CODE></STRONG></A><BR>
<A NAME="ei_rpc_from/5"><STRONG><CODE>int ei_rpc_from(ei_cnode *ec, int fd,
int timeout, erlang_msg *msg, ei_x_buff *x)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P> These functions support calling Erlang functions on remote nodes.
<CODE>ei_rpc_to()</CODE> sends an rpc request to a remote node and
<CODE>ei_rpc_from()</CODE> receives the results of such a call.
<CODE>ei_rpc()</CODE> combines the functionality of these two functions
by sending an rpc request and waiting for the results. See also
<CODE>rpc:call/4</CODE>.
<P> <CODE>ec</CODE> is the C-node structure previously initiated by a
call to <CODE>ei_connect_init()</CODE> or
<CODE>ei_connect_xinit()</CODE>
<P> <CODE>fd</CODE> is an open descriptor to an Erlang connection.
<P> <CODE>timeout</CODE> is the maximum time (in ms) to wait for
results. Specify <CODE>ERL_NO_TIMEOUT</CODE> to wait forever.
<CODE>ei_rpc()</CODE> will wait infinitely for the answer,
i.e. the call will never time out.
<P> <CODE>mod</CODE> is the name of the module containing the function
to be run on the remote node.
<P> <CODE>fun</CODE> is the name of the function to run.
<P> <CODE>argbuf</CODE> is a pointer to a buffer with an encoded
Erlang list, without a version magic number, containing the
arguments to be passed to the function.
<P> <CODE>argbuflen</CODE> is the length of the buffer containing the
encoded Erlang list.
<P> <CODE>msg</CODE> structure of type <CODE>erlang_msg</CODE> and contains
information on the message received. See <CODE>ei_receive_msg()</CODE>
for a description of the <CODE>erlang_msg</CODE> format.
<P> <CODE>x</CODE> points to the dynamic buffer that receives the
result. For for <CODE>ei_rpc()</CODE> this will be the result
without the version magic number. For <CODE>ei_rpc_from()</CODE>
the result will return a version magic number and a 2-tuple
<CODE>{rex,Reply}</CODE>.
<P> <CODE>ei_rpc()</CODE> returns the number of bytes in the result
on success and -1 on failure. <CODE>ei_rpc_from()</CODE> returns
number of bytes or one of <CODE>ERL_TICK</CODE>, <CODE>ERL_TIMEOUT</CODE>
and <CODE>ERL_ERROR</CODE> otherwise. When failing,
all three functions set <CODE>erl_errno</CODE> to one of:
<P>
<DL>
<DT>
<CODE>EIO</CODE>
</DT>
<DD>
I/O error.
</DD>
<DT>
<CODE>ETIMEDOUT</CODE>
</DT>
<DD>
Timeout expired.
</DD>
<DT>
<CODE>EAGAIN</CODE>
</DT>
<DD>
Temporary error: Try again.
</DD>
</DL>
<P> Example, check to see if an erlang process is alive:
<PRE>
int index = 0, is_alive;
ei_x_buff args, result;
ei_x_new(&result);
ei_x_new(&args);
ei_x_encode_list_header(&args, 1);
ei_x_encode_pid(&args, &check_pid);
ei_x_encode_empty_list(&args);
if (ei_rpc(&ec, fd, "erlang", "is_process_alive",
args.buff, args.index, &result) < 0)
handle_error();
if (ei_decode_version(result.buff, &index) < 0
|| ei_decode_bool(result.buff, &index, &is_alive) < 0)
handle_error();
</PRE>
</DIV>
<P><A NAME="ei_publish/2"><STRONG><CODE>int ei_publish(ei_cnode *ec, int port)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P> These functions are used by a server process to register
with the local name server <STRONG>epmd</STRONG>, thereby allowing
other processes to send messages by using the registered name.
Before calling either of these functions, the process should
have called <CODE>bind()</CODE> and <CODE>listen()</CODE> on an open socket.
<P> <CODE>ec</CODE> is the C-node structure.
<P> <CODE>port</CODE> is the local name to register, and should be the
same as the port number that was previously bound to the socket.
<P> <CODE>addr</CODE> is the 32-bit IP address of the local host.
<P> To unregister with epmd, simply close the returned
descriptor. See also <CODE>ei_unpublish()</CODE>.
<P> On success, the functions return a descriptor connecting the
calling process to epmd. On failure, they return -1 and set
<CODE>erl_errno</CODE> to <CODE>EIO</CODE>.
<P> Additionally, <CODE>errno</CODE> values from <CODE>socket</CODE><STRONG>(2)</STRONG>
and <CODE>connect</CODE><STRONG>(2)</STRONG> system calls may be propagated
into <CODE>erl_errno</CODE>.
</DIV>
<P><A NAME="ei_publish_tmo/3"><STRONG><CODE>int ei_publish_tmo(ei_cnode *ec, int port,
unsigned timeout_ms)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P> ei_publish with an optional timeout argument,
see the description at the beginning of this document.
</DIV>
<P><A NAME="ei_accept/3"><STRONG><CODE>int ei_accept(ei_cnode *ec, int listensock,
ErlConnect *conp)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P> This function is used by a server process to accept a
connection from a client process.
<P> <CODE>ec</CODE> is the C-node structure.
<P> <CODE>listensock</CODE> is an open socket descriptor on which
<CODE>listen()</CODE> has previously been called.
<P> <CODE>conp</CODE> is a pointer to an <CODE>ErlConnect</CODE> struct,
described as follows:
<PRE>
typedef struct {
char ipadr[4];
char nodename[MAXNODELEN];
} ErlConnect;
</PRE>
<P> On success, <CODE>conp</CODE> is filled in with the address and
node name of the connecting client and a file descriptor is
returned. On failure, <CODE>ERL_ERROR</CODE> is returned and
<CODE>erl_errno</CODE> is set to <CODE>EIO</CODE>.
</DIV>
<P><A NAME="ei_accept_tmo/4"><STRONG><CODE>int ei_accept_tmo(ei_cnode *ec, int listensock,
ErlConnect *conp, unsigned timeout_ms)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P> ei_accept with an optional timeout argument,
see the description at the beginning of this document.
</DIV>
<P><A NAME="ei_unpublish/1"><STRONG><CODE>int ei_unpublish(ei_cnode *ec)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P> This function can be called by a process to unregister a
specified node from epmd on the localhost. This may be
useful, for example, when epmd has not detected the failure of a
node, and will not allow the name to be reused. If you use this
function to unregister your own process, be sure to also close
the descriptor that was returned by <CODE>ei_publish()</CODE>.
<P>
<TABLE CELLPADDING=4>
<TR>
<TD VALIGN=TOP><IMG ALT="Note!" SRC="note.gif"></TD>
<TD>
<P>Careless use of this function may have unpredictable
results, if the registered node is in fact still running.
</TD>
</TR>
</TABLE>
<P> <CODE>ec</CODE> is the node structure of the node to unregister.
<P> If the node was successfully unregistered from epmd, the
function returns 0. Otherwise, it returns -1 and sets
<CODE>erl_errno</CODE> is to <CODE>EIO</CODE>.
</DIV>
<P><A NAME="ei_unpublish_tmo/2"><STRONG><CODE>int ei_unpublish_tmo(ei_cnode *ec, unsigned
timeout_ms)
</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P> ei_unpublish with an optional timeout argument,
see the description at the beginning of this document.
</DIV>
<P><A NAME="ei_thisnodename/1"><STRONG><CODE>const char *ei_thisnodename(ei_cnode *ec)</CODE></STRONG></A><BR>
<A NAME="ei_thishostname/1"><STRONG><CODE>const char *ei_thishostname(ei_cnode *ec)</CODE></STRONG></A><BR>
<A NAME="ei_thisalivename/1"><STRONG><CODE>const char *ei_thisalivename(ei_cnode *ec)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P> These functions can be used to retrieve information about
the C Node. These values are initially set with
<CODE>ei_connect_init()</CODE> or <CODE>ei_connect_xinit()</CODE>.
<P> They simply fetches the appropriate field from the <CODE>ec</CODE>
structure. Read the field directly will probably be safe for
a long time, so these functions are not really needed.
</DIV>
<P><A NAME="ei_self/1"><STRONG><CODE>erlang_pid *ei_self(ei_cnode *ec)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P> This function retrieves the Pid of the C-node. Every C-node
has a (pseudo) pid used in <CODE>ei_send_reg</CODE>, <CODE>ei_rpc</CODE>
and others. This is contained in a field in the <CODE>ec</CODE>
structure. It will be safe for a long time to fetch this
field directly from the <CODE>ei_cnode</CODE> structure.
</DIV>
<P><A NAME="*ei_gethostbyname/1"><STRONG><CODE>struct hostent *ei_gethostbyname(const char *name)</CODE></STRONG></A><BR>
<A NAME="*ei_gethostbyaddr/3"><STRONG><CODE>struct hostent *ei_gethostbyaddr(const char *addr, int len, int type)</CODE></STRONG></A><BR>
<A NAME="*ei_gethostbyname_r/5"><STRONG><CODE>struct hostent *ei_gethostbyname_r(const char *name,
struct hostent *hostp,
char *buffer,
int buflen,
int *h_errnop)</CODE></STRONG></A><BR>
<A NAME="*ei_gethostbyaddr_r/7"><STRONG><CODE>struct hostent *ei_gethostbyaddr_r(const char *addr,
int length,
int type,
struct hostent *hostp,
char *buffer,
int buflen,
int *h_errnop)</CODE></STRONG></A><BR>
<DIV CLASS=REFBODY>
<P> These are convinience functions for some common name lookup functions.
</DIV>
<H3>Debug Information</H3>
<DIV CLASS=REFBODY>
<P> If a connection attempt fails, the following can be checked:
<P>
<UL>
<LI>
<CODE>erl_errno</CODE>
</LI>
<LI>
that the right cookie was used
</LI>
<LI>
that <STRONG>epmd</STRONG> is running
</LI>
<LI>
the remote Erlang node on the other side is running the
same version of Erlang as the <CODE>ei</CODE>
library.
</LI>
</UL>
</DIV>
<H3>AUTHORS</H3>
<DIV CLASS=REFBODY>
Jakob Cederlund - support@erlang.ericsson.se<BR>
</DIV>
<CENTER>
<HR>
<SMALL>erl_interface 3.5.5.2<BR>
Copyright © 1991-2006
<A HREF="http://www.erlang.se">Ericsson AB</A><BR>
</SMALL>
</CENTER>
</BODY>
</HTML>
|