2
* This file is part of nzbget
4
* Copyright (C) 2004 Sven Henkel <sidddy@users.sourceforge.net>
5
* Copyright (C) 2007-2010 Andrei Prygounkov <hugbug@users.sourceforge.net>
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.
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.
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.
22
* $Date: 2010-01-30 15:43:58 +0100 (Sat, 30 Jan 2010) $
41
#include <arpa/inet.h>
42
#include <netinet/in.h>
49
#include "Connection.h"
50
#include "MessageBase.h"
51
#include "QueueCoordinator.h"
52
#include "RemoteClient.h"
55
extern QueueCoordinator* g_pQueueCoordinator;
56
extern Options* g_pOptions;
60
debug("Creating Frontend");
62
m_iNeededLogFirstID = 0;
63
m_iNeededLogEntries = 0;
66
m_fCurrentDownloadSpeed = 0;
68
m_bPauseDownload = false;
69
m_bPauseDownload2 = false;
77
m_iUpdateInterval = g_pOptions->GetUpdateInterval();
80
bool Frontend::PrepareData()
88
if (!RequestMessages() || ((m_bSummary || m_bFileList) && !RequestFileList()))
90
printf("\nUnable to send request to nzbget-server at %s (port %i) \n", g_pOptions->GetServerIP(), g_pOptions->GetServerPort());
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);
114
void Frontend::FreeData()
118
for (Log::Messages::iterator it = m_RemoteMessages.begin(); it != m_RemoteMessages.end(); it++)
122
m_RemoteMessages.clear();
124
for (FileQueue::iterator it = m_RemoteQueue.GetFileQueue()->begin(); it != m_RemoteQueue.GetFileQueue()->end(); it++)
128
m_RemoteQueue.GetFileQueue()->clear();
132
Log::Messages * Frontend::LockMessages()
136
return &m_RemoteMessages;
140
return g_pLog->LockMessages();
144
void Frontend::UnlockMessages()
148
g_pLog->UnlockMessages();
152
DownloadQueue* Frontend::LockQueue()
156
return &m_RemoteQueue;
160
return g_pQueueCoordinator->LockQueue();
164
void Frontend::UnlockQueue()
168
g_pQueueCoordinator->UnlockQueue();
172
bool Frontend::IsRemoteMode()
174
return g_pOptions->GetRemoteClientMode();
177
void Frontend::ServerPauseUnpause(bool bPause, bool bSecondRegister)
181
RequestPauseUnpause(bPause, bSecondRegister);
187
g_pOptions->SetPauseDownload2(bPause);
191
g_pOptions->SetPauseDownload(bPause);
196
void Frontend::ServerSetDownloadRate(float fRate)
200
RequestSetDownloadRate(fRate);
204
g_pOptions->SetDownloadRate(fRate);
208
void Frontend::ServerDumpDebug()
216
g_pQueueCoordinator->LogDebugInfo();
220
bool Frontend::ServerEditQueue(QueueEditor::EEditAction eAction, int iOffset, int iID)
224
return RequestEditQueue((eRemoteEditAction)eAction, iOffset, iID);
228
return g_pQueueCoordinator->GetQueueEditor()->EditEntry(iID, true, eAction, iOffset, NULL);
233
void Frontend::InitMessageBase(SNZBRequestBase* pMessageBase, int iRequest, int iSize)
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';
242
bool Frontend::RequestMessages()
244
NetAddress netAddress(g_pOptions->GetServerIP(), g_pOptions->GetServerPort());
245
Connection connection(&netAddress);
247
bool OK = connection.Connect();
253
SNZBLogRequest LogRequest;
254
InitMessageBase(&LogRequest.m_MessageBase, eRemoteRequestLog, sizeof(LogRequest));
255
LogRequest.m_iLines = htonl(m_iNeededLogEntries);
256
if (m_iNeededLogEntries == 0)
258
LogRequest.m_iIDFrom = htonl(m_iNeededLogFirstID > 0 ? m_iNeededLogFirstID : 1);
262
LogRequest.m_iIDFrom = 0;
265
if (connection.Send((char*)(&LogRequest), sizeof(LogRequest)) < 0)
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))
281
if (ntohl(LogResponse.m_iTrailingDataLength) > 0)
283
pBuf = (char*)malloc(ntohl(LogResponse.m_iTrailingDataLength));
284
if (!connection.RecvAll(pBuf, ntohl(LogResponse.m_iTrailingDataLength)))
291
connection.Disconnect();
293
if (ntohl(LogResponse.m_iTrailingDataLength) > 0)
295
char* pBufPtr = (char*)pBuf;
296
for (unsigned int i = 0; i < ntohl(LogResponse.m_iNrTrailingEntries); i++)
298
SNZBLogResponseEntry* pLogAnswer = (SNZBLogResponseEntry*) pBufPtr;
300
char* szText = pBufPtr + sizeof(SNZBLogResponseEntry);
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);
305
pBufPtr += sizeof(SNZBLogResponseEntry) + ntohl(pLogAnswer->m_iTextLen);
314
bool Frontend::RequestFileList()
316
NetAddress netAddress(g_pOptions->GetServerIP(), g_pOptions->GetServerPort());
317
Connection connection(&netAddress);
319
bool OK = connection.Connect();
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);
330
if (connection.Send((char*)(&ListRequest), sizeof(ListRequest)) < 0)
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))
346
if (ntohl(ListResponse.m_iTrailingDataLength) > 0)
348
pBuf = (char*)malloc(ntohl(ListResponse.m_iTrailingDataLength));
349
if (!connection.RecvAll(pBuf, ntohl(ListResponse.m_iTrailingDataLength)))
356
connection.Disconnect();
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));
373
if (m_bFileList && ntohl(ListResponse.m_iTrailingDataLength) > 0)
376
client.SetVerbose(false);
377
client.BuildFileList(&ListResponse, pBuf, &m_RemoteQueue);
388
bool Frontend::RequestPauseUnpause(bool bPause, bool bSecondRegister)
391
client.SetVerbose(false);
392
return client.RequestServerPauseUnpause(bPause, bSecondRegister ? eRemotePauseUnpauseActionDownload2 : eRemotePauseUnpauseActionDownload);
395
bool Frontend::RequestSetDownloadRate(float fRate)
398
client.SetVerbose(false);
399
return client.RequestServerSetDownloadRate(fRate);
402
bool Frontend::RequestDumpDebug()
405
client.SetVerbose(false);
406
return client.RequestServerDumpDebug();
409
bool Frontend::RequestEditQueue(eRemoteEditAction iAction, int iOffset, int iID)
412
client.SetVerbose(false);
413
return client.RequestServerEditQueue(iAction, iOffset, NULL, &iID, 1, false);