pscom_err_t pscom_connect_socket_str(pscom_connection_t *connection, const char *socket_str) { int nodeid; int portno; char name[8]; int res; res = pscom_parse_socket_ondemand_str(socket_str, &nodeid, &portno, &name); if (res) goto err_parse; connection->nodeid = nodeid; connection->portno = portno; if (!name[0]) { return pscom_connect(connection, nodeid, portno); } else { return pscom_connect_ondemand(connection, nodeid, portno, name); } /* error code */ err_parse: if (socket_str) { DPRINT(1, "CONNECT (%s) failed : %s", socket_str, pscom_err_str(res)); } else { DPRINT(1, "CONNECT (<null>) failed : %s", pscom_err_str(res)); } return res; }
int pscom_parse_socket_str(const char *socket_str, int *nodeid, int *portno) { char name[8]; int rc = pscom_parse_socket_ondemand_str(socket_str, nodeid, portno, &name); if (!rc && name[0]) { // No error. But the name is not empty. errno = EINVAL; rc = -1; } return rc; }
int main(int argc, char **argv) { pscom_err_t rc; int ret; progname = strdup(argc && argv[0] ? argv[0] : "< ??? >"); parse_opt(argc, argv); pscom_set_debug(arg_verbose); pscom_init(PSCOM_VERSION); sock = pscom_open_socket(0, 0); if (!sock) abort_on_error("pscom_open_socket() failed", PSCOM_ERR_STDERROR); sock->ops = socket_ops_server; pscom_socket_set_name(sock, arg_listenname); rc = pscom_listen(sock, arg_listenport); if (rc) abort_on_error("pscom_listen() failed", rc); pscom_connection_t *con = pscom_open_connection(sock); rc = pscom_connect_socket_str(con, arg_peer_str); if (rc) abort_on_error("pscom_connect_socket_str()", rc); if (0) { // dummy connection pscom_connection_t *cond = pscom_open_connection(sock); rc = pscom_connect_socket_str(cond, "localhost:8912@dummy"); if (rc) abort_on_error("pscom_connect_socket_str()", rc); } // printf("Lokal connection: %p\n", con); { int peer_port; char peer_name[8]; ret = pscom_parse_socket_ondemand_str(arg_peer_str, NULL, &peer_port, &peer_name); if (ret) error(1, errno, "parse peer address failed"); printf("Call:\n"); printf("%s -l %d -n %1.8s %s%s\n" , progname, peer_port, peer_name, pscom_listen_socket_ondemand_str(sock), arg_send ? "" : " -s"); } pscom_stop_listen(sock); if (arg_send) { printf("Send in 2 sec\n"); sleep(2); char buf[1] = "x"; pscom_send(con, NULL, 0, buf, 1); printf("Send: %1.1s\n", buf); rc = pscom_recv(con, NULL, NULL, 0, buf, 1); if (rc) abort_on_error("pscom_recv()", rc); printf("Receive: %1.1s\n", buf); } else { char buf[1] = "o"; rc = pscom_recv(con, NULL, NULL, 0, buf, 1); if (rc) abort_on_error("pscom_recv()", rc); printf("Receive: %1.1s\n", buf); buf[0] = 'y'; pscom_send(con, NULL, 0, buf, 1); printf("Send: %1.1s\n", buf); } // sleep(10); puts(CYAN); pscom_dump_info(stdout); puts(NORM); pscom_flush(con); pscom_close_connection(con); pscom_close_socket(sock); return 0; }