1 //  Copyright (C) 2011, 2014, 2015 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 "advertised-game.h"
22 #include "xmlhelper.h"
23 #include "profile.h"
24 #include "network-connection.h"
25 #include "connection-manager.h"
26 
27 
28 Glib::ustring AdvertisedGame::d_tag_name = "advertisedgame";
29 
30 //#define debug(x) {cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<endl<<flush;}
31 #define debug(x)
32 
AdvertisedGame(GameScenario * scen,Profile * p)33 AdvertisedGame::AdvertisedGame(GameScenario *scen, Profile *p)
34 	:RecentlyPlayedNetworkedGame(scen, p)
35 {
36   d_creation_date.assign_current_time();
37   d_last_pinged_date.assign_current_time();
38   d_profile = new Profile(*p);
39 }
40 
41 
AdvertisedGame(const RecentlyPlayedNetworkedGame & orig,Profile * p)42 AdvertisedGame::AdvertisedGame(const RecentlyPlayedNetworkedGame &orig, Profile *p)
43         :RecentlyPlayedNetworkedGame(orig)
44 {
45   d_creation_date.assign_current_time();
46   d_last_pinged_date.assign_current_time();
47   d_profile = new Profile(*p);
48 }
49 
AdvertisedGame(const AdvertisedGame & orig)50 AdvertisedGame::AdvertisedGame(const AdvertisedGame &orig)
51         :RecentlyPlayedNetworkedGame(orig),
52         d_creation_date(orig.d_creation_date),
53         d_last_pinged_date(orig.d_last_pinged_date)
54 {
55   d_profile = new Profile(*orig.d_profile);
56 }
57 
AdvertisedGame(XML_Helper * helper)58 AdvertisedGame::AdvertisedGame(XML_Helper *helper)
59 	:RecentlyPlayedNetworkedGame(helper)
60 {
61   Glib::ustring s;
62   helper->getData(s, "created_on");
63   d_creation_date.assign_from_iso8601(s);
64   helper->getData(s, "last_pinged_on");
65   d_last_pinged_date.assign_from_iso8601(s);
66   helper->registerTag(Profile::d_tag,
67 		      sigc::mem_fun(*this, &AdvertisedGame::loadProfile));
68 }
69 
~AdvertisedGame()70 AdvertisedGame::~AdvertisedGame()
71 {
72   delete d_profile;
73 }
74 
doSave(XML_Helper * helper) const75 bool AdvertisedGame::doSave(XML_Helper *helper) const
76 {
77   bool retval = true;
78   Glib::ustring s = d_creation_date.as_iso8601();
79   retval &= helper->saveData("created_on", s);
80   s = d_last_pinged_date.as_iso8601();
81   retval &= helper->saveData("last_pinged_on", s);
82   retval &= helper->saveData("host", getHost());
83   retval &= helper->saveData("port", getPort());
84   retval &= d_profile->save(helper);
85   return retval;
86 }
87 
saveEntry(XML_Helper * helper) const88 bool AdvertisedGame::saveEntry(XML_Helper* helper) const
89 {
90   bool retval = true;
91   retval &= helper->openTag(d_tag_name);
92   retval &= dynamic_cast<const RecentlyPlayedGame*>(this)->saveContents(helper);
93   retval &= helper->closeTag();
94   return retval;
95 }
96 
loadProfile(Glib::ustring tag,XML_Helper * helper)97 bool AdvertisedGame::loadProfile(Glib::ustring tag, XML_Helper *helper)
98 {
99   if (tag == Profile::d_tag)
100     {
101       d_profile = new Profile(helper);
102       return true;
103     }
104   return false;
105 }
106 
ping()107 void AdvertisedGame::ping()
108 {
109   NetworkConnection *conn = ConnectionManager::create_connection();
110 
111   conn->connected.connect
112     (sigc::bind(sigc::mem_fun(*this, &AdvertisedGame::on_connected_to_game),
113                 conn));
114   conn->connection_failed.connect
115     (sigc::bind(sigc::mem_fun(*this,
116                               &AdvertisedGame::on_could_not_connect_to_game),
117                 conn));
118   conn->connectToHost(getHost(), getPort());
119 }
120 
on_connected_to_game(NetworkConnection * conn)121 void AdvertisedGame::on_connected_to_game(NetworkConnection *conn)
122 {
123   conn->tear_down_connection();
124   d_last_pinged_date.assign_current_time();
125   pinged.emit(true);
126 }
127 
on_could_not_connect_to_game(NetworkConnection * conn)128 void AdvertisedGame::on_could_not_connect_to_game(NetworkConnection *conn)
129 {
130   conn->tear_down_connection();
131   pinged.emit(false);
132 }
133