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