1/*
2 * Copyright 2018-2019, Andrew Lindesay <apl@lindesay.co.nz>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
5
6
7#include "ProcessCoordinatorFactory.h"
8
9#include <Autolock.h>
10#include <AutoLocker.h>
11
12#include <package/Context.h>
13#include <package/PackageRoster.h>
14
15#include "AbstractServerProcess.h"
16#include "LocalPkgDataLoadProcess.h"
17#include "LocalRepositoryUpdateProcess.h"
18#include "Model.h"
19#include "PackageInfoListener.h"
20#include "ProcessCoordinator.h"
21#include "ProcessNode.h"
22#include "ServerHelper.h"
23#include "ServerIconExportUpdateProcess.h"
24#include "ServerPkgDataUpdateProcess.h"
25#include "ServerReferenceDataUpdateProcess.h"
26#include "ServerRepositoryDataUpdateProcess.h"
27#include "ServerSettings.h"
28
29
30using namespace BPackageKit;
31
32
33/* static */ ProcessCoordinator*
34ProcessCoordinatorFactory::CreateBulkLoadCoordinator(
35	PackageInfoListener *packageInfoListener,
36	ProcessCoordinatorListener* processCoordinatorListener,
37	Model* model, bool forceLocalUpdate)
38{
39	uint32 serverProcessOptions = _CalculateServerProcessOptions();
40	BAutolock locker(model->Lock());
41	ProcessCoordinator* processCoordinator = new ProcessCoordinator(
42		processCoordinatorListener);
43
44	ProcessNode *localRepositoryUpdate =
45		new ProcessNode(new LocalRepositoryUpdateProcess(model,
46			forceLocalUpdate));
47	processCoordinator->AddNode(localRepositoryUpdate);
48
49	ProcessNode *localPkgDataLoad =
50		new ProcessNode(new LocalPkgDataLoadProcess(
51			packageInfoListener, model, forceLocalUpdate));
52	localPkgDataLoad->AddPredecessor(localRepositoryUpdate);
53	processCoordinator->AddNode(localPkgDataLoad);
54
55	ProcessNode *serverIconExportUpdate =
56		new ProcessNode(new ServerIconExportUpdateProcess(model,
57			serverProcessOptions));
58	serverIconExportUpdate->AddPredecessor(localPkgDataLoad);
59	processCoordinator->AddNode(serverIconExportUpdate);
60
61	ProcessNode *serverRepositoryDataUpdate =
62		new ProcessNode(new ServerRepositoryDataUpdateProcess(model,
63			serverProcessOptions));
64	serverRepositoryDataUpdate->AddPredecessor(localPkgDataLoad);
65	processCoordinator->AddNode(serverRepositoryDataUpdate);
66
67	ProcessNode *serverReferenceDataUpdate =
68		new ProcessNode(new ServerReferenceDataUpdateProcess(model,
69			serverProcessOptions));
70	processCoordinator->AddNode(serverReferenceDataUpdate);
71
72	// create a process for each of the repositories that are configured on the
73	// local system.  Later, only those that have a web-app repository server
74	// code will be actually processed, but this means that the creation of the
75	// 'processes' does not need to be dynamic as the process coordinator runs.
76
77	BPackageRoster roster;
78	BStringList repoNames;
79	status_t repoNamesResult = roster.GetRepositoryNames(repoNames);
80
81	if (repoNamesResult == B_OK) {
82		AutoLocker<BLocker> locker(model->Lock());
83
84		for (int32 i = 0; i < repoNames.CountStrings(); i++) {
85			ProcessNode* processNode = new ProcessNode(
86				new ServerPkgDataUpdateProcess(
87					model->Language().PreferredLanguage().Code(),
88					repoNames.StringAt(i), model, serverProcessOptions));
89			processNode->AddPredecessor(serverRepositoryDataUpdate);
90			processNode->AddPredecessor(serverReferenceDataUpdate);
91			processCoordinator->AddNode(processNode);
92		}
93	} else {
94		printf("a problem has arisen getting the repository names.\n");
95	}
96
97	return processCoordinator;
98}
99
100
101/* static */ uint32
102ProcessCoordinatorFactory::_CalculateServerProcessOptions()
103{
104	uint32 processOptions = 0;
105
106	if (ServerSettings::IsClientTooOld()) {
107		printf("bulk load proceeding without network communications "
108			"because the client is too old\n");
109		processOptions |= SERVER_PROCESS_NO_NETWORKING;
110	}
111
112	if (!ServerHelper::IsNetworkAvailable())
113		processOptions |= SERVER_PROCESS_NO_NETWORKING;
114
115	if (ServerSettings::PreferCache())
116		processOptions |= SERVER_PROCESS_PREFER_CACHE;
117
118	if (ServerSettings::DropCache())
119		processOptions |= SERVER_PROCESS_DROP_CACHE;
120
121	return processOptions;
122}
123