128 lines
2.4 KiB
C
128 lines
2.4 KiB
C
/* copyright (c) 2020 Mathieu Kerjouan <contact@steepath.eu>
|
|
*
|
|
* erluv -n NODE_NAME -t TARGET_NODE -c COOKIE -i ID
|
|
*
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
#include <ei.h>
|
|
#include <string.h>
|
|
#include <strings.h>
|
|
#include <unistd.h>
|
|
#include <uv.h>
|
|
#include <uv/unix.h>
|
|
#include <uv/bsd.h>
|
|
#include "erluv.h"
|
|
|
|
extern short creation;
|
|
extern int id;
|
|
extern char cookie[];
|
|
extern char node_name[];
|
|
extern char node_target[];
|
|
|
|
void
|
|
usage() {
|
|
printf("usage: erluv ...\n");
|
|
}
|
|
|
|
|
|
int
|
|
create_loop(uv_loop_t *loop) {
|
|
/* create the loop */
|
|
loop = malloc(sizeof(uv_loop_t));
|
|
uv_loop_init(loop);
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
close_loop(uv_loop_t *loop) {
|
|
uv_loop_close(uv_default_loop());
|
|
free(loop);
|
|
return 0;
|
|
}
|
|
|
|
void
|
|
process_on_exit(uv_process_t* p, int64_t exit_status, int term_signal) {
|
|
fprintf(stderr, "process %d exited\n", p->pid);
|
|
}
|
|
|
|
int
|
|
process_new(uv_loop_t *loop) {
|
|
uv_process_t child_req = {0};
|
|
uv_process_options_t options = {0};
|
|
|
|
char *name = "ls";
|
|
char *args[3] = { "ls", "/", NULL };
|
|
char *env[] = { NULL };
|
|
|
|
options.exit_cb = process_on_exit;
|
|
options.file = name;
|
|
options.args = args;
|
|
options.env = env;
|
|
|
|
int r = uv_spawn(loop, &child_req, &options);
|
|
if (r) {
|
|
fprintf(stderr, "%s\n", uv_strerror(r));
|
|
return 1;
|
|
}
|
|
else {
|
|
fprintf(stderr, "Launched process with ID %d\n", child_req.pid);
|
|
}
|
|
|
|
return uv_run(loop, UV_RUN_DEFAULT);
|
|
}
|
|
|
|
|
|
void action(uv_tcp_t* handle) {
|
|
printf("receive data");
|
|
}
|
|
|
|
/* main function
|
|
*
|
|
*/
|
|
int
|
|
main(int argc, char *argv[]) {
|
|
/* init default variable */
|
|
(void)init();
|
|
|
|
/* create the loop */
|
|
uv_loop_t *loop;
|
|
create_loop(loop);
|
|
|
|
/* connect to erlang node */
|
|
ei_cnode ec;
|
|
ei_connect_init(&ec, node_name, cookie, creation);
|
|
|
|
/* create the connection to the server */
|
|
int sockfd;
|
|
if ((sockfd = ei_connect(&ec, node_target)) < 0)
|
|
fprintf(stderr, "ERROR: ei_connect failed\n");
|
|
|
|
/* create an erlang pid
|
|
erlang_pid pid;
|
|
ei_make_pid(&ec, &pid);
|
|
erlang_pid *p = ei_self(&ec);
|
|
printf("%d, %d, %d\n", p->num, p->serial, p->creation);
|
|
printf("%d, %d, %d\n", pid.num, pid.serial, pid.creation);
|
|
*/
|
|
|
|
char *message = "my_message";
|
|
char *process_name = "ttt";
|
|
|
|
ei_x_buff x;
|
|
ei_x_new_with_version(&x);
|
|
ei_x_encode_string(&x, message);
|
|
ei_reg_send(&ec, sockfd, process_name, x.buff, x.index);
|
|
ei_x_free(&x);
|
|
shutdown(sockfd, 1);
|
|
|
|
// uv_tcp_close_reset(server, close_cb);
|
|
|
|
close(sockfd);
|
|
close_loop(loop);
|
|
|
|
return 0;
|
|
}
|
|
|