1 /*
2  * %CopyrightBegin%
3  *
4  * Copyright Ericsson AB 1999-2016. All Rights Reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * %CopyrightEnd%
19  */
20 
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include "ei.h"
25 #include "erl_interface.h"
26 
27 #define MSGSIZE	13
28 
29 #define SELF(fd) erl_mk_pid(erl_thisnodename(),fd,0,erl_thiscreation())
30 
31 #ifdef VXWORKS
32 #define MAIN cnode
33 #else
34 #define MAIN main
35 #endif
36 
37 /* FIXME uses mix och ei and erl_interface */
38 
39 /*
40    A small cnode.
41    To be called from the test case erl_eterm_SUITE:cnode_1.
42 
43    1) Set up connection to node 'test_server' on the same host.
44    All sends are done to a registered process named 'mip'.
45    2) Create a long ref and send it.
46    3) Create a pid for ourselves and send it.
47    4) Receive a message.
48    5) Send back the message part of the message.
49    6) Send back the 'to' part of the message.
50    7) Exit.
51 */
52 
MAIN(int argc,char ** argv)53 MAIN(int argc, char **argv)
54 
55 {
56     unsigned char *msgbufp;
57     int msgsize;
58     ErlMessage msg;
59     char msgbuf[MSGSIZE];
60     char buf[100];
61     char buf1[100];
62     char buf2[100];
63     int ix;
64     int s;
65     int fd;
66     char node[80];
67     char server[80];
68     char host[80];
69     int number;
70     ETERM *ref, *ref1, *ref2;
71     FILE *dfile = fopen("cnode_debug_printout", "w");
72 
73     erl_init(NULL, 0);
74 
75     number = 1;
76     if (argc >= 2) {
77 	s = erl_connect_init(number, argv[1], 0);
78     } else {
79 	s = erl_connect_init(number, (char *) 0, 0);
80     }
81     gethostname(host, sizeof(host));
82     sprintf(node, "c%d@%s", number, host);
83 
84     fprintf(dfile, "s = %d\n", s); fflush(dfile);
85 
86     sprintf(server, "test_server@%s", host);
87     fd = erl_connect(server);
88     fprintf(dfile, "fd = %d\n", fd);
89 
90 /*    fprintf(dfile, "dist = %d\n", erl_distversion(fd)); */
91 
92 #if 1
93     ref = erl_mk_long_ref(node, 4711, 113, 98, 0);
94 #else
95     ref = erl_mk_ref(node, 4711, 0);
96 #endif
97     fprintf(dfile, "ref = %p\n", ref); fflush(dfile);
98 
99     s = erl_reg_send(fd, "mip", ref);
100     fprintf(dfile, "s = %d\n", s); fflush(dfile);
101 
102     {
103       ETERM* emsg;
104       emsg = SELF(fd);
105       fprintf(dfile, "pid = %p\n", emsg); fflush(dfile);
106       s = erl_reg_send(fd,"mip",emsg);
107       fprintf(dfile, "s2 = %d\n", s); fflush(dfile);
108       erl_free_term(emsg);
109     }
110 
111     msgsize = 4;
112     msgbufp = (unsigned char *) malloc(msgsize);
113 
114     do {
115 #if 0
116 	s = erl_receive_msg(fd, msgbuf, MSGSIZE, &msg);
117 #else
118 	s = erl_xreceive_msg(fd, &msgbufp, &msgsize, &msg);
119 #endif
120 	switch (s) {
121 	  case ERL_TICK:
122 	    fprintf(dfile, "tick\n");
123 	    break;
124 	  case ERL_ERROR:
125             fprintf(dfile, "error: %s (%d)\n", strerror(erl_errno), erl_errno);
126 	    break;
127 	  case ERL_MSG:
128 	    fprintf(dfile, "msg %d\n", msgsize);
129 	    break;
130 	  default:
131 	    fprintf(dfile, "unknown result %d\n", s);
132 	    break;
133 	}
134         fflush(dfile);
135     } while (s == ERL_TICK);
136 
137     s = erl_reg_send(fd, "mip", msg.msg);
138     fprintf(dfile, "s = %d\n", s); fflush(dfile);
139     s = erl_reg_send(fd, "mip", msg.to);
140     fprintf(dfile, "s = %d\n", s); fflush(dfile);
141 #if 0
142     /* from = NULL! */
143     s = erl_reg_send(fd, "mip", msg.from);
144     fprintf(dfile, "s = %d\n", s); fflush(dfile);
145 #endif
146 
147 #if 0
148     /* Unused code which tests refs in some ways. */
149     ix = 0;
150     s = ei_encode_term(buf, &ix, ref);
151     printf ("ei encode = %d, ix = %d\n", s, ix);
152 
153     /* Compare old and new ref equal */
154     ref1 = erl_mk_long_ref(node, 4711, 113, 98, 0);
155     ref2 = erl_mk_ref(node, 4711, 0);
156     s = erl_encode(ref1, buf1);
157     fprintf(dfile, "enc1 s = %d\n", s); fflush(dfile);
158     s = erl_encode(ref2, buf2);
159     fprintf(dfile, "enc2 s = %d\n", s); fflush(dfile);
160     s = erl_compare_ext(buf1, buf2);
161     fprintf(dfile, "comp s = %d\n", s); fflush(dfile);
162 
163     /* Compare, in another way */
164     s = erl_match(ref1, ref2);
165     fprintf(dfile, "match s = %d\n", s); fflush(dfile);
166 #endif
167 
168     fclose(dfile);
169 
170     erl_close_connection(fd);
171 
172     return 0;
173 }
174