Blue Brain BioExplorer
AddModelFromBlobTask.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2024, EPFL/Blue Brain Project
3  * All rights reserved. Do not distribute without permission.
4  * Responsible Author: Daniel.Nachbaur@epfl.ch
5  *
6  * This file is part of Blue Brain BioExplorer <https://github.com/BlueBrain/BioExplorer>
7  *
8  * This library is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License version 3.0 as published
10  * by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #include "AddModelFromBlobTask.h"
23 
24 #include "Errors.h"
25 #include "LoadModelFunctor.h"
26 
29 
30 #include <sstream>
31 
32 namespace core
33 {
35  : _param(param)
36 {
37  _checkValidity(engine);
38 
39  _blob.reserve(param.size);
40 
41  LoadModelFunctor functor{engine, param};
42  functor.setCancelToken(_cancelToken);
43  functor.setProgressFunc([&progress = progress, w = CHUNK_PROGRESS_WEIGHT](const auto& msg, auto, auto amount)
44  { progress.update(msg, w + (amount * (1.f - w))); });
45 
46  // load data, return model descriptor or stop if blob receive was invalid
47  _finishTasks.emplace_back(_errorEvent.get_task());
48  _finishTasks.emplace_back(_chunkEvent.get_task().then(std::move(functor)));
49  _task = async::when_any(_finishTasks)
50  .then(
51  [&engine](async::when_any_result<std::vector<async::task<ModelDescriptorPtr>>> results)
52  {
53  engine.triggerRender();
54  return results.tasks[results.index].get();
55  });
56 }
57 
58 void AddModelFromBlobTask::appendBlob(const std::string& blob)
59 {
60  // if more bytes than expected are received, error and stop
61  if (_blob.size() + blob.size() > _param.size)
62  {
63  _errorEvent.set_exception(std::make_exception_ptr(INVALID_BINARY_RECEIVE));
64  return;
65  }
66 
67  _blob.insert(_blob.end(), blob.begin(), blob.end());
68 
69  _receivedBytes += blob.size();
70  std::stringstream msg;
71  msg << "Receiving " << _param.getName() << " ...";
72  progress.update(msg.str(), _progressBytes());
73 
74  // if blob is complete, start the loading
75  if (_blob.size() == _param.size)
76  _chunkEvent.set({_param.type, _param.getName(), std::move(_blob)});
77 }
78 
79 void AddModelFromBlobTask::_checkValidity(Engine& engine)
80 {
81  if (_param.type.empty() || _param.size == 0)
82  throw MISSING_PARAMS;
83 
84  const auto& registry = engine.getScene().getLoaderRegistry();
85  if (!registry.isSupportedType(_param.type))
86  throw UNSUPPORTED_TYPE;
87 }
88 } // namespace core
Progress progress
Definition: Task.h:76
async::cancellation_token _cancelToken
Definition: Task.h:79
AddModelFromBlobTask(const BinaryParam &param, Engine &engine)
void appendBlob(const std::string &blob)
Provides an abstract implementation of a ray-tracing engine.
Definition: Engine.h:59
PLATFORM_API std::function< void()> triggerRender
Callback when a new frame shall be triggered. Currently called by event plugins Deflect and Rockets.
Definition: Engine.h:174
PLATFORM_API Scene & getScene()
Returns the scene.
Definition: Engine.h:147
PLATFORM_API const std::string & getName() const
getName gets the name of the model
Definition: Model.h:226
void update(const std::string &operation, const float amount)
Definition: Progress.h:46
PLATFORM_API LoaderRegistry & getLoaderRegistry()
Get the registry for all supported loaders of this scene.
Definition: Scene.h:214
const TaskRuntimeError MISSING_PARAMS
Definition: Errors.h:34
const TaskRuntimeError UNSUPPORTED_TYPE
Definition: Errors.h:36
const TaskRuntimeError INVALID_BINARY_RECEIVE
Definition: Errors.h:38
@ vector
Definition: CommonTypes.h:68
std::string type
file extension or type (MESH, POINTS, CIRCUIT)
size_t size
size in bytes of file