]> git.vomp.tv Git - vompclient.git/blob - vconnect.cc
Control/main/winmain/util reorganisation
[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 "control.h"
24 #include "i18n.h"
25 #include "boxstack.h"
26 #include "message.h"
27 #include "log.h"
28 #include "vdr.h"
29 #include "wol.h"
30 #include "vserverselect.h"
31 #include "messagequeue.h"
32 #include "util.h"
33
34 #include "vconnect.h"
35
36 VConnect::VConnect()
37 {
38   boxstack = BoxStack::getInstance();
39   vdr = VDR::getInstance();
40   logger = Log::getInstance();
41
42   setSize(400, 200);
43   createBuffer();
44   if (Video::getInstance()->getFormat() == Video::PAL)
45   {
46     setPosition(170, 200);
47   }
48   else
49   {
50     setPosition(160, 150);
51   }
52
53   exitable = 0;
54 }
55
56 VConnect::~VConnect()
57 {
58   threadReqQuit = true;
59   vdpc.stop();
60   stop();
61 }
62
63 void VConnect::draw()
64 {
65   VInfo::draw();
66   logger->log("VConnect", Log::DEBUG, "Draw done");
67 }
68
69 int VConnect::handleCommand(int /* command */)
70 {
71   return 1;
72 }
73
74 void VConnect::run()
75 {
76   threadMutex.lock();
77   threadReqQuit = false;
78   connectThread = std::thread([this]
79   {
80     threadMutex.lock();
81     threadMutex.unlock();
82     threadMethod();
83   });
84   threadMutex.unlock();
85 }
86
87 void VConnect::stop()
88 {
89   threadMutex.lock();
90   threadReqQuit = true;
91   threadCond.notify_one();
92   threadMutex.unlock();
93   connectThread.join();
94 }
95
96 void VConnect::threadMethod()
97 {
98   logger->log("VConnect", Log::DEBUG, "start threadMethod");
99
100   ULONG delay = 0;
101   int success;
102
103   std::unique_lock<std::mutex> ul(threadMutex, std::defer_lock);
104
105   const std::string& commandLineServer = getCommandLineServer();
106
107   if (!vdpc.init())
108   {
109     logger->log("VConnect", Log::CRIT, "Failed to init VDPC");
110     return;
111   }
112
113   do
114   {
115     if (!commandLineServer.empty()) // Server is specified, fake a servers array
116     {
117       //servers.emplace_back(commandLineServer, "", 3024, 0);
118       vdpc.TEMPaddCLIServer(commandLineServer); // FIXME move to new config system NCONFIG
119     }
120     else
121     {
122       setOneLiner(tr("Locating server"));
123       draw();
124       boxstack->update(this);
125       vdpc.go();
126       if (threadReqQuit) return;
127     }
128
129     for (ULONG i = 0; i < vdpc.numServers(); i++)
130       logger->log("VConnect", Log::INFO, "Found server: %i %s %s %u", vdpc[i].ipVersion, vdpc[i].ip.c_str(), vdpc[i].name.c_str(), vdpc[i].port, vdpc[i].version);
131
132     if (vdpc.numServers() == 1)
133     {
134       selectedServer = 0;
135     }
136     else
137     {
138       selectedServer = -1;
139       VServerSelect* vs = new VServerSelect(vdpc, this);
140       vs->draw();
141       boxstack->add(vs);
142       boxstack->update(vs);
143
144       ul.lock();
145       if (threadReqQuit) { ul.unlock(); return; }
146       threadCond.wait(ul);
147       ul.unlock();
148     }
149
150     if (threadReqQuit) return;
151
152     logger->log("VConnect", Log::NOTICE, "Connecting to server at %s %u", vdpc[selectedServer].ip.c_str(), vdpc[selectedServer].port);
153     Wol::getInstance()->setWakeUpIP(vdpc[selectedServer].ip.c_str());
154     vdr->setServerIP(vdpc[selectedServer].ip.c_str());
155     vdr->setServerPort(vdpc[selectedServer].port);
156
157     setOneLiner(tr("Connecting to VDR"));
158     draw();
159     boxstack->update(this);
160
161     success = vdr->connect();
162     if (success)
163     {
164       logger->log("VConnect", Log::DEBUG, "Connected ok, doing login");
165       unsigned int version_server_min, version_server_max, version_client;
166       int subtitles;
167       success = vdr->doLogin(&version_server_min, &version_server_max, &version_client, Control::getInstance()->getASLList(), subtitles);
168       Control::getInstance()->setSubDefault(subtitles);
169
170       if (!success)
171       {
172         vdr->disconnect();
173         if (version_server_min >version_client || version_client > version_server_max) {
174                 char buffer[1024];
175                 sprintf(buffer,"Version error: s min: %x s max: %x c: %x",version_server_min,version_server_max,version_client);
176                 setOneLiner(buffer);
177         } else {
178                 setOneLiner(tr("Login failed"));
179         }
180         delay = 3000;
181       }
182     }
183     else
184     {
185       setOneLiner(tr("Connection failed"));
186       delay = 3000;
187     }
188
189     draw();
190     boxstack->update(this);
191     MILLISLEEP(delay);
192   } while(!success);
193
194   logger->log("VConnect", Log::INFO, "Send VDR connected message");
195   Message* m = new Message(); // Must be done after this thread ends
196   m->from = this;
197   m->p_to = Message::CONTROL;
198   m->message = Message::VDR_CONNECTED;
199   MessageQueue::getInstance()->postMessage(m);
200 }
201
202 void VConnect::processMessage(Message* m)
203 {
204   if (m->message == Message::SERVER_SELECTED)
205   {
206     selectedServer = m->parameter;
207     threadCond.notify_one();
208   }
209 }