]> git.vomp.tv Git - vompclient.git/blob - vconnect.cc
Event class to std::string
[vompclient.git] / vconnect.cc
1 /*
2     Copyright 2004-2020 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP.  If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include "defines.h"
21 #include "video.h"
22 #include "colour.h"
23 #include "command.h"
24 #include "i18n.h"
25 #include "boxstack.h"
26 #include "message.h"
27 #include "log.h"
28 #include "wol.h"
29 #include "vserverselect.h"
30 #include "messagequeue.h"
31
32 #include "vconnect.h"
33
34 VConnect::VConnect()
35 {
36   boxstack = BoxStack::getInstance();
37   vdr = VDR::getInstance();
38   logger = Log::getInstance();
39
40   setSize(400, 200);
41   createBuffer();
42   if (Video::getInstance()->getFormat() == Video::PAL)
43   {
44     setPosition(170, 200);
45   }
46   else
47   {
48     setPosition(160, 150);
49   }
50
51   exitable = 0;
52 }
53
54 VConnect::~VConnect()
55 {
56   threadReqQuit = true;
57   vdr->cancelFindingServer();
58   stop();
59 }
60
61 void VConnect::draw()
62 {
63   VInfo::draw();
64   logger->log("VConnect", Log::DEBUG, "Draw done");
65 }
66
67 int VConnect::handleCommand(int /* command */)
68 {
69   return 1;
70 }
71
72 void VConnect::run()
73 {
74   threadMutex.lock();
75   threadReqQuit = false;
76   connectThread = std::thread([this]
77   {
78     threadMutex.lock();
79     threadMutex.unlock();
80     threadMethod();
81   });
82   threadMutex.unlock();
83 }
84
85 void VConnect::stop()
86 {
87   threadMutex.lock();
88   threadReqQuit = true;
89   threadCond.notify_one();
90   threadMutex.unlock();
91   connectThread.join();
92 }
93
94 void VConnect::threadMethod()
95 {
96   ULONG delay = 0;
97   int success;
98
99   std::unique_lock<std::mutex> ul(threadMutex, std::defer_lock);
100
101   const std::string& commandLineServer = getCommandLineServer();
102
103   do
104   {
105     if (!commandLineServer.empty()) // Server is specified, fake a servers array
106     {
107       servers.emplace_back(commandLineServer, "", 3024, 0);
108     }
109     else
110     {
111       setOneLiner(tr("Locating server"));
112       draw();
113       boxstack->update(this);
114       vdr->findServers(servers);
115       if (threadReqQuit) return;
116     }
117
118     if (servers.size() == 1)
119     {
120       selectedServer = 0;
121     }
122     else
123     {
124       selectedServer = -1;
125       VServerSelect* vs = new VServerSelect(servers, this);
126       vs->draw();
127       boxstack->add(vs);
128       boxstack->update(vs);
129
130       ul.lock();
131       if (threadReqQuit) { ul.unlock(); return; }
132       threadCond.wait(ul);
133       ul.unlock();
134     }
135
136     if (threadReqQuit) return;
137
138     logger->log("VConnect", Log::NOTICE, "Connecting to server at %s %u", servers[selectedServer].ip.c_str(), servers[selectedServer].port);
139     Wol::getInstance()->setWakeUpIP(servers[selectedServer].ip.c_str());
140     vdr->setServerIP(servers[selectedServer].ip.c_str());
141     vdr->setServerPort(servers[selectedServer].port);
142
143     setOneLiner(tr("Connecting to VDR"));
144     draw();
145     boxstack->update(this);
146
147     success = vdr->connect();
148     if (success)
149     {
150       logger->log("VConnect", Log::DEBUG, "Connected ok, doing login");
151       unsigned int version_server_min, version_server_max, version_client;
152       int subtitles;
153       success = vdr->doLogin(&version_server_min, &version_server_max, &version_client, Command::getInstance()->getASLList(), subtitles);
154       Command::getInstance()->setSubDefault(subtitles);
155
156       if (!success)
157       {
158         vdr->disconnect();
159         if (version_server_min >version_client || version_client > version_server_max) {
160                 char buffer[1024];
161                 sprintf(buffer,"Version error: s min: %x s max: %x c: %x",version_server_min,version_server_max,version_client);
162                 setOneLiner(buffer);
163         } else {
164                 setOneLiner(tr("Login failed"));
165         }
166         delay = 3000;
167       }
168     }
169     else
170     {
171       setOneLiner(tr("Connection failed"));
172       delay = 3000;
173     }
174
175     draw();
176     boxstack->update(this);
177     MILLISLEEP(delay);
178
179     servers.clear(); // In case we're going around
180
181   } while(!success);
182
183   logger->log("VConnect", Log::INFO, "Send VDR connected message");
184   Message* m = new Message(); // Must be done after this thread ends
185   m->from = this;
186   m->to = Command::getInstance();
187   m->message = Message::VDR_CONNECTED;
188   MessageQueue::getInstance()->postMessage(m);
189 }
190
191 void VConnect::processMessage(Message* m)
192 {
193   if (m->message == Message::SERVER_SELECTED)
194   {
195     selectedServer = m->parameter;
196     threadCond.notify_one();
197   }
198 }