ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/dclient/src/frontend/client/main.cpp
Revision: 1.3
Committed: Mon Oct 18 14:11:49 2010 UTC (16 years ago) by sf-pippijn
Branch: MAIN
Changes since 1.2: +10 -10 lines
Log Message:
check and log but do not die when ncurses stuff fails

File Contents

# User Rev Content
1 sf-pippijn 1.1 #include "autoconf.h"
2    
3     #include "dclient/asio_service_manager.h"
4     #include "dclient/client.h"
5     #include "dclient/input_handler.h"
6     #include "dclient/query_handler.h"
7     #include "dclient/renderer.h"
8     #include "dclient/widgets.h"
9 sf-pippijn 1.2 #include "dclient/widgets/inventory.h"
10 sf-pippijn 1.1
11     #include "deliantra/asio_communicator.h"
12     #include "deliantra/command_parser.h"
13     #include "deliantra/handshake_handler.h"
14 sf-pippijn 1.2 #include "deliantra/inventory_manager.h"
15 sf-pippijn 1.1 #include "deliantra/message_logger.h"
16    
17     #include "deliantra/data/item.h"
18    
19     #include "adt/bind.h"
20    
21     #include <ndk++/application.hh>
22    
23     #include <boost/asio/posix/stream_descriptor.hpp>
24 sf-pippijn 1.3 #include <boost/asio/deadline_timer.hpp>
25 sf-pippijn 1.1
26 sf-pippijn 1.3 #include <csignal>
27 sf-pippijn 1.1 #include <cstring>
28     #include <iomanip>
29     #include <memory>
30    
31     template<typename Handler>
32     struct fd_listener
33     {
34     fd_listener (boost::asio::io_service &io_service, int fd, Handler &handler)
35     : in (io_service, fd)
36     , handler (handler)
37     , reading (false)
38     {
39     read_more ();
40     }
41    
42     private:
43     void read_more ()
44     {
45     if (!reading)
46     {
47     reading = true;
48     in.async_read_some (boost::asio::null_buffers (), bind (&fd_listener::handle_read, this));
49     }
50     }
51    
52     void handle_read (boost::system::error_code const &error, size_t length)
53     {
54     reading = false;
55     handler.handle_read (error, length);
56     read_more ();
57     }
58    
59     boost::asio::posix::stream_descriptor in;
60     Handler &handler;
61     bool reading;
62     };
63    
64     static void
65     start_game ( std::string const &server
66     , std::string const &port
67     , std::string const &user
68     , std::string const &pass
69     )
70     {
71     // ncurses
72     std::auto_ptr<ndk::application> app (&ndk::app ());
73    
74     // asio
75     boost::asio::io_service io_service;
76    
77     // deliantra
78     asio_communicator comm (io_service, server, port);
79     command_parser cmdp;
80     handshake_handler hh (comm);
81     message_logger logger;
82 sf-pippijn 1.2 inventory_manager invman;
83 sf-pippijn 1.1
84     // dclient
85     client cli (comm, cmdp);
86     widgets ui;
87     renderer rend (ui);
88     asio_service_manager svs (io_service);
89     input_handler input (comm, svs, ui);
90     fd_listener<input_handler> in (io_service, STDIN_FILENO, input);
91     query_handler qh (comm, ui, user, pass);
92    
93     // link them all together
94 sf-pippijn 1.2 invman.set_inventory_view (*ui.inventory);
95     invman.set_container_view (*ui.container);
96 sf-pippijn 1.1 comm.set_packet_handler (cli);
97     cmdp.add_command_handler (hh);
98     cmdp.add_command_handler (qh);
99 sf-pippijn 1.2 cmdp.add_command_handler (invman);
100 sf-pippijn 1.1 cmdp.add_command_handler (rend);
101     cmdp.add_command_handler (logger);
102    
103     // run the event loop
104     io_service.run ();
105     }
106    
107    
108     int
109     main (int argc, char *argv[])
110     try
111     {
112     if (argc == 2 && !strcmp (argv[1], "--version"))
113     {
114     printf ("%s version %s\n", PACKAGE_NAME, PACKAGE_VERSION);
115     return EXIT_SUCCESS;
116     }
117    
118     if (argc < 3 || !strcmp (argv[1], "--help"))
119     {
120     printf ("Usage: %s <host> <port> [user] [password]\n", PACKAGE);
121     return EXIT_SUCCESS;
122     }
123    
124     std::string server, port, user, pass;
125    
126     server = argv[1];
127     port = argv[2];
128     if (argc == 5)
129     {
130     user = argv[3];
131     pass = argv[4];
132     }
133    
134     start_game (server, port, user, pass);
135    
136     return EXIT_SUCCESS;
137     }
138     catch (std::exception const &e)
139     {
140 sf-pippijn 1.3 LOG ("exception") << e.what () << "\n";
141 sf-pippijn 1.1 printf ("exception caught: %s\n", e.what ());
142 sf-pippijn 1.3 return EXIT_FAILURE;
143 sf-pippijn 1.1 }
144     catch (int i)
145     {
146 sf-pippijn 1.3 LOG ("exception") << '0' << std::oct << std::setfill ('0') << std::setw (3) << i << "\n";
147 sf-pippijn 1.1 printf ("exception caught: 0%03o\n", i);
148 sf-pippijn 1.3 return EXIT_FAILURE;
149 sf-pippijn 1.1 }
150     catch (char const *s)
151     {
152 sf-pippijn 1.3 LOG ("exception") << s << "\n";
153 sf-pippijn 1.1 printf ("exception caught: %s\n", s);
154 sf-pippijn 1.3 return EXIT_FAILURE;
155 sf-pippijn 1.1 }
156     catch (...)
157     {
158 sf-pippijn 1.3 LOG ("exception");
159 sf-pippijn 1.1 puts ("exception caught");
160 sf-pippijn 1.3 return EXIT_FAILURE;
161 sf-pippijn 1.1 }