-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpop3server.cpp
More file actions
100 lines (83 loc) · 3.05 KB
/
pop3server.cpp
File metadata and controls
100 lines (83 loc) · 3.05 KB
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
/*
* File: pop3server.cpp
* Author: Dirk Vermeir
* Edited by: Wouter Van Rossem
*
* Created on 23 juli 2009, 14:43
*/
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <dvutil/debug.h>
#include <dvutil/props.h>
#include <dvthread/logstream.h>
#include <dvnet/serversocket.h>
#include "player.h"
#include "manager.h"
// In a production system, server_log would be linked
// to a file stream. Alternatively, it can be launched
// like so:
//
// nohup ./pop3 config-file >& /var/log/pop3server_log.txt &
std::ofstream logfile ("pop3.log", std::ios_base::app);
Dv::Thread::logstream pop3server_log (logfile, "pop3server");
Dv::Debug debug (&pop3server_log, 0);
int
main (int argc, char* argv[])
{
static const char* usage = "pop3 config-file"; // Manual.
// Put everything in a try block, so we catch all I/O and
// network errors, among others.
try
{
// First check whether the required config file argument is
// present (there is no default).
if ( argc != 2 )
throw std::runtime_error(usage);
Dv::Props config;
{ // read configuration info
std::ifstream ifconfig(argv[1]);
if ( !ifconfig )
throw std::runtime_error(std::string(argv[1]) + ": cannot open");
ifconfig >> config;
}
// Set up the manager object. Note that this will also start
// a thread that will actually handle player requests.
Manager manager("pop3manager", config, &debug);
// The delay to use througout for I/O operations, mailbox waiting etc.
size_t delay = config("timeout");
// Set up a server socket listening on the port
Dv::Net::ServerSocket server(config("port").get<int>());
// Each time around the main loop, check whether the manager wants to stop.
while ( !manager.done() )
{
// The following times out after delay millisec, thus we will check
// manager::done regularly.
if ( server.connection(delay) )
{
// The delay argument to ServerSocket::accept() makes e.g.
// getline(socket) time out after delay millisecs, ensuring that
// we can often check conditions in a player's main loop.
Dv::shared_ptr<Dv::Net::Socket> socket(server.accept(delay));
// The timeout argument to Player::make ensures that the player
// will e.g. timeout after not receiving a reply from the
// manager.
Player::make(manager, socket, delay, config("debuglevel"), &debug)->start();
}
}
// The manager wants to stop: kill it. This will kill all
// remaining players and wait for them to finish, then it
// will kill the manager thread and wait for it to finish before
// returning.
manager.kill();
}
catch (std::exception& e)
{
// You get here if anything goes wrong in the try block.
pop3server_log << "pop3server error: " << e.what() << std::endl;
return 2;
}
pop3server_log << "bye" << std::endl;
logfile.close();
return 0;
}