File: main-dispatcher.cpp

package info (click to toggle)
spice 0.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,460 kB
  • sloc: ansic: 43,305; cpp: 30,185; sh: 5,342; python: 3,040; makefile: 844
file content (172 lines) | stat: -rw-r--r-- 5,594 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
   Copyright (C) 2009-2015 Red Hat, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

#include <config.h>

#include <cerrno>
#include <cstring>

#include <pthread.h>
#include <unistd.h>

#include "red-common.h"
#include "dispatcher.h"
#include "main-dispatcher.h"
#include "red-client.h"
#include "reds.h"

/*
 * Main Dispatcher
 * ===============
 *
 * Communication channel between any non main thread and the main thread.
 *
 * The main thread is that from which spice_server_init is called.
 *
 * Messages are single sized, sent from the non-main thread to the main-thread.
 * No acknowledge is sent back. This prevents a possible deadlock with the main
 * thread already waiting on a response for the existing red_dispatcher used
 * by the worker thread.
 *
 * All events have three functions:
 * main_dispatcher_<event_name> - non static, public function
 * main_dispatcher_self_<event_name> - handler for main thread
 * main_dispatcher_handle_<event_name> - handler for callback from main thread
 *   seperate from self because it may send an ack or do other work in the future.
 */

enum {
    MAIN_DISPATCHER_CHANNEL_EVENT = 0,
    MAIN_DISPATCHER_MIGRATE_SEAMLESS_DST_COMPLETE,
    MAIN_DISPATCHER_CLIENT_DISCONNECT,

    MAIN_DISPATCHER_NUM_MESSAGES
};

struct MainDispatcherChannelEventMessage {
    int event;
    SpiceChannelEventInfo *info;
};

struct MainDispatcherMigrateSeamlessDstCompleteMessage {
    RedClient *client;
};

struct MainDispatcherClientDisconnectMessage {
    RedClient *client;
};

/* channel_event - calls core->channel_event, must be done in main thread */
static void main_dispatcher_handle_channel_event(void *opaque,
                                                 void *payload)
{
    auto reds = static_cast<RedsState *>(opaque);
    auto channel_event = static_cast<MainDispatcherChannelEventMessage *>(payload);

    reds_handle_channel_event(reds, channel_event->event, channel_event->info);
}

void MainDispatcher::channel_event(int event, SpiceChannelEventInfo *info)
{
    MainDispatcherChannelEventMessage msg = {0,};

    if (pthread_self() == thread_id) {
        reds_handle_channel_event(reds, event, info);
        return;
    }
    msg.event = event;
    msg.info = info;
    send_message(MAIN_DISPATCHER_CHANNEL_EVENT, &msg);
}


static void main_dispatcher_handle_migrate_complete(void *opaque,
                                                    void *payload)
{
    auto reds = static_cast<RedsState *>(opaque);
    auto mig_complete = static_cast<MainDispatcherMigrateSeamlessDstCompleteMessage *>(payload);

    reds_on_client_seamless_migrate_complete(reds, mig_complete->client);
    mig_complete->client->unref();
}

static void main_dispatcher_handle_client_disconnect(void *opaque,
                                                     void *payload)
{
    auto reds = static_cast<RedsState *>(opaque);
    auto msg = static_cast<MainDispatcherClientDisconnectMessage *>(payload);

    spice_debug("client=%p", msg->client);
    reds_client_disconnect(reds, msg->client);
    msg->client->unref();
}

void MainDispatcher::seamless_migrate_dst_complete(RedClient *client)
{
    MainDispatcherMigrateSeamlessDstCompleteMessage msg;

    if (pthread_self() == thread_id) {
        reds_on_client_seamless_migrate_complete(reds, client);
        return;
    }

    msg.client = red::add_ref(client);
    send_message(MAIN_DISPATCHER_MIGRATE_SEAMLESS_DST_COMPLETE, &msg);
}

void MainDispatcher::client_disconnect(RedClient *client)
{
    MainDispatcherClientDisconnectMessage msg;

    if (!client->is_disconnecting()) {
        spice_debug("client %p", client);
        msg.client = red::add_ref(client);
        send_message(MAIN_DISPATCHER_CLIENT_DISCONNECT, &msg);
    } else {
        spice_debug("client %p already during disconnection", client);
    }
}

/*
 * FIXME:
 * Reds routines shouldn't be exposed. Instead reds.cpp should register the callbacks,
 * and the corresponding operations should be made only via main_dispatcher.
 */
MainDispatcher::MainDispatcher(RedsState *init_reds):
    Dispatcher(MAIN_DISPATCHER_NUM_MESSAGES),
    reds(init_reds),
    thread_id(pthread_self())
{
    set_opaque(reds);

    watch = create_watch(reds_get_core_interface(reds));
    register_handler(MAIN_DISPATCHER_CHANNEL_EVENT,
                     main_dispatcher_handle_channel_event,
                     sizeof(MainDispatcherChannelEventMessage), false);
    register_handler(MAIN_DISPATCHER_MIGRATE_SEAMLESS_DST_COMPLETE,
                     main_dispatcher_handle_migrate_complete,
                     sizeof(MainDispatcherMigrateSeamlessDstCompleteMessage), false);
    register_handler(MAIN_DISPATCHER_CLIENT_DISCONNECT,
                     main_dispatcher_handle_client_disconnect,
                     sizeof(MainDispatcherClientDisconnectMessage), false);
}

MainDispatcher::~MainDispatcher()
{
    red_watch_remove(watch);
}