Browse Source

Add helper functions for sending/receiving to a process instance

master
Con Kolivas 11 years ago
parent
commit
1953cd2935
  1. 59
      src/libckpool.c
  2. 2
      src/libckpool.h

59
src/libckpool.c

@ -695,6 +695,65 @@ bool send_unix_msg(int sockd, const char *buf)
return true; return true;
} }
/* Send a single message to a process instance when there will be no response,
* closing the socket immediately. */
bool send_proc(proc_instance_t *pi, const char *msg)
{
char *path = pi->us.path;
bool ret = false;
int sockd;
if (unlikely(!path || !strlen(path))) {
LOGERR("Attempted to send message to null path in send_proc");
goto out;
}
if (unlikely(!msg || !strlen(msg))) {
LOGERR("Attempted to send null message to socket %s in send_proc", path);
goto out;
}
sockd = open_unix_client(path);
if (unlikely(sockd < 0)) {
LOGWARNING("Failed to open socket %s", path);
goto out;
}
if (unlikely(!send_unix_msg(sockd, msg)))
LOGWARNING("Failed to send %s to socket %s", msg, path);
else
ret = true;
close(sockd);
out:
return ret;
}
/* Send a single message to a process instance and retrieve the response, then
* close the socket. */
char *send_recv_proc(proc_instance_t *pi, const char *msg)
{
char *path = pi->us.path, *buf = NULL;
int sockd;
if (unlikely(!path || !strlen(path))) {
LOGERR("Attempted to send message to null path in send_proc");
goto out;
}
if (unlikely(!msg || !strlen(msg))) {
LOGERR("Attempted to send null message to socket %s in send_proc", path);
goto out;
}
sockd = open_unix_client(path);
if (unlikely(sockd < 0)) {
LOGWARNING("Failed to open socket %s", path);
goto out;
}
if (unlikely(!send_unix_msg(sockd, msg)))
LOGWARNING("Failed to send %s to socket %s", msg, path);
else
buf = recv_unix_msg(sockd);
close(sockd);
out:
return buf;
}
json_t *json_rpc_call(connsock_t *cs, const char *rpc_req) json_t *json_rpc_call(connsock_t *cs, const char *rpc_req)
{ {

2
src/libckpool.h

@ -226,6 +226,8 @@ int open_unix_server(const char *server_path);
int open_unix_client(const char *server_path); int open_unix_client(const char *server_path);
char *recv_unix_msg(int sockd); char *recv_unix_msg(int sockd);
bool send_unix_msg(int sockd, const char *buf); bool send_unix_msg(int sockd, const char *buf);
bool send_proc(proc_instance_t *pi, const char *msg);
char *send_recv_proc(proc_instance_t *pi, const char *msg);
json_t *json_rpc_call(connsock_t *cs, const char *rpc_req); json_t *json_rpc_call(connsock_t *cs, const char *rpc_req);

Loading…
Cancel
Save