summaryrefslogtreecommitdiff
path: root/src/msm/client.c
blob: 3cebf35924f08a923b091212dbc7c577b82cf6d8 (plain)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/* msm client object */

/* 
 * Copyright (C) 2001 Havoc Pennington
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#include "client.h"
#include "props.h"
#include "util.h"

#include <stdlib.h>
#include <string.h>

struct _MsmClient
{
  MsmServer *server;
  SmsConn cnxn;
  MsmClientState state;
  char *id;
  char *hostname;
  char *desc;
  int restart_style;
  GList *properties;
};

#define DEFAULT_RESTART_STYLE SmRestartIfRunning

MsmClient*
msm_client_new (MsmServer *server,
                SmsConn    cnxn)
{
  MsmClient *client;

  client = g_new (MsmClient, 1);

  client->server = server;
  client->cnxn = cnxn;
  client->state = MSM_CLIENT_STATE_NEW;
  client->id = NULL;
  client->hostname = NULL;
  client->desc = g_strdup ("unknown");
  client->restart_style = DEFAULT_RESTART_STYLE;
  client->properties = NULL;
  
  return client;
}

void
msm_client_free (MsmClient *client)
{
  IceConn ice_cnxn;
  GList *tmp;
  
  ice_cnxn = SmsGetIceConnection (client->cnxn);
  SmsCleanUp (client->cnxn);
  IceSetShutdownNegotiation (ice_cnxn, False);
  IceCloseConnection (ice_cnxn);

  tmp = client->properties;
  while (tmp != NULL)
    {
      SmProp *prop = tmp->data;

      SmFreeProperty (prop);
      
      tmp = tmp->next;
    }

  g_list_free (client->properties);
  
  g_free (client->id);
  g_free (client->hostname);
  g_free (client->desc);
    
  g_free (client);
}

SmsConn
msm_client_get_connection (MsmClient *client)
{
  return client->cnxn;
}

const char*
msm_client_get_description (MsmClient *client)
{
  return client->desc;
}

MsmClientState
msm_client_get_state (MsmClient *client)
{
  return client->state;
}

MsmServer*
msm_client_get_server (MsmClient *client)
{
  return client->server;
}

const char*
msm_client_get_id (MsmClient *client)
{
  return client->id;
}

int
msm_client_get_restart_style (MsmClient *client)
{
  return client->restart_style;
}

void
msm_client_register (MsmClient  *client,
                     const char *id)
{
  char *p;

  if (client->state != MSM_CLIENT_STATE_NEW)
    {
      msm_warning (_("Client '%s' attempted to register when it was already registered\n"), client->desc);

      return;
    }
  
  client->state = MSM_CLIENT_STATE_IDLE;
  client->id = g_strdup (id);

  SmsRegisterClientReply (client->cnxn, client->id);

  p = SmsClientHostName (client->cnxn);
  client->hostname = g_strdup (p);
  free (p);
}

void
msm_client_interact_request (MsmClient *client)
{
  if (client->state != MSM_CLIENT_STATE_SAVING &&
      client->state != MSM_CLIENT_STATE_SAVING_PHASE2)
    {
      msm_warning (_("Client '%s' requested interaction when it was not being saved\n"),
                   client->desc);

      return;
    }
  
  msm_server_queue_interaction (client->server, client);
}

void
msm_client_begin_interact (MsmClient *client)
{  
  SmsInteract (client->cnxn);
}

static void
internal_save (MsmClient *client,
               int        save_style,
               gboolean   allow_interaction,
               gboolean   shut_down)
{
  if (client->state != MSM_CLIENT_STATE_IDLE)
    {
      msm_warning (_("Tried to save client '%s' but it was not in the idle state\n"),
                   client->desc);

      return;
    }

  client->state = MSM_CLIENT_STATE_SAVING;
  
  SmsSaveYourself (client->cnxn,
                   save_style,
                   shut_down,
                   allow_interaction ? SmInteractStyleAny : SmInteractStyleNone,
                   FALSE /* not "fast" */);
}

void
msm_client_save (MsmClient *client,
                 gboolean   allow_interaction,
                 gboolean   shut_down)
{  
  internal_save (client, SmSaveBoth, /* ? don't know what to do here */
                 allow_interaction, shut_down);
}

void
msm_client_initial_save (MsmClient  *client)
{
  /* This is the save on client registration in the spec under
   * RegisterClientReply
   */
  internal_save (client, SmSaveLocal, FALSE, FALSE);
}

void
msm_client_shutdown_cancelled (MsmClient *client)
{
  if (client->state != MSM_CLIENT_STATE_SAVING &&
      client->state != MSM_CLIENT_STATE_SAVING_PHASE2)
    {
      msm_warning (_("Tried to send cancel shutdown to client '%s' which was not saving\n"),
                   client->desc);
      return;
    }

  client->state = MSM_CLIENT_STATE_IDLE;
  SmsShutdownCancelled (client->cnxn);
}

void
msm_client_phase2_request (MsmClient *client)
{
  if (client->state != MSM_CLIENT_STATE_SAVING)
    {
      msm_warning (_("Client '%s' requested phase 2 save but was not in a phase 1 save\n"),
                   client->desc);
      return;
    }
  
  client->state = MSM_CLIENT_STATE_PHASE2_REQUESTED;
}

void
msm_client_save_phase2 (MsmClient *client)
{
  if (client->state != MSM_CLIENT_STATE_PHASE2_REQUESTED)
    {
      msm_warning (_("We tried to save client '%s' in phase 2, but it hadn't requested it.\n"), client->desc);
      return;
    }

  SmsSaveYourselfPhase2 (client->cnxn);
}

void
msm_client_die (MsmClient  *client)
{
  client->state = MSM_CLIENT_STATE_DEAD;
  SmsDie (client->cnxn);
}

void
msm_client_save_complete (MsmClient *client)
{
  client->state = MSM_CLIENT_STATE_IDLE;
  SmsSaveComplete (client->cnxn);
}

void
msm_client_save_confirmed (MsmClient  *client,
                           gboolean    successful)
{
  if (client->state != MSM_CLIENT_STATE_SAVING &&
      client->state != MSM_CLIENT_STATE_SAVING_PHASE2)
    {
      msm_warning (_("Client '%s' said it was done saving, but it hadn't been told to save\n"),
                   client->desc);
      return;
    }

  if (successful)
    client->state = MSM_CLIENT_STATE_SAVE_DONE;
  else
    client->state = MSM_CLIENT_STATE_SAVE_FAILED;
}

void
msm_client_set_property_taking_ownership (MsmClient *client,
                                          SmProp    *prop)
{
  /* we own prop which should be freed with SmFreeProperty() */

  /* pass our ownership into the proplist */
  client->properties = proplist_replace (client->properties, prop);

  /* update pieces of the client struct */
  if (strcmp (prop->name, "SmRestartStyleHint") == 0)
    {
      int hint;
      if (smprop_get_card8 (prop, &hint))
        client->restart_style = hint;
      else
        client->restart_style = DEFAULT_RESTART_STYLE;
    }
}

void
msm_client_unset_property (MsmClient *client,
                           const char *name)
{
  client->properties = proplist_delete (client->properties, name);

  /* Return to default values */
  if (strcmp (name, "SmRestartStyleHint") == 0)
    {
      client->restart_style = DEFAULT_RESTART_STYLE;
    }
}

void
msm_client_send_properties (MsmClient *client)
{
  int n_props;
  SmProp **props;
  GList *tmp;
  int i;
  
  n_props = g_list_length (client->properties);
  props = g_new (SmProp*, n_props);

  i = 0;
  tmp = client->properties;
  while (tmp != NULL)
    {
      props[i] = tmp->data;

      tmp = tmp->next;
      ++i;
    }
  
  SmsReturnProperties (client->cnxn, n_props, props);

  g_free (props);
}