}
/**
- * @brief Internal function to check connection.
- */
-static bool
-_nns_edge_check_connection (GSocket * socket)
-{
- GIOCondition condition;
-
- if (!socket || g_socket_is_closed (socket))
- return false;
-
- condition = g_socket_condition_check (socket,
- G_IO_IN | G_IO_OUT | G_IO_PRI | G_IO_ERR | G_IO_HUP);
-
- if (!condition || (condition & (G_IO_ERR | G_IO_HUP))) {
- nns_edge_logw ("Socket is not available, possibly closed.");
- return false;
- }
-
- return true;
-}
-
-/**
* @brief Receive data from connected socket.
*/
static bool
}
/**
- * @brief Get available port number.
+ * @brief Internal function to check connection.
*/
-static int
-_get_available_port (void)
+static bool
+_nns_edge_check_connection (nns_edge_conn_s * conn)
{
- struct sockaddr_in sin;
- int port = 0, sock;
- socklen_t len = sizeof (struct sockaddr);
-
- sin.sin_family = AF_INET;
- sin.sin_addr.s_addr = INADDR_ANY;
- sock = socket (AF_INET, SOCK_STREAM, 0);
- if (sock < 0) {
- nns_edge_loge ("Failed to get available port. Socket creation failure.");
- return 0;
- }
- sin.sin_port = port;
- if (bind (sock, (struct sockaddr *) &sin, sizeof (struct sockaddr)) == 0) {
- if (getsockname (sock, (struct sockaddr *) &sin, &len) == 0) {
- port = ntohs (sin.sin_port);
- nns_edge_logi ("Available port number: %d", port);
- } else {
- nns_edge_logw ("Failed to read local socket info.");
- }
+ GIOCondition condition;
+
+ if (!conn || !conn->socket || g_socket_is_closed (conn->socket))
+ return false;
+
+ condition = g_socket_condition_check (conn->socket,
+ G_IO_IN | G_IO_OUT | G_IO_PRI | G_IO_ERR | G_IO_HUP);
+
+ if (!condition || (condition & (G_IO_ERR | G_IO_HUP))) {
+ nns_edge_logw ("Socket is not available, possibly closed.");
+ return false;
}
- close (sock);
- return port;
+ return true;
}
/**
return NNS_EDGE_ERROR_INVALID_PARAMETER;
}
- if (!_nns_edge_check_connection (conn->socket)) {
+ if (!_nns_edge_check_connection (conn)) {
nns_edge_loge ("Failed to send command, socket has error.");
return NNS_EDGE_ERROR_IO;
}
if (!conn || !cmd)
return NNS_EDGE_ERROR_INVALID_PARAMETER;
- if (!_nns_edge_check_connection (conn->socket)) {
+ if (!_nns_edge_check_connection (conn)) {
nns_edge_loge ("Failed to receive command, socket has error.");
return NNS_EDGE_ERROR_IO;
}
if (!conn)
return false;
- if (conn->running && conn->msg_thread) {
+ /* Stop and clear the message thread. */
+ if (conn->msg_thread) {
conn->running = 0;
pthread_cancel (conn->msg_thread);
pthread_join (conn->msg_thread, NULL);
_nns_edge_cmd_init (&cmd, _NNS_EDGE_CMD_ERROR, 0);
_nns_edge_cmd_send (conn, &cmd);
- /* Release socket. If its last reference is dropped, it will close socket automatically. */
+ /**
+ * Close and release the socket.
+ * Using GSocket, if its last reference is dropped, it will close socket automatically.
+ */
g_clear_object (&conn->socket);
}
/* Send ip and port to destination. */
_nns_edge_cmd_init (&cmd, _NNS_EDGE_CMD_HOST_INFO, client_id);
- _get_host_str (eh->recv_ip, eh->recv_port, &host);
+ _get_host_str (eh->ip, eh->port, &host);
cmd.info.num = 1;
cmd.info.mem_size[0] = strlen (host) + 1;
cmd.mem[0] = host;
client_id = _tdata->client_id;
SAFE_FREE (_tdata);
- /* Start message thread, add ref to clearly make the socket live. */
conn->running = 1;
-
while (conn->running) {
nns_edge_data_h data_h;
unsigned int i;
if (tid < 0) {
nns_edge_loge ("Failed to create message handler thread.");
conn->running = 0;
+ conn->msg_thread = 0;
SAFE_FREE (thread_data);
return NNS_EDGE_ERROR_IO;
}
eh->id = nns_edge_strdup (id);
eh->topic = nns_edge_strdup (topic);
eh->protocol = NNS_EDGE_PROTOCOL_TCP;
- eh->is_server = true;
- eh->recv_ip = nns_edge_strdup ("localhost");
- eh->recv_port = 0;
+ eh->is_server = false;
+ eh->ip = nns_edge_strdup ("localhost");
+ eh->port = 0;
eh->caps_str = NULL;
/* Connection data for each client ID. */
}
eh->is_server = is_server;
- if (!is_server && 0 == eh->recv_port) {
- eh->recv_port = _get_available_port ();
- if (eh->recv_port <= 0) {
+ if (!is_server && 0 == eh->port) {
+ eh->port = nns_edge_get_available_port ();
+ if (eh->port <= 0) {
nns_edge_loge ("Failed to start edge. Cannot get available port.");
nns_edge_unlock (eh);
return NNS_EDGE_ERROR_CONNECTION_FAILURE;
eh->listener = g_socket_listener_new ();
g_socket_listener_set_backlog (eh->listener, N_BACKLOG);
- if (!_nns_edge_get_saddr (eh->recv_ip, eh->recv_port, &saddr)) {
+ if (!_nns_edge_get_saddr (eh->ip, eh->port, &saddr)) {
nns_edge_loge ("Failed to get socket address");
ret = NNS_EDGE_ERROR_CONNECTION_FAILURE;
goto error;
SAFE_FREE (eh->id);
SAFE_FREE (eh->topic);
SAFE_FREE (eh->ip);
- SAFE_FREE (eh->recv_ip);
SAFE_FREE (eh->caps_str);
nns_edge_unlock (eh);
}
conn_data = _nns_edge_get_connection (eh, eh->client_id);
- if (!_nns_edge_check_connection (conn_data->sink_conn->socket)) {
+ if (!_nns_edge_check_connection (conn_data->sink_conn)) {
nns_edge_loge ("Failed to request, connection failure.");
nns_edge_unlock (eh);
return NNS_EDGE_ERROR_CONNECTION_FAILURE;
* @todo User handles (replace or append) the capability of edge handle.
* @todo Change key-value set as json or hash table.
*/
- if (0 == g_ascii_strcasecmp (key, "CAPS")) {
+ if (0 == strcasecmp (key, "CAPS") || 0 == strcasecmp (key, "CAPABILITY")) {
SAFE_FREE (eh->caps_str);
- eh->caps_str = g_strdup (value);
- } else if (0 == g_ascii_strcasecmp (key, "IP")) {
- SAFE_FREE (eh->recv_ip);
- eh->recv_ip = nns_edge_strdup (value);
- } else if (0 == g_ascii_strcasecmp (key, "PORT")) {
- eh->recv_port = g_ascii_strtoll (value, NULL, 10);
- } else if (0 == g_ascii_strcasecmp (key, "TOPIC")) {
+ eh->caps_str = nns_edge_strdup (value);
+ } else if (0 == strcasecmp (key, "IP")) {
+ SAFE_FREE (eh->ip);
+ eh->ip = nns_edge_strdup (value);
+ } else if (0 == strcasecmp (key, "PORT")) {
+ eh->port = g_ascii_strtoll (value, NULL, 10);
+ } else if (0 == strcasecmp (key, "TOPIC")) {
SAFE_FREE (eh->topic);
eh->topic = nns_edge_strdup (value);
} else {
}
if (!value) {
- nns_edge_loge ("Invalid param, given value is invalid.");
+ nns_edge_loge ("Invalid param, value should not be null.");
return NNS_EDGE_ERROR_INVALID_PARAMETER;
}
* @todo User handles (replace or append) the capability of edge handle.
* @todo Change key-value set as json or hash table.
*/
- if (0 == g_ascii_strcasecmp (key, "CAPS")) {
+ if (0 == strcasecmp (key, "CAPS") || 0 == strcasecmp (key, "CAPABILITY")) {
*value = nns_edge_strdup (eh->caps_str);
- } else if (0 == g_ascii_strcasecmp (key, "IP")) {
- *value = nns_edge_strdup (eh->recv_ip);
- } else if (0 == g_ascii_strcasecmp (key, "PORT")) {
- *value = nns_edge_strdup_printf ("%d", eh->recv_port);
- } else if (0 == g_ascii_strcasecmp (key, "TOPIC")) {
+ } else if (0 == strcasecmp (key, "IP")) {
+ *value = nns_edge_strdup (eh->ip);
+ } else if (0 == strcasecmp (key, "PORT")) {
+ *value = nns_edge_strdup_printf ("%d", eh->port);
+ } else if (0 == strcasecmp (key, "TOPIC")) {
*value = nns_edge_strdup (eh->topic);
} else {
nns_edge_logw ("Failed to get edge info. Unknown key: %s", key);