2 Copyright 2004-2020 Chris Tallon
4 This file is part of VOMP.
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.
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.
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/>.
31 #include "vserverselect.h"
32 #include "messagequeue.h"
37 static const char* TAG = "VConnect";
41 boxstack = BoxStack::getInstance();
42 vdr = VDR::getInstance();
43 logger = LogNT::getInstance();
47 if (Video::getInstance()->getFormat() == Video::PAL)
49 setPosition(170, 200);
53 setPosition(160, 150);
69 logger->debug(TAG, "Draw done");
72 int VConnect::handleCommand(int /* command */)
80 threadReqQuit = false;
81 connectThread = std::thread([this]
94 vdr->abortConnect(); // If this and vdr are connecting, cancel it
95 threadCond.notify_one();
100 void VConnect::threadMethod()
102 logger->debug(TAG, "start threadMethod");
107 std::unique_lock<std::mutex> ul(threadMutex, std::defer_lock);
109 VDRServer configServerStr;
110 const VDRServer* selectedServerStr = NULL;
112 bool serverFromConfig{}; // FIXME Redesign all this.
114 if (Config::getInstance()->getString("server", "address", configServerStr.ip))
116 configServerStr.port = 3024;
118 if (Config::getInstance()->getInt("server", "port", newPort))
120 configServerStr.port = newPort;
123 selectedServerStr = &configServerStr;
124 serverFromConfig = true;
126 else // No server specified in config. Start VDPC
130 logger->crit(TAG, "Failed to init VDPC");
138 if (!selectedServerStr)
140 setOneLiner(tr("Locating server"));
142 boxstack->update(this);
144 if (threadReqQuit) return;
146 for (ULONG i = 0; i < vdpc.numServers(); i++)
147 logger->info(TAG, "Found server: {} {} {} {}", vdpc[i].ipVersion, vdpc[i].ip.c_str(), vdpc[i].name.c_str(), vdpc[i].port, vdpc[i].version);
149 if (vdpc.numServers() == 1)
152 selectedServerStr = &vdpc[0];
157 VServerSelect* vs = new VServerSelect(vdpc, this); // FIXME is this deleted?
160 boxstack->update(vs);
163 if (threadReqQuit) { ul.unlock(); return; }
166 // This thread has been notified by VServerSelect. selectedServer is now an index into vdpc of the chosen server
168 selectedServerStr = &vdpc[selectedServer];
173 if (threadReqQuit) return;
177 logger->info(TAG, "Connecting to server at {} {}", selectedServerStr->ip, selectedServerStr->port);
178 Wol::getInstance()->setWakeUpIP(selectedServerStr->ip.c_str());
179 vdr->setServerIP(selectedServerStr->ip.c_str());
180 vdr->setServerPort(selectedServerStr->port);
182 // In case of go around, clear selectedServerStr here if it was discovered
183 if (!serverFromConfig) selectedServerStr = NULL;
186 setOneLiner(tr("Connecting to VDR"));
188 boxstack->update(this);
190 if (threadReqQuit) return;
191 success = vdr->connect();
192 if (threadReqQuit) return;
196 logger->debug(TAG, "Connected ok, doing login");
197 unsigned int version_server_min, version_server_max, version_client;
199 success = vdr->doLogin(&version_server_min, &version_server_max, &version_client, Control::getInstance()->getASLList(), subtitles);
200 Control::getInstance()->setSubDefault(subtitles);
205 if (version_server_min >version_client || version_client > version_server_max) {
207 sprintf(buffer,"Version error: s min: %x s max: %x c: %x",version_server_min,version_server_max,version_client);
210 setOneLiner(tr("Login failed"));
217 setOneLiner(tr("Connection failed"));
222 boxstack->update(this);
224 MILLISLEEP(delay); // FIXME wait on cond?
225 if (threadReqQuit) return;
228 logger->info(TAG, "Send VDR connected message");
229 Message* m = new Message(); // Must be done after this thread ends
231 m->p_to = Message::CONTROL;
232 m->message = Message::VDR_CONNECTED;
233 MessageQueue::getInstance()->postMessage(m);
236 void VConnect::processMessage(Message* m)
238 if (m->message == Message::SERVER_SELECTED)
240 selectedServer = m->parameter;
241 threadCond.notify_one();