1 /** @addtogroup MCD_MCDIMPL_DAEMON_SRV
2  * @{
3  * @file
4  *
5  * Connection server.
6  *
7  * Handles incoming socket connections from clients using the MobiCore driver.
8  *
9  * Iterative socket server using UNIX domain stream protocol.
10  *
11  * <!-- Copyright Giesecke & Devrient GmbH 2009 - 2012 -->
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. The name of the author may not be used to endorse or promote
22  *    products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
26  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
29  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
31  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 #ifndef SERVER_H_
38 #define SERVER_H_
39 
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <sys/un.h>
43 #include <string>
44 #include <cstdio>
45 #include <vector>
46 #include "CThread.h"
47 #include "ConnectionHandler.h"
48 
49 /** Number of incoming connections that can be queued.
50  * Additional clients will generate the error ECONNREFUSED. */
51 #define LISTEN_QUEUE_LEN    (16)
52 
53 
54 class Server: public CThread
55 {
56 
57 public:
58     /**
59      * Server contructor.
60      *
61      * @param connectionHanler Connection handler to pass incoming connections to.
62      * @param localAdrerss Pointer to a zero terminated string containing the file to listen to.
63      */
64     Server(
65         ConnectionHandler *connectionHandler,
66         const char *localAddr
67     );
68 
69     /**
70      * Server destructor.
71      * All available connections will be terminated. Resources will be freed.
72      */
73     virtual ~Server(
74         void
75     );
76 
77     /**
78      * Start server and listen for incoming connections.
79      * Implements the central socket server loop. Incoming connections will be stored.
80      */
81     virtual void run(
82     );
83 
84     /**
85      * Remove a connection object from the list of available connections.
86      * Detaching is required for notification connections wich are never used to transfer command
87      * data from TLCs to the driver. If the function succeeds, the connection object will no longer
88      * be handled by the server.
89      *
90      * @param connection The connection object to remove.
91      */
92     virtual void detachConnection(
93         Connection *connection
94     );
95 
96 protected:
97     int serverSock;
98     string socketAddr;
99     ConnectionHandler   *connectionHandler; /**< Connection handler registered to the server */
100 
101 private:
102     connectionList_t    peerConnections; /**< Connections to devices */
103 
104 };
105 
106 #endif /* SERVER_H_ */
107 
108 /** @} */
109