aboutsummaryrefslogtreecommitdiffstats
path: root/apps/clients.c
blob: b5bafcdabd42e146565beb74f61074b2245edc73 (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
/*
 * clients.c:
 *
 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
 * All rights reserved.
 *
 */

static char rcsid[] = "$Id$";

/*
 * $Log$
 * Revision 1.3  2008/02/14 02:46:44  james
 * *** empty log message ***
 *
 * Revision 1.2  2008/02/14 00:57:58  james
 * *** empty log message ***
 *
 * Revision 1.1  2008/02/13 18:05:06  james
 * *** empty log message ***
 *
 */

#include <sympathy.h>
#include "clients.h"

static void
client_msg (IPC_Msg * m, Context * c)
{
  switch (m->hdr.type)
    {

    case IPC_MSG_TYPE_NOOP:
      break;
    case IPC_MSG_TYPE_DEBUG:
      fprintf (stderr, "%p [%d] %s\n", m, m->hdr.size, m->debug.msg);
      break;
    case IPC_MSG_TYPE_KEY:
      vt102_send (c, m->key.key);
      break;
    default:
      fprintf (stderr, "Unhandeled message type %d\n", m->hdr.type);
    }
}

void
client_free (Client * c)
{
  if (c->s)
    socket_free (c->s);

  free (c);
  fprintf (stderr, "Client at %p freed\n", c);
}

Client *
clients_new_client (Clients * cs, Socket * s, Context * ctx)
{
  Client *c;

  c = (Client *) malloc (sizeof (Client));

  c->dead = 0;
  c->s = s;
  c->next = cs->head;

  cs->head = c;
  cs->n++;

  fprintf (stderr, "Client at %p created\n", c);


  if (ipc_msg_send_debug (s, "new_client"))
    c->dead++;

  return c;
}

void
clients_reap (Clients * cs)
{
  Client **p, *c;


  for (p = &cs->head; *p;)
    {
      Client *c = *p;

      if (c->dead)
        {
          *p = c->next;
          client_free (c);
          cs->n--;
        }
      else
        {
          p = &(c->next);
        }
    }
}

Clients *
clients_new (void)
{
  Clients *ret = (Clients *) malloc (sizeof (Clients));

  ret->n = 0;
  ret->head = NULL;

  return ret;
}

void
clients_pre_select (Clients * cs, fd_set * rfds, fd_set * wfds)
{
  Client *c;

  for (c = cs->head; c; c = c->next)
    {
      socket_pre_select (c->s, rfds, wfds);
    }
}

void
clients_post_select (Clients * cs, Context * ctx, fd_set * rfds,
                     fd_set * wfds)
{
  Client *c;
  int deaded = 0;

  for (c = cs->head; c; c = c->next)
    {
      if (socket_post_select (c->s, rfds, wfds))
        {
          c->dead++;
          deaded++;
        }

      if (c->s->msg)
        {
          client_msg (c->s->msg, ctx);
          socket_consume_msg (c->s);
        }

    }

  if (deaded)
    clients_reap (cs);
}

int
clients_output (Clients * cs, void *buf, int len)
{
  char mbuf[IPC_MAX_BUF + sizeof (IPC_Msg_term)];
  IPC_Msg_term *m = (IPC_Msg_term *) mbuf;

  Client *c;

  if (!len)
    return;
  if (len > IPC_MAX_BUF)
    len = IPC_MAX_BUF;

  m->size = len + sizeof (IPC_Msg_term);
  m->type = IPC_MSG_TYPE_TERM;
  m->len = len;
  memcpy (m->term, buf, len);

  for (c = cs->head; c; c = c->next)
    {
      if (!c->dead)
        if (ipc_msg_send (c->s, (IPC_Msg *) m))
          c->dead++;
    }


  return len;
}

void
clients_shutdown (Clients * cs)
{
  Client *c;

  for (c = cs->head; c; c = c->next)
    {
      c->dead++;
    }


  clients_reap (cs);
}