Exemplo n.º 1
0
static void
handle_connection_closed_cb (G_GNUC_UNUSED SoupSession * session,
    SoupMessage * msg, AurClient * client)
{
  AurClientFlags flag = get_flag_from_msg (msg);

  client->connecting &= ~flag;

  if (client->idle_timeout) {
    g_source_remove (client->idle_timeout);
    client->idle_timeout = 0;
  }

  if (msg->status_code == SOUP_STATUS_CANCELLED)
    return;

  if (client->was_connected & flag) {
    g_print ("%s disconnected from server. Reason %s status %d\n",
        flag == AUR_CLIENT_PLAYER ? "Player" : "Controller",
        msg->reason_phrase, msg->status_code);
  }
  client->was_connected &= ~flag;

  if (flag == AUR_CLIENT_PLAYER && client->player)
    gst_element_set_state (client->player, GST_STATE_READY);

  if (client->player_info) {
    free_player_info (client->player_info);
    client->player_info = NULL;
    g_signal_emit (client, signals[SIGNAL_PLAYER_INFO_CHANGED], 0);
  }

  if (client->timeout == 0) {
    client->timeout =
        g_timeout_add_seconds (1, (GSourceFunc) try_reconnect, client);
  }

  if (!client->was_connected) {
    g_free (client->connected_server);
    client->connected_server = NULL;
    client->connected_port = 0;
    client->paused = TRUE;
    client->enabled = FALSE;
    g_object_notify (G_OBJECT (client), "paused");
    g_object_notify (G_OBJECT (client), "enabled");
    g_object_notify (G_OBJECT (client), "connected-server");
  }
}
Exemplo n.º 2
0
static void
handle_received_chunk (SoupMessage * msg, SoupBuffer * chunk,
    SnraClient * client)
{
  const gchar *ptr;
  gsize length;
  SnraClientFlags flag = get_flag_from_msg (msg);

  if (client->was_connected & flag) {
    g_print ("Successfully connected %s to server %s:%d\n",
        flag == SNRA_CLIENT_PLAYER ? "player" : "controller",
        client->connected_server, client->connected_port);
    client->was_connected |= flag;
  }

  /* Successful server connection, stop avahi discovery */
  if (client->avahi_client) {
    avahi_client_free (client->avahi_client);
    client->avahi_sb = NULL;
    client->avahi_client = NULL;
  }

  if (client->json == NULL)
    client->json = json_parser_new ();
#if 0
  {
    gchar *tmp = g_strndup (chunk->data, chunk->length);
    g_print ("%s\n", tmp);
    g_free (tmp);
  }
#endif

  ptr = memchr (chunk->data, '\0', chunk->length);
  if (!ptr)
    return;

  /* Save remaining portion */
  ptr += 1;
  length =(chunk->length - (ptr - chunk->data));

  chunk = soup_message_body_flatten (msg->response_body);
  if (json_parser_load_from_data (client->json, chunk->data, chunk->length,
          NULL)) {
    JsonNode *root = json_parser_get_root (client->json);
    GstStructure *s = snra_json_to_gst_structure (root);

    if (s == NULL)
      goto end;                   /* Invalid chunk */

    if (flag == SNRA_CLIENT_PLAYER)
      handle_player_message (client, s);
    else
      handle_controller_message (client, s);

    gst_structure_free (s);
  }

end:
  soup_message_body_truncate (msg->response_body);
  /* Put back remaining part */
  if (length)
    soup_message_body_append (msg->response_body, SOUP_MEMORY_COPY, ptr, length);
}
Exemplo n.º 3
0
static void
handle_received_chunk (SoupMessage * msg, SoupBuffer * chunk,
    AurClient * client)
{
  const gchar *ptr;
  gsize length;
  AurClientFlags flag = get_flag_from_msg (msg);
  JsonNode *root;
  GstStructure *s;
  GError *err = NULL;
  gchar *json_str = NULL;

  if (client->was_connected & flag) {
    g_print ("Successfully connected %s to server %s:%d\n",
        flag == AUR_CLIENT_PLAYER ? "player" : "controller",
        client->connected_server, client->connected_port);
    client->was_connected |= flag;
  }

  /* Set up or re-trigger 20 second idle timeout for ping messages */
  if (client->idle_timeout)
    g_source_remove (client->idle_timeout);
  client->idle_timeout = g_timeout_add_seconds (20,
      (GSourceFunc) conn_idle_timeout, client);

#if HAVE_AVAHI
  /* Successful server connection, stop avahi discovery */
  if (client->avahi_client) {
    avahi_client_free (client->avahi_client);
    client->avahi_sb = NULL;
    client->avahi_client = NULL;
  }
#endif

  if (client->json == NULL)
    client->json = json_parser_new ();

  ptr = memchr (chunk->data, '\0', chunk->length);
  if (!ptr)
    return;

  /* Save remaining portion */
  ptr += 1;
  length = (chunk->length - (ptr - chunk->data));

  chunk = soup_message_body_flatten (msg->response_body);

  // Ignore null string chunks
  if (chunk->length < 2)
    goto end;

  /* Workaround: Copy to a string to avoid stupid
   * UTF-8 validation bug in json-glib 1.0.2 */
  json_str = g_strndup (chunk->data, chunk->length);

#if 0
  g_print ("%s\n", json_str);
#endif

  if (!json_parser_load_from_data (client->json, json_str, -1,
          &err) || err != NULL)
    goto fail;

  root = json_parser_get_root (client->json);
  s = aur_json_to_gst_structure (root);

  if (s == NULL)
    goto fail;                  /* Invalid chunk */

  if (flag == AUR_CLIENT_PLAYER)
    handle_player_message (client, s);
  else
    handle_controller_message (client, s);

  gst_structure_free (s);

end:
  g_free (json_str);

  soup_message_body_truncate (msg->response_body);
  /* Put back remaining part */
  if (length)
    soup_message_body_append (msg->response_body, SOUP_MEMORY_COPY, ptr,
        length);
  return;

fail:{
    g_print ("Failed to parse message '%s'\n", json_str);
    if (err) {
      g_print ("Error: %s\n", err->message);
      g_error_free (err);
    }
    goto end;
  }
}