1 //  Copyright (C) 2011, 2014 Ben Asselstine
2 //
3 //  This program is free software; you can redistribute it and/or modify
4 //  it under the terms of the GNU General Public License as published by
5 //  the Free Software Foundation; either version 3 of the License, or
6 //  (at your option) any later version.
7 //
8 //  This program is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 //  GNU Library General Public License for more details.
12 //
13 //  You should have received a copy of the GNU General Public License
14 //  along with this program; if not, write to the Free Software
15 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 //  02110-1301, USA.
17 
18 //#include <iostream>
19 #include <fstream>
20 #include <sstream>
21 #include "hosted-game.h"
22 #include "advertised-game.h"
23 #include "xmlhelper.h"
24 #include "profile.h"
25 
26 
27 Glib::ustring HostedGame::d_tag = "hostedgame";
28 
29 //#define debug(x) {std::cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<std::endl<<std::flush;}
30 #define debug(x)
31 
HostedGame(AdvertisedGame * advertised_game)32 HostedGame::HostedGame(AdvertisedGame *advertised_game)
33 {
34   unresponsive = false;
35   d_pid = 0;
36   //watch out, not copying here.
37   d_advertised_game = advertised_game;
38 }
39 
HostedGame(XML_Helper * helper)40 HostedGame::HostedGame(XML_Helper *helper)
41 {
42   unresponsive = false;
43   helper->getData(d_pid, "pid");
44   helper->registerTag(AdvertisedGame::d_tag_name,
45 		      sigc::mem_fun(*this, &HostedGame::loadAdvertisedGame));
46 }
47 
~HostedGame()48 HostedGame::~HostedGame()
49 {
50   delete d_advertised_game;
51 }
52 
save(XML_Helper * helper) const53 bool HostedGame::save(XML_Helper* helper) const
54 {
55   bool retval = true;
56   retval &= helper->openTag(d_tag);
57   retval &= helper->saveData("pid", d_pid);
58   retval &= d_advertised_game->saveEntry(helper);
59   retval &= helper->closeTag();
60   return retval;
61 }
62 
loadAdvertisedGame(Glib::ustring tag,XML_Helper * helper)63 bool HostedGame::loadAdvertisedGame(Glib::ustring tag, XML_Helper *helper)
64 {
65   if (tag == AdvertisedGame::d_tag_name)
66     {
67       d_advertised_game = new AdvertisedGame(helper);
68       return true;
69     }
70   return false;
71 }
72 
ping()73 void HostedGame::ping()
74 {
75   getAdvertisedGame()->pinged.connect
76     (sigc::mem_fun(*this, &HostedGame::on_pinged));
77   getAdvertisedGame()->ping();
78 }
79 
on_pinged(bool success)80 void HostedGame::on_pinged(bool success)
81 {
82   if (!success)
83     cannot_ping_game.emit(this);
84 }
85