Copyright update for 2011
[bcusdk.git] / eibd / libserver / server.cpp
blob8ec5e39ba9f27f41a500e75fbf572060de97a4bd
1 /*
2 EIBD eib bus access and management daemon
3 Copyright (C) 2005-2011 Martin Koegler <mkoegler@auto.tuwien.ac.at>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include <unistd.h>
21 #include "server.h"
22 #include "client.h"
25 Server::~Server ()
27 TRACEPRINTF (t, 8, this, "StopServer");
28 Stop ();
29 for (int i = 0; i < connections (); i++)
30 connections[i]->StopDelete ();
31 while (connections () != 0)
32 pth_yield (0);
34 if (fd != -1)
35 close (fd);
36 TRACEPRINTF (t, 8, this, "Server ended");
39 bool
40 Server::deregister (ClientConnection * con)
42 for (unsigned i = 0; i < connections (); i++)
43 if (connections[i] == con)
45 connections.deletepart (i, 1);
46 return 1;
48 return 0;
51 Server::Server (Layer3 * layer3, Trace * tr)
53 t = tr;
54 l3 = layer3;
55 fd = -1;
58 void
59 Server::Run (pth_sem_t * stop1)
61 pth_event_t stop = pth_event (PTH_EVENT_SEM, stop1);
62 while (pth_event_status (stop) != PTH_STATUS_OCCURRED)
64 int cfd;
65 cfd = pth_accept_ev (fd, 0, 0, stop);
66 if (cfd != -1)
68 TRACEPRINTF (t, 8, this, "New Connection");
69 setupConnection (cfd);
70 ClientConnection *c = new ClientConnection (this, l3, t, cfd);
71 connections.setpart (&c, connections (), 1);
72 c->Start ();
75 pth_event_free (stop, PTH_FREE_THIS);
78 void
79 Server::setupConnection (int cfd)