1 | <?php |
||
55 | class RemoteUpstreamService { |
||
56 | |||
57 | |||
58 | use TNC22Request; |
||
59 | |||
60 | |||
61 | /** @var EventWrapperRequest */ |
||
62 | private $eventWrapperRequest; |
||
63 | |||
64 | /** @var RemoteStreamService */ |
||
65 | private $remoteStreamService; |
||
66 | |||
67 | /** @var InterfaceService */ |
||
68 | private $interfaceService; |
||
69 | |||
70 | /** @var ConfigService */ |
||
71 | private $configService; |
||
72 | |||
73 | |||
74 | /** |
||
75 | * RemoteUpstreamService constructor. |
||
76 | * |
||
77 | * @param EventWrapperRequest $eventWrapperRequest |
||
78 | * @param RemoteStreamService $remoteStreamService |
||
79 | * @param InterfaceService $interfaceService |
||
80 | * @param ConfigService $configService |
||
81 | */ |
||
82 | public function __construct( |
||
93 | |||
94 | |||
95 | /** |
||
96 | * @param string $token |
||
97 | * |
||
98 | * @return EventWrapper[] |
||
99 | */ |
||
100 | public function getEventsByToken(string $token): array { |
||
103 | |||
104 | |||
105 | /** |
||
106 | * @param EventWrapper $wrapper |
||
107 | * |
||
108 | * @throws FederatedItemException |
||
109 | * @throws RemoteInstanceException |
||
110 | * @throws RemoteNotFoundException |
||
111 | * @throws RemoteResourceNotFoundException |
||
112 | * @throws UnknownRemoteException |
||
113 | */ |
||
114 | public function broadcastEvent(EventWrapper $wrapper): void { |
||
125 | |||
126 | |||
127 | /** |
||
128 | * @param FederatedEvent $event |
||
129 | * |
||
130 | * @throws FederatedItemException |
||
131 | * @throws OwnerNotFoundException |
||
132 | * @throws RemoteInstanceException |
||
133 | * @throws RemoteNotFoundException |
||
134 | * @throws RemoteResourceNotFoundException |
||
135 | * @throws UnknownRemoteException |
||
136 | */ |
||
137 | public function confirmEvent(FederatedEvent $event): void { |
||
147 | |||
148 | |||
149 | // |
||
150 | // |
||
151 | // |
||
152 | |||
153 | // /** |
||
154 | // * @param array $sync |
||
155 | // * |
||
156 | // * @throws GSStatusException |
||
157 | // */ |
||
158 | // public function synchronize(array $sync) { |
||
159 | // $this->configService->getGSStatus(); |
||
160 | // |
||
161 | // $this->synchronizeCircles($sync); |
||
162 | // $this->removeDeprecatedCircles(); |
||
163 | // $this->removeDeprecatedEvents(); |
||
164 | // } |
||
165 | |||
166 | |||
167 | // /** |
||
168 | // * @param array $circles |
||
169 | // */ |
||
170 | // public function synchronizeCircles(array $circles): void { |
||
171 | // $event = new GSEvent(GSEvent::GLOBAL_SYNC, true); |
||
172 | // $event->setSource($this->configService->getLocalInstance()); |
||
173 | // $event->setData(new SimpleDataStore($circles)); |
||
174 | // |
||
175 | // foreach ($this->federatedEventService->getInstances() as $instance) { |
||
176 | // try { |
||
177 | // $this->broadcastEvent($event, $instance); |
||
178 | // } catch (RequestContentException | RequestNetworkException | RequestResultSizeException | RequestServerException | RequestResultNotJsonException $e) { |
||
179 | // } |
||
180 | // } |
||
181 | // } |
||
182 | // |
||
183 | // |
||
184 | // /** |
||
185 | // * |
||
186 | // */ |
||
187 | // private function removeDeprecatedCircles() { |
||
188 | // $knownCircles = $this->circlesRequest->forceGetCircles(); |
||
189 | // foreach ($knownCircles as $knownItem) { |
||
190 | // if ($knownItem->getOwner() |
||
191 | // ->getInstance() === '') { |
||
192 | // continue; |
||
193 | // } |
||
194 | // |
||
195 | // try { |
||
196 | // $this->checkCircle($knownItem); |
||
197 | // } catch (GSStatusException $e) { |
||
198 | // } |
||
199 | // } |
||
200 | // } |
||
201 | // |
||
202 | // |
||
203 | // /** |
||
204 | // * @param DeprecatedCircle $circle |
||
205 | // * |
||
206 | // * @throws GSStatusException |
||
207 | // */ |
||
208 | // private function checkCircle(DeprecatedCircle $circle): void { |
||
209 | // $status = $this->confirmCircleStatus($circle); |
||
210 | // |
||
211 | // if (!$status) { |
||
212 | // $this->circlesRequest->destroyCircle($circle->getUniqueId()); |
||
213 | // $this->membersRequest->removeAllFromCircle($circle->getUniqueId()); |
||
214 | // } |
||
215 | // } |
||
216 | // |
||
217 | // |
||
218 | // /** |
||
219 | // * @param DeprecatedCircle $circle |
||
220 | // * |
||
221 | // * @return bool |
||
222 | // * @throws GSStatusException |
||
223 | // */ |
||
224 | // public function confirmCircleStatus(DeprecatedCircle $circle): bool { |
||
225 | // $event = new GSEvent(GSEvent::CIRCLE_STATUS, true); |
||
226 | // $event->setSource($this->configService->getLocalInstance()); |
||
227 | // $event->setDeprecatedCircle($circle); |
||
228 | // |
||
229 | // $this->signEvent($event); |
||
230 | // |
||
231 | // $path = $this->urlGenerator->linkToRoute('circles.EventWrapper.status'); |
||
232 | // $request = new NC22Request($path, Request::TYPE_POST); |
||
233 | // $this->configService->configureRequest($request); |
||
234 | // $request->setDataSerialize($event); |
||
235 | // |
||
236 | // $requestIssue = false; |
||
237 | // $notFound = false; |
||
238 | // $foundWithNoOwner = false; |
||
239 | // foreach ($this->federatedEventService->getInstances() as $instance) { |
||
240 | // $request->setHost($instance); |
||
241 | // |
||
242 | // try { |
||
243 | // $result = $this->retrieveJson($request); |
||
244 | // if ($this->getInt('status', $result, 0) !== 1) { |
||
245 | // throw new RequestContentException('result status is not good'); |
||
246 | // } |
||
247 | // |
||
248 | // $status = $this->getInt('success.data.status', $result); |
||
249 | // |
||
250 | // // if error, we assume the circle might still exist. |
||
251 | // if ($status === CircleStatus::STATUS_ERROR) { |
||
252 | // return true; |
||
253 | // } |
||
254 | // |
||
255 | // if ($status === CircleStatus::STATUS_OK) { |
||
256 | // return true; |
||
257 | // } |
||
258 | // |
||
259 | // // TODO: check the data.supposedOwner entry. |
||
260 | // if ($status === CircleStatus::STATUS_NOT_OWNER) { |
||
261 | // $foundWithNoOwner = true; |
||
262 | // } |
||
263 | // |
||
264 | // if ($status === CircleStatus::STATUS_NOT_FOUND) { |
||
265 | // $notFound = true; |
||
266 | // } |
||
267 | // |
||
268 | // } catch (RequestContentException |
||
269 | // | RequestNetworkException |
||
270 | // | RequestResultNotJsonException |
||
271 | // | RequestResultSizeException |
||
272 | // | RequestServerException $e) { |
||
273 | // $requestIssue = true; |
||
274 | // // TODO: log instances that have network issue, after too many tries (7d), remove this circle. |
||
275 | // continue; |
||
276 | // } |
||
277 | // } |
||
278 | // |
||
279 | // // if no request issue, we can imagine that the instance that owns the circle is down. |
||
280 | // // We'll wait for more information (cf request exceptions management); |
||
281 | // if ($requestIssue) { |
||
282 | // return true; |
||
283 | // } |
||
284 | // |
||
285 | // // circle were not found in any other instances, we can easily says that the circle does not exists anymore |
||
286 | // if ($notFound && !$foundWithNoOwner) { |
||
287 | // return false; |
||
288 | // } |
||
289 | // |
||
290 | // // circle were found everywhere but with no owner on every instance. we need to assign a new owner. |
||
291 | // // This should be done by checking admin rights. if no admin rights, let's assume that circle should be removed. |
||
292 | // if (!$notFound && $foundWithNoOwner) { |
||
293 | // // TODO: assign a new owner and check that when changing owner, we do check that the destination instance is updated FOR SURE! |
||
294 | // return true; |
||
295 | // } |
||
296 | // |
||
297 | // // some instances returned notFound, some returned circle with no owner. let's assume the circle is deprecated. |
||
298 | // return false; |
||
299 | // } |
||
300 | // |
||
301 | // /** |
||
302 | // * @throws GSStatusException |
||
303 | // */ |
||
304 | // public function syncEvents() { |
||
305 | // |
||
306 | // } |
||
307 | // |
||
308 | // /** |
||
309 | // * |
||
310 | // */ |
||
311 | // private function removeDeprecatedEvents() { |
||
312 | //// $this->deprecatedEvents(); |
||
313 | // |
||
314 | // } |
||
315 | |||
316 | } |
||
317 | |||
318 |