Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
main.cpp
1 
2 /***************************************************************************
3  * main.cpp - World Info Viewer
4  *
5  * Created: Wed April 09 20:04:46 2008
6  * Copyright 2008 Daniel Beck
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include <tools/worldinfo_viewer/worldinfo_viewer.h>
24 #include <tools/worldinfo_viewer/backend_thread.h>
25 #include <worldinfo_utils/data_container.h>
26 #include <utils/system/hostinfo.h>
27 #include <utils/time/clock.h>
28 #include <config/sqlite.h>
29 
30 #include <iostream>
31 #include <string>
32 
33 using namespace fawkes;
34 
35 int main(int argc, char** argv)
36 {
38 
39  HostInfo* host_info = new HostInfo();
40  Configuration* config = new SQLiteConfiguration(CONFDIR);
41  config->load(host_info->short_name(), "default");
42  delete host_info;
43 
44  std::string addr;
45  unsigned int port;
46  std::string key;
47  std::string iv;
48  try
49  {
50  addr = config->get_string("/worldinfo/multicast_addr");
51  port = config->get_uint("/worldinfo/udp_port");
52  key = config->get_string("/worldinfo/encryption_key");
53  iv = config->get_string("/worldinfo/encryption_iv");
54  }
55  catch (Exception &e)
56  {
57  delete config;
58  e.append("Could not get required configuration data for world info viewer");
59  e.print_trace();
60  throw;
61  }
62 
63  delete config;
64 
65  Clock* clock = Clock::instance();
66  WorldInfoDataContainer* data_container = new WorldInfoDataContainer(clock, 10000);
67  WorldInfoViewerBackendThread* backend_thread =
68  new WorldInfoViewerBackendThread( data_container, addr.c_str(), port,
69  key.c_str(), iv.c_str() );
70 
71  backend_thread->start();
72 
73  try
74  {
75  Gtk::Main kit(argc, argv);
76 #ifdef GLIBMM_EXCEPTIONS_ENABLED
77  Glib::RefPtr<Gtk::Builder> builder =
78  Gtk::Builder::create_from_file( RESDIR"/guis/worldinfo_viewer/"
79  "worldinfo_viewer.ui" );
80 #else
81  std::auto_ptr<Gtk::BuilderError> error;
82  Glib::RefPtr<Gtk::Builder> builder =
83  Gtk::Builder::create_from_file(RESDIR"/guis/worldinfo_viewer/worldinfo_viewer.ui", error);
84  if (error.get()) {
85  throw fawkes::Exception("Failed to load UI file: %s", error->what().c_str());
86  }
87 #endif
88 
89  WorldInfoViewer viewer(builder, data_container);
90  backend_thread->new_gamestate_data().connect( sigc::mem_fun(viewer, &WorldInfoViewer::gamestate_changed ) );
91 
92  kit.run( viewer.get_window() );
93  }
94  catch (std::exception const& e)
95  {
96  std::cerr << "Error: " << e.what() << std::endl;
97  }
98 
99  backend_thread->cancel();
100  backend_thread->join();
101  delete backend_thread;
102 
103  delete data_container;
104  Clock::finalize();
105 
107 
108  return 0;
109 }