Instrument Neutral Distributed Interface INDI  2.0.2
sharedblob_parse.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright(c) 2022 Ludovic Pollet. All rights reserved.
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 *******************************************************************************/
18 
19 #include "sharedblob_parse.h"
20 
21 #define INDI_SHARED_BLOB_SUPPORT
22 #include "sharedblob.h"
23 
24 #include <map>
25 #include <sstream>
26 #include <mutex>
27 #include <cstdint>
28 
29 #include <unistd.h>
30 
31 namespace INDI
32 {
33 
34 static std::mutex attachedBlobMutex;
35 static std::map<std::string, int> receivedFds;
36 static uint64_t idGenerator = rand();
37 
38 
39 std::string allocateBlobUid(int fd)
40 {
41  std::lock_guard<std::mutex> lock(attachedBlobMutex);
42 
43  std::stringstream ss;
44  ss << idGenerator++;
45 
46  std::string id = ss.str();
47 
48  receivedFds[id] = fd;
49  return id;
50 }
51 
52 void * attachBlobByUid(const std::string &identifier, size_t size)
53 {
54  int fd;
55  {
56  std::lock_guard<std::mutex> lock(attachedBlobMutex);
57  auto where = receivedFds.find(identifier);
58  if (where == receivedFds.end())
59  {
60  return nullptr;
61  }
62  fd = where->second;
63  receivedFds.erase(where);
64  }
65 
66  return IDSharedBlobAttach(fd, size);
67 }
68 
69 void releaseBlobUids(const std::vector<std::string> &blobs)
70 {
71  std::vector<int> toDestroy;
72  {
73  std::lock_guard<std::mutex> lock(attachedBlobMutex);
74  for(auto id : blobs)
75  {
76  auto idPos = receivedFds.find(id);
77  if (idPos != receivedFds.end())
78  {
79  toDestroy.push_back(idPos->second);
80  receivedFds.erase(idPos);
81  }
82  }
83  }
84 
85  for(auto fd : toDestroy)
86  {
87  ::close(fd);
88  }
89 }
90 
91 }
int fd
Definition: intelliscope.c:43
Namespace to encapsulate INDI client, drivers, and mediator classes.
void * attachBlobByUid(const std::string &identifier, size_t size)
void releaseBlobUids(const std::vector< std::string > &blobs)
std::string allocateBlobUid(int fd)
void * IDSharedBlobAttach(int fd, size_t size)
Definition: sharedblob.c:106