Blue Brain BioExplorer
BinaryRequests.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2018, EPFL/Blue Brain Project
3  * All rights reserved. Do not distribute without permission.
4  * Responsible Author: Daniel Nachbaur <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 #pragma once
23 
27 
28 #include <rockets/jsonrpc/types.h>
29 
30 namespace core
31 {
32 const std::string METHOD_REQUEST_MODEL_UPLOAD = "request-model-upload";
33 
39 {
40 public:
45  auto createTask(const BinaryParam& param, uintptr_t clientID, Engine& engine)
46  {
47  auto task = std::make_shared<AddModelFromBlobTask>(param, engine);
48 
49  std::lock_guard<std::mutex> lock(_mutex);
50  _requests.emplace(std::make_pair(clientID, param.chunksID), task);
51  _nextChunkID = param.chunksID;
52 
53  return task;
54  }
55 
56  void setNextChunkID(const std::string& id) { _nextChunkID = id; }
58  rockets::ws::Response processMessage(const rockets::ws::Request& wsRequest)
59  {
60  std::lock_guard<std::mutex> lock(_mutex);
61  const auto key = std::make_pair(wsRequest.clientID, _nextChunkID);
62  if (_requests.count(key) == 0)
63  {
64  CORE_ERROR("Missing RPC " << METHOD_REQUEST_MODEL_UPLOAD << " or cancelled?");
65  return {};
66  }
67 
68  _requests[key]->appendBlob(wsRequest.message);
69  return {};
70  }
71 
73  void removeRequest(const uintptr_t clientID)
74  {
75  std::lock_guard<std::mutex> lock(_mutex);
76  for (auto i = _requests.begin(); i != _requests.end();)
77  {
78  if (i->first.first != clientID)
79  {
80  ++i;
81  continue;
82  }
83  i->second->cancel();
84  i = _requests.erase(i);
85  }
86  }
87 
89  void removeTask(TaskPtr task)
90  {
91  std::lock_guard<std::mutex> lock(_mutex);
92  for (auto i = _requests.begin(); i != _requests.end();)
93  {
94  if (i->second != task)
95  {
96  ++i;
97  continue;
98  }
99  i = _requests.erase(i);
100  }
101  }
102 
103 private:
104  using ClientRequestID = std::pair<uintptr_t, std::string>;
105  std::map<ClientRequestID, std::shared_ptr<AddModelFromBlobTask>> _requests;
106  std::string _nextChunkID;
107  std::mutex _mutex;
108 };
109 } // namespace core
void removeTask(TaskPtr task)
void setNextChunkID(const std::string &id)
auto createTask(const BinaryParam &param, uintptr_t clientID, Engine &engine)
rockets::ws::Response processMessage(const rockets::ws::Request &wsRequest)
void removeRequest(const uintptr_t clientID)
Provides an abstract implementation of a ray-tracing engine.
Definition: Engine.h:59
const std::string METHOD_REQUEST_MODEL_UPLOAD
std::shared_ptr< AbstractTask > TaskPtr
Definition: Types.h:331
#define CORE_ERROR(__msg)
Definition: Logs.h:31