summaryrefslogtreecommitdiffstats
path: root/network/akgnet_server.hh
blob: 40d9129772843e1d5bbaac7f474d38d8982f41de (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
/*
* This file is part of rasdaman community.
*
* Rasdaman community 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 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community 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 rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
/
/**
 * INCLUDE: akgnet_server.hh
 *
 * MODULE:  akg network
 * CLASS:   GenericServer, BlockingServer
 *
 * COMMENTS:
 * Namespace akg
 * 
*/

#ifndef AKGNET_SERVER_HH
#define AKGNET_SERVER_HH

//#### Includes #################################

#include "akgnet_socket.hh"
#include "akgnet_selector.hh"
#include "akgnet_commbuffer.hh"

//###############################################


namespace akg
  {
/** Abstract base class for servers. Offers basic functionality
    for opening the listen socket and accepting a new connection
    and other helper functions for more evoluate servers
*/   
class GenericServer
  {
    public:
      /// Default constructor
      GenericServer() throw();
      
      /// Destructor
      virtual ~GenericServer() throw();
      
      //*************************
      /** Pure function to run the server. Has to initialize 
          the listen socket, than makes a loop by listening,
          accepting and dispatching the connection 
	  for processing. It should'n throw, it has to handle
	  correcty every exception
       */
      virtual bool runServer() throw() =0;
      //*************************
      
      /// Instructs the server to leave the loop (runServer())
      void shouldExit() throw();
    
      /// Sets the listen port
      void setListenPort(int) throw();
      
      /// Returns the listen port
      int  getListenPort() throw();
      
      /** Sets the timeout, how much time the selector should
          wait for incomming requests
      */
      void setTimeout(int sec,int milisec) throw();
      
      /// Disables timeout, means wait unlimited
      void disableTimeout() throw();
      
    protected:
      /// Init the listen socket
      bool initListenSocket(int port, bool nonblocking) throw();
      
      /** Connects a new client by accepting the connection 
          and setting the ServerSocket in read modus
      */
      bool connectNewClient(ServerSocket&) throw();
      
      /** Closes the given Socket and removes it
          from the Selector
      */
      void closeSocket(Socket&) throw();
      
      ListenSocket listenSocket;
      int          listenPort;
      
      Selector     selector;
      
      bool exitRequest;
      
    private:
      /// unimplemented, objects of this type can't be copied
      GenericServer(const GenericServer&);
      /// unimplemented, objects of this type can't be copied
      GenericServer& operator=(const GenericServer&);
   }; 


/** Base class for a simple blocking server, capable
    of dealing with a single client. Don't use except for
    very simple cases. 
    This version doesn't care much about errors
*/     
class BlockingServer : public GenericServer
  {
    public:
      /// Default constructor
      BlockingServer()  throw();
      /// Destructor
      ~BlockingServer() throw();
      
      /** runs the server. Accepts only one connection
          and blocks until the request is done
      */
      bool runServer() throw();
    protected:
      //************************************************
      /** Pure function to process the request. It has to read,
          process and write the answer, because afterwards 
	  the socket is closed. Don't throw!
	*/ 
      virtual void executeRequest(ServerSocket&) throw() =0;
      
      /** Pure function to execute on timeout. Don't throw!
      */
      virtual void executeTimeout() throw() =0;
      //************************************************
    private:
      ServerSocket serverSocket;
      
      /// unimplemented, objects of this type can't be copied
      BlockingServer(const BlockingServer&);
      /// unimplemented, objects of this type can't be copied
      BlockingServer& operator=(const BlockingServer&);
   }; 
   
   
} //namespace
#endif