[spice-gtk,v1,2/6] spice-channel: add internal spice_channel_set_state()

Submitted by Victor Toso on Sept. 24, 2019, 9:14 a.m.

Details

Message ID 20190924091502.16038-3-victortoso@redhat.com
State New
Headers show
Series "migration object" ( rev: 2 1 ) in Spice

Not browsing as part of any series.

Commit Message

Victor Toso Sept. 24, 2019, 9:14 a.m.
From: Victor Toso <me@victortoso.com>

So we can easily track channel's state changes instead of doing so by
accessing its Private structure. Some more checking on the state
machine can be later added as well.

Signed-off-by: Victor Toso <victortoso@redhat.com>
---
 src/channel-main.c       |  8 ++++----
 src/spice-channel-priv.h |  1 +
 src/spice-channel.c      | 37 +++++++++++++++++++++++++++++++++++++
 src/spice-session.c      |  4 ++--
 4 files changed, 44 insertions(+), 6 deletions(-)

Patch hide | download patch | download mbox

diff --git a/src/channel-main.c b/src/channel-main.c
index ac0d408..a0ab520 100644
--- a/src/channel-main.c
+++ b/src/channel-main.c
@@ -2215,11 +2215,11 @@  static void migrate_channel_event_cb(SpiceChannel *channel, SpiceChannelEvent ev
             if (mig->do_seamless) {
                 SpiceMainChannelPrivate *main_priv = SPICE_MAIN_CHANNEL(channel)->priv;
 
-                c->state = SPICE_CHANNEL_STATE_MIGRATION_HANDSHAKE;
+                spice_channel_set_state(channel, SPICE_CHANNEL_STATE_MIGRATION_HANDSHAKE);
                 mig->dst_channel = channel;
                 main_priv->migrate_data = mig;
             } else {
-                c->state = SPICE_CHANNEL_STATE_MIGRATING;
+                spice_channel_set_state(channel, SPICE_CHANNEL_STATE_MIGRATING);
                 mig->nchannels--;
             }
             /* now connect the rest of the channels */
@@ -2238,7 +2238,7 @@  static void migrate_channel_event_cb(SpiceChannel *channel, SpiceChannelEvent ev
             }
             g_list_free(channels);
         } else {
-            c->state = SPICE_CHANNEL_STATE_MIGRATING;
+            spice_channel_set_state(channel, SPICE_CHANNEL_STATE_MIGRATING);
             mig->nchannels--;
         }
 
@@ -2262,7 +2262,7 @@  static gboolean main_migrate_handshake_done(gpointer data)
     g_return_val_if_fail(spice_channel_get_channel_type(channel) == SPICE_CHANNEL_MAIN, FALSE);
     g_return_val_if_fail(spice_channel_get_state(channel) == SPICE_CHANNEL_STATE_MIGRATION_HANDSHAKE, FALSE);
 
-    c->state = SPICE_CHANNEL_STATE_MIGRATING;
+    spice_channel_set_state(channel, SPICE_CHANNEL_STATE_MIGRATING);
     mig->nchannels--;
     if (mig->nchannels == 0)
         coroutine_yieldto(mig->from, NULL);
diff --git a/src/spice-channel-priv.h b/src/spice-channel-priv.h
index 80ea714..4f893dd 100644
--- a/src/spice-channel-priv.h
+++ b/src/spice-channel-priv.h
@@ -171,6 +171,7 @@  void spice_channel_up(SpiceChannel *channel);
 void spice_channel_wakeup(SpiceChannel *channel, gboolean cancel);
 
 SpiceSession* spice_channel_get_session(SpiceChannel *channel);
+void spice_channel_set_state(SpiceChannel *channel, gint state);
 enum spice_channel_state spice_channel_get_state(SpiceChannel *channel);
 guint64 spice_channel_get_queue_size (SpiceChannel *channel);
 
diff --git a/src/spice-channel.c b/src/spice-channel.c
index 315e287..26fa671 100644
--- a/src/spice-channel.c
+++ b/src/spice-channel.c
@@ -3009,6 +3009,43 @@  SpiceSession* spice_channel_get_session(SpiceChannel *channel)
     return channel->priv->session;
 }
 
+static const char *
+channel_state_to_string(gint state)
+{
+    switch(state) {
+    case SPICE_CHANNEL_STATE_UNCONNECTED:
+        return "unconnected";
+    case SPICE_CHANNEL_STATE_RECONNECTING:
+        return "reconnecting";
+    case SPICE_CHANNEL_STATE_CONNECTING:
+        return "connecting";
+    case SPICE_CHANNEL_STATE_READY:
+        return "ready";
+    case SPICE_CHANNEL_STATE_SWITCHING:
+        return "switching";
+    case SPICE_CHANNEL_STATE_MIGRATING:
+        return "migrating";
+    case SPICE_CHANNEL_STATE_MIGRATION_HANDSHAKE:
+        return "migration handshake";
+    default:
+        g_warn_if_reached();
+    }
+    return "????";
+}
+
+G_GNUC_INTERNAL
+void spice_channel_set_state(SpiceChannel *channel, gint state)
+{
+    g_return_if_fail(SPICE_IS_CHANNEL(channel));
+    g_return_if_fail(state >= SPICE_CHANNEL_STATE_UNCONNECTED &&
+                     state <= SPICE_CHANNEL_STATE_MIGRATION_HANDSHAKE);
+
+    CHANNEL_DEBUG(channel, "state changing from %s -> %s",
+                  channel_state_to_string(channel->priv->state),
+                  channel_state_to_string(state));
+    channel->priv->state = state;
+}
+
 G_GNUC_INTERNAL
 enum spice_channel_state spice_channel_get_state(SpiceChannel *channel)
 {
diff --git a/src/spice-session.c b/src/spice-session.c
index afeb724..a770c92 100644
--- a/src/spice-session.c
+++ b/src/spice-session.c
@@ -1889,7 +1889,7 @@  static gboolean after_main_init(gpointer data)
         l = l->next;
 
         spice_session_channel_migrate(self, channel);
-        channel->priv->state = SPICE_CHANNEL_STATE_READY;
+        spice_channel_set_state(channel, SPICE_CHANNEL_STATE_READY);
         spice_channel_up(channel);
     }
 
@@ -1937,7 +1937,7 @@  void spice_session_migrate_end(SpiceSession *self)
 
         if (!SPICE_IS_MAIN_CHANNEL(channel)) {
             /* freeze other channels */
-            channel->priv->state = SPICE_CHANNEL_STATE_MIGRATING;
+            spice_channel_set_state(channel, SPICE_CHANNEL_STATE_MIGRATING);
         }
 
         /* reset for migration, disconnect */