0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
OpenFileMap.h
Go to the documentation of this file.
1 /* -*- c++ -*-
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
22 #include <Common/Logger.h>
23 
24 #include <memory>
25 #include <mutex>
26 #include <unordered_map>
27 
28 extern "C" {
29 #include <netinet/in.h>
30 }
31 
32 namespace Hypertable {
33 namespace FsBroker {
34 namespace Lib {
35 
38 
39  class OpenFileData {
40  public:
41  virtual ~OpenFileData() { return; }
42  struct sockaddr_in addr;
43  };
44 
45  typedef std::shared_ptr<OpenFileData> OpenFileDataPtr;
46 
47 
48  class OpenFileMap {
49 
50  public:
51 
52  void create(int fd, struct sockaddr_in &addr, OpenFileDataPtr &fdata) {
53  std::lock_guard<std::mutex> lock(m_mutex);
54  fdata->addr = addr;
55  m_file_map[fd] = fdata;
56  }
57 
58  bool get(int fd, OpenFileDataPtr &fdata) {
59  std::lock_guard<std::mutex> lock(m_mutex);
60  FileMap::iterator iter = m_file_map.find(fd);
61  if (iter != m_file_map.end()) {
62  fdata = (*iter).second;
63  return true;
64  }
65  return false;
66  }
67 
68  bool remove(int fd, OpenFileDataPtr &fdata) {
69  std::lock_guard<std::mutex> lock(m_mutex);
70  FileMap::iterator iter = m_file_map.find(fd);
71  if (iter != m_file_map.end()) {
72  fdata = (*iter).second;
73  m_file_map.erase(iter);
74  return true;
75  }
76  return false;
77  }
78 
79  void remove(int fd) {
80  std::lock_guard<std::mutex> lock(m_mutex);
81  FileMap::iterator iter = m_file_map.find(fd);
82  if (iter != m_file_map.end())
83  m_file_map.erase(iter);
84  }
85 
86  void remove_all(struct sockaddr_in &addr) {
87  std::lock_guard<std::mutex> lock(m_mutex);
88  FileMap::iterator iter = m_file_map.begin();
89 
90  while (iter != m_file_map.end()) {
91  if ((*iter).second->addr.sin_family == addr.sin_family &&
92  (*iter).second->addr.sin_port == addr.sin_port &&
93  (*iter).second->addr.sin_addr.s_addr == addr.sin_addr.s_addr) {
94  FileMap::iterator del_it = iter;
95  HT_INFOF("Removing handle %d from open file map because of lost "
96  "owning client connection", (*iter).first);
97  ++iter;
98  m_file_map.erase(del_it);
99  }
100  else
101  ++iter;
102  }
103  }
104 
105  void remove_all() {
106  std::lock_guard<std::mutex> lock(m_mutex);
107  m_file_map.clear();
108  }
109 
110  private:
111 
112  typedef std::unordered_map<int, OpenFileDataPtr> FileMap;
113 
115  FileMap m_file_map;
116  };
117 
119 
120 }}}
static std::mutex mutex
Definition: Logger.cc:43
std::unordered_map< int, OpenFileDataPtr > FileMap
Definition: OpenFileMap.h:112
std::shared_ptr< OpenFileData > OpenFileDataPtr
Definition: OpenFileMap.h:45
void remove_all(struct sockaddr_in &addr)
Definition: OpenFileMap.h:86
Logging routines and macros.
Hypertable definitions
#define HT_INFOF(msg,...)
Definition: Logger.h:272
void create(int fd, struct sockaddr_in &addr, OpenFileDataPtr &fdata)
Definition: OpenFileMap.h:52