~ubuntu-branches/debian/experimental/nzbget/experimental

« back to all changes in this revision

Viewing changes to .pc/fix_fsf_address.patch/Frontend.cpp

  • Committer: Package Import Robot
  • Author(s): Andreas Moog
  • Date: 2013-07-18 14:50:28 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130718145028-qhxse81w1sj5w424
Tags: 11.0+dfsg-1
* New upstream release (Closes: #701896)
* Repackage original tarball to remove copies of jquery and twitter-
  bootstrap
* debian/watch: Update for new versioning scheme
* debian/patches: Remove all old patches, add one patch:
  - dont-embed-libraries.patch: Don't install embedded jquery and bootstrap 
    libraries
* debian/combat: Upgrade to debhelper combat 9
* debian/control:
  - Fix Vcs-Git field
  - Adjust debhelper version for combat level 9
  - Add jquery and bootstrap to depends for integrated webserver
  - Add python to recommends for post-processing scripts
  - Bump build-depends on libpar2-dev to support the cancel function
* debian/links:
  - Use the system jquery and bootstrap libraries
* debian/rules:
  - Add get-orig-source target to build modified upstream tarball
* Adjust sample nzbget.conf:
  - Only listen to 127.0.0.1 instead of 0.0.0.0
  - Use nzbget.conf as template for webui configuration
* Adjust sample nzbgetd init file:
  - Point to correct location of nzbget binary

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  This file is part of nzbget
3
 
 *
4
 
 *  Copyright (C) 2004 Sven Henkel <sidddy@users.sourceforge.net>
5
 
 *  Copyright (C) 2007-2010 Andrei Prygounkov <hugbug@users.sourceforge.net>
6
 
 *
7
 
 *  This program is free software; you can redistribute it and/or modify
8
 
 *  it under the terms of the GNU General Public License as published by
9
 
 *  the Free Software Foundation; either version 2 of the License, or
10
 
 *  (at your option) any later version.
11
 
 *
12
 
 *  This program is distributed in the hope that it will be useful,
13
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 *  GNU General Public License for more details.
16
 
 *
17
 
 *  You should have received a copy of the GNU General Public License
18
 
 *  along with this program; if not, write to the Free Software
19
 
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
 
 *
21
 
 * $Revision: 380 $
22
 
 * $Date: 2010-01-30 15:43:58 +0100 (Sat, 30 Jan 2010) $
23
 
 *
24
 
 */
25
 
 
26
 
 
27
 
#ifdef HAVE_CONFIG_H
28
 
#include "config.h"
29
 
#endif
30
 
 
31
 
#ifdef WIN32
32
 
#include "win32.h"
33
 
#endif
34
 
 
35
 
#include <stdlib.h>
36
 
#include <string.h>
37
 
#include <cstdio>
38
 
#include <fstream>
39
 
#ifndef WIN32
40
 
#include <unistd.h>
41
 
#include <arpa/inet.h>
42
 
#include <netinet/in.h>
43
 
#endif
44
 
 
45
 
#include "nzbget.h"
46
 
#include "Options.h"
47
 
#include "Frontend.h"
48
 
#include "Log.h"
49
 
#include "Connection.h"
50
 
#include "MessageBase.h"
51
 
#include "QueueCoordinator.h"
52
 
#include "RemoteClient.h"
53
 
#include "Util.h"
54
 
 
55
 
extern QueueCoordinator* g_pQueueCoordinator;
56
 
extern Options* g_pOptions;
57
 
 
58
 
Frontend::Frontend()
59
 
{
60
 
        debug("Creating Frontend");
61
 
 
62
 
        m_iNeededLogFirstID = 0;
63
 
        m_iNeededLogEntries = 0;
64
 
        m_bSummary = false;
65
 
        m_bFileList = false;
66
 
        m_fCurrentDownloadSpeed = 0;
67
 
        m_lRemainingSize = 0;
68
 
        m_bPauseDownload = false;
69
 
        m_bPauseDownload2 = false;
70
 
        m_fDownloadLimit = 0;
71
 
        m_iThreadCount = 0;
72
 
        m_iPostJobCount = 0;
73
 
        m_iUpTimeSec = 0;
74
 
        m_iDnTimeSec = 0;
75
 
        m_iAllBytes = 0;
76
 
        m_bStandBy = 0;
77
 
        m_iUpdateInterval = g_pOptions->GetUpdateInterval();
78
 
}
79
 
 
80
 
bool Frontend::PrepareData()
81
 
{
82
 
        if (IsRemoteMode())
83
 
        {
84
 
                if (IsStopped())
85
 
                {
86
 
                        return false;
87
 
                }
88
 
                if (!RequestMessages() || ((m_bSummary || m_bFileList) && !RequestFileList()))
89
 
                {
90
 
                        printf("\nUnable to send request to nzbget-server at %s (port %i)    \n", g_pOptions->GetServerIP(), g_pOptions->GetServerPort());
91
 
                        Stop();
92
 
                        return false;
93
 
                }
94
 
        }
95
 
        else
96
 
        {
97
 
                if (m_bSummary)
98
 
                {
99
 
                        m_fCurrentDownloadSpeed = g_pQueueCoordinator->CalcCurrentDownloadSpeed();
100
 
                        m_lRemainingSize = g_pQueueCoordinator->CalcRemainingSize();
101
 
                        m_bPauseDownload = g_pOptions->GetPauseDownload();
102
 
                        m_bPauseDownload2 = g_pOptions->GetPauseDownload2();
103
 
                        m_fDownloadLimit = g_pOptions->GetDownloadRate();
104
 
                        m_iThreadCount = Thread::GetThreadCount();
105
 
                        PostQueue* pPostQueue = g_pQueueCoordinator->LockQueue()->GetPostQueue();
106
 
                        m_iPostJobCount = pPostQueue->size();
107
 
                        g_pQueueCoordinator->UnlockQueue();
108
 
                        g_pQueueCoordinator->CalcStat(&m_iUpTimeSec, &m_iDnTimeSec, &m_iAllBytes, &m_bStandBy);
109
 
                }
110
 
        }
111
 
        return true;
112
 
}
113
 
 
114
 
void Frontend::FreeData()
115
 
{
116
 
        if (IsRemoteMode())
117
 
        {
118
 
                for (Log::Messages::iterator it = m_RemoteMessages.begin(); it != m_RemoteMessages.end(); it++)
119
 
                {
120
 
                        delete *it;
121
 
                }
122
 
                m_RemoteMessages.clear();
123
 
 
124
 
                for (FileQueue::iterator it = m_RemoteQueue.GetFileQueue()->begin(); it != m_RemoteQueue.GetFileQueue()->end(); it++)
125
 
                {
126
 
                        delete *it;
127
 
                }
128
 
                m_RemoteQueue.GetFileQueue()->clear();
129
 
        }
130
 
}
131
 
 
132
 
Log::Messages * Frontend::LockMessages()
133
 
{
134
 
        if (IsRemoteMode())
135
 
        {
136
 
                return &m_RemoteMessages;
137
 
        }
138
 
        else
139
 
        {
140
 
                return g_pLog->LockMessages();
141
 
        }
142
 
}
143
 
 
144
 
void Frontend::UnlockMessages()
145
 
{
146
 
        if (!IsRemoteMode())
147
 
        {
148
 
                g_pLog->UnlockMessages();
149
 
        }
150
 
}
151
 
 
152
 
DownloadQueue* Frontend::LockQueue()
153
 
{
154
 
        if (IsRemoteMode())
155
 
        {
156
 
                return &m_RemoteQueue;
157
 
        }
158
 
        else
159
 
        {
160
 
                return g_pQueueCoordinator->LockQueue();
161
 
        }
162
 
}
163
 
 
164
 
void Frontend::UnlockQueue()
165
 
{
166
 
        if (!IsRemoteMode())
167
 
        {
168
 
                g_pQueueCoordinator->UnlockQueue();
169
 
        }
170
 
}
171
 
 
172
 
bool Frontend::IsRemoteMode()
173
 
{
174
 
        return g_pOptions->GetRemoteClientMode();
175
 
}
176
 
 
177
 
void Frontend::ServerPauseUnpause(bool bPause, bool bSecondRegister)
178
 
{
179
 
        if (IsRemoteMode())
180
 
        {
181
 
                RequestPauseUnpause(bPause, bSecondRegister);
182
 
        }
183
 
        else
184
 
        {
185
 
                if (bSecondRegister)
186
 
                {
187
 
                        g_pOptions->SetPauseDownload2(bPause);
188
 
                }
189
 
                else
190
 
                {
191
 
                        g_pOptions->SetPauseDownload(bPause);
192
 
                }
193
 
        }
194
 
}
195
 
 
196
 
void Frontend::ServerSetDownloadRate(float fRate)
197
 
{
198
 
        if (IsRemoteMode())
199
 
        {
200
 
                RequestSetDownloadRate(fRate);
201
 
        }
202
 
        else
203
 
        {
204
 
                g_pOptions->SetDownloadRate(fRate);
205
 
        }
206
 
}
207
 
 
208
 
void Frontend::ServerDumpDebug()
209
 
{
210
 
        if (IsRemoteMode())
211
 
        {
212
 
                RequestDumpDebug();
213
 
        }
214
 
        else
215
 
        {
216
 
                g_pQueueCoordinator->LogDebugInfo();
217
 
        }
218
 
}
219
 
 
220
 
bool Frontend::ServerEditQueue(QueueEditor::EEditAction eAction, int iOffset, int iID)
221
 
{
222
 
        if (IsRemoteMode())
223
 
        {
224
 
                return RequestEditQueue((eRemoteEditAction)eAction, iOffset, iID);
225
 
        }
226
 
        else
227
 
        {
228
 
                return g_pQueueCoordinator->GetQueueEditor()->EditEntry(iID, true, eAction, iOffset, NULL);
229
 
        }
230
 
        return false;
231
 
}
232
 
 
233
 
void Frontend::InitMessageBase(SNZBRequestBase* pMessageBase, int iRequest, int iSize)
234
 
{
235
 
        pMessageBase->m_iSignature      = htonl(NZBMESSAGE_SIGNATURE);
236
 
        pMessageBase->m_iType = htonl(iRequest);
237
 
        pMessageBase->m_iStructSize = htonl(iSize);
238
 
        strncpy(pMessageBase->m_szPassword, g_pOptions->GetServerPassword(), NZBREQUESTPASSWORDSIZE);
239
 
        pMessageBase->m_szPassword[NZBREQUESTPASSWORDSIZE - 1] = '\0';
240
 
}
241
 
 
242
 
bool Frontend::RequestMessages()
243
 
{
244
 
        NetAddress netAddress(g_pOptions->GetServerIP(), g_pOptions->GetServerPort());
245
 
        Connection connection(&netAddress);
246
 
 
247
 
        bool OK = connection.Connect();
248
 
        if (!OK)
249
 
        {
250
 
                return false;
251
 
        }
252
 
 
253
 
        SNZBLogRequest LogRequest;
254
 
        InitMessageBase(&LogRequest.m_MessageBase, eRemoteRequestLog, sizeof(LogRequest));
255
 
        LogRequest.m_iLines = htonl(m_iNeededLogEntries);
256
 
        if (m_iNeededLogEntries == 0)
257
 
        {
258
 
                LogRequest.m_iIDFrom = htonl(m_iNeededLogFirstID > 0 ? m_iNeededLogFirstID : 1);
259
 
        }
260
 
        else
261
 
        {
262
 
                LogRequest.m_iIDFrom = 0;
263
 
        }
264
 
 
265
 
        if (connection.Send((char*)(&LogRequest), sizeof(LogRequest)) < 0)
266
 
        {
267
 
                return false;
268
 
        }
269
 
 
270
 
        // Now listen for the returned log
271
 
        SNZBLogResponse LogResponse;
272
 
        int iResponseLen = connection.Recv((char*) &LogResponse, sizeof(LogResponse));
273
 
        if (iResponseLen != sizeof(LogResponse) || 
274
 
                (int)ntohl(LogResponse.m_MessageBase.m_iSignature) != (int)NZBMESSAGE_SIGNATURE ||
275
 
                ntohl(LogResponse.m_MessageBase.m_iStructSize) != sizeof(LogResponse))
276
 
        {
277
 
                return false;
278
 
        }
279
 
 
280
 
        char* pBuf = NULL;
281
 
        if (ntohl(LogResponse.m_iTrailingDataLength) > 0)
282
 
        {
283
 
                pBuf = (char*)malloc(ntohl(LogResponse.m_iTrailingDataLength));
284
 
                if (!connection.RecvAll(pBuf, ntohl(LogResponse.m_iTrailingDataLength)))
285
 
                {
286
 
                        free(pBuf);
287
 
                        return false;
288
 
                }
289
 
        }
290
 
 
291
 
        connection.Disconnect();
292
 
 
293
 
        if (ntohl(LogResponse.m_iTrailingDataLength) > 0)
294
 
        {
295
 
                char* pBufPtr = (char*)pBuf;
296
 
                for (unsigned int i = 0; i < ntohl(LogResponse.m_iNrTrailingEntries); i++)
297
 
                {
298
 
                        SNZBLogResponseEntry* pLogAnswer = (SNZBLogResponseEntry*) pBufPtr;
299
 
 
300
 
                        char* szText = pBufPtr + sizeof(SNZBLogResponseEntry);
301
 
 
302
 
                        Message* pMessage = new Message(ntohl(pLogAnswer->m_iID), (Message::EKind)ntohl(pLogAnswer->m_iKind), ntohl(pLogAnswer->m_tTime), szText);
303
 
                        m_RemoteMessages.push_back(pMessage);
304
 
 
305
 
                        pBufPtr += sizeof(SNZBLogResponseEntry) + ntohl(pLogAnswer->m_iTextLen);
306
 
                }
307
 
 
308
 
                free(pBuf);
309
 
        }
310
 
 
311
 
        return true;
312
 
}
313
 
 
314
 
bool Frontend::RequestFileList()
315
 
{
316
 
        NetAddress netAddress(g_pOptions->GetServerIP(), g_pOptions->GetServerPort());
317
 
        Connection connection(&netAddress);
318
 
 
319
 
        bool OK = connection.Connect();
320
 
        if (!OK)
321
 
        {
322
 
                return false;
323
 
        }
324
 
 
325
 
        SNZBListRequest ListRequest;
326
 
        InitMessageBase(&ListRequest.m_MessageBase, eRemoteRequestList, sizeof(ListRequest));
327
 
        ListRequest.m_bFileList = htonl(m_bFileList);
328
 
        ListRequest.m_bServerState = htonl(m_bSummary);
329
 
 
330
 
        if (connection.Send((char*)(&ListRequest), sizeof(ListRequest)) < 0)
331
 
        {
332
 
                return false;
333
 
        }
334
 
 
335
 
        // Now listen for the returned list
336
 
        SNZBListResponse ListResponse;
337
 
        int iResponseLen = connection.Recv((char*) &ListResponse, sizeof(ListResponse));
338
 
        if (iResponseLen != sizeof(ListResponse) || 
339
 
                (int)ntohl(ListResponse.m_MessageBase.m_iSignature) != (int)NZBMESSAGE_SIGNATURE ||
340
 
                ntohl(ListResponse.m_MessageBase.m_iStructSize) != sizeof(ListResponse))
341
 
        {
342
 
                return false;
343
 
        }
344
 
 
345
 
        char* pBuf = NULL;
346
 
        if (ntohl(ListResponse.m_iTrailingDataLength) > 0)
347
 
        {
348
 
                pBuf = (char*)malloc(ntohl(ListResponse.m_iTrailingDataLength));
349
 
                if (!connection.RecvAll(pBuf, ntohl(ListResponse.m_iTrailingDataLength)))
350
 
                {
351
 
                        free(pBuf);
352
 
                        return false;
353
 
                }
354
 
        }
355
 
 
356
 
        connection.Disconnect();
357
 
 
358
 
        if (m_bSummary)
359
 
        {
360
 
                m_bPauseDownload = ntohl(ListResponse.m_bDownloadPaused);
361
 
                m_bPauseDownload2 = ntohl(ListResponse.m_bDownload2Paused);
362
 
                m_lRemainingSize = Util::JoinInt64(ntohl(ListResponse.m_iRemainingSizeHi), ntohl(ListResponse.m_iRemainingSizeLo));
363
 
                m_fCurrentDownloadSpeed = ntohl(ListResponse.m_iDownloadRate) / 1024.0f;
364
 
                m_fDownloadLimit = ntohl(ListResponse.m_iDownloadLimit) / 1024.0f;
365
 
                m_iThreadCount = ntohl(ListResponse.m_iThreadCount);
366
 
                m_iPostJobCount = ntohl(ListResponse.m_iPostJobCount);
367
 
                m_iUpTimeSec = ntohl(ListResponse.m_iUpTimeSec);
368
 
                m_iDnTimeSec = ntohl(ListResponse.m_iDownloadTimeSec);
369
 
                m_bStandBy = ntohl(ListResponse.m_bDownloadStandBy);
370
 
                m_iAllBytes = Util::JoinInt64(ntohl(ListResponse.m_iDownloadedBytesHi), ntohl(ListResponse.m_iDownloadedBytesLo));
371
 
        }
372
 
 
373
 
        if (m_bFileList && ntohl(ListResponse.m_iTrailingDataLength) > 0)
374
 
        {
375
 
                RemoteClient client;
376
 
                client.SetVerbose(false);
377
 
                client.BuildFileList(&ListResponse, pBuf, &m_RemoteQueue);
378
 
        }
379
 
 
380
 
        if (pBuf)
381
 
        {
382
 
                free(pBuf);
383
 
        }
384
 
 
385
 
        return true;
386
 
}
387
 
 
388
 
bool Frontend::RequestPauseUnpause(bool bPause, bool bSecondRegister)
389
 
{
390
 
        RemoteClient client;
391
 
        client.SetVerbose(false);
392
 
        return client.RequestServerPauseUnpause(bPause, bSecondRegister ? eRemotePauseUnpauseActionDownload2 : eRemotePauseUnpauseActionDownload);
393
 
}
394
 
 
395
 
bool Frontend::RequestSetDownloadRate(float fRate)
396
 
{
397
 
        RemoteClient client;
398
 
        client.SetVerbose(false);
399
 
        return client.RequestServerSetDownloadRate(fRate);
400
 
}
401
 
 
402
 
bool Frontend::RequestDumpDebug()
403
 
{
404
 
        RemoteClient client;
405
 
        client.SetVerbose(false);
406
 
        return client.RequestServerDumpDebug();
407
 
}
408
 
 
409
 
bool Frontend::RequestEditQueue(eRemoteEditAction iAction, int iOffset, int iID)
410
 
{
411
 
        RemoteClient client;
412
 
        client.SetVerbose(false);
413
 
        return client.RequestServerEditQueue(iAction, iOffset, NULL, &iID, 1, false);
414
 
}