]>
Dogcows Code - chaz/yoink/blob - src/moof/resource.cc
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
10 **************************************************************************/
12 #include "../config.h"
17 #include <sys/inotify.h>
18 #include <sys/ioctl.h>
21 #include <boost/algorithm/string.hpp>
22 #include <boost/weak_ptr.hpp>
24 #include <stlplus/portability/file_system.hpp>
27 #include "resource.hh"
38 static std::string search_paths_
;
40 typedef boost::weak_ptr
<resource
> resource_weakptr
;
41 static hash
<std::string
,resource_weakptr
,hash_function
> resource_table_
;
44 resource::type_lookup_ptr
resource::type_lookup_
;
48 static hash
<int,std::string
,hash_function
> monitor_lookup_
;
49 static int monitor_fd_
= inotify_init1(IN_NONBLOCK
);
52 int resource::reload_as_needed()
54 int num_resources
= 0;
57 log_info("hotloading?");
60 if (0 < (num_bytes
= read(monitor_fd_
, bytes
, num_bytes
)))
62 char* end
= bytes
+ num_bytes
;
65 log_warning("num_bytes:", num_bytes
);
70 struct inotify_event
* event
= (struct inotify_event
*)byte
;
72 if (event
->mask
& IN_IGNORED
)
74 log_warning("watch", event
->wd
, "removed");
78 hash
<int,std::string
,hash_function
>::iterator it
;
79 it
= monitor_lookup_
.find(event
->wd
);
80 if (it
!= monitor_lookup_
.end())
83 std::string path
= (*it
).second
;
84 monitor_lookup_
.erase(it
);
85 resource::reload(path
);
88 byte
+= sizeof(*event
) + event
->len
;
101 #ifdef USE_HOTLOADING
102 inotify_rm_watch(monitor_fd_
, wd_
);
107 resource_ptr
resource::load(const std::string
& path
)
109 std::string extension
= stlplus::extension_part(path
);
111 if (!find(path
)) return resource_ptr();
113 hash
<std::string
,resource_weakptr
,hash_function
>::iterator it
;
114 it
= resource_table_
.find(path
);
115 if (it
!= resource_table_
.end())
117 resource_weakptr rsrc
= (*it
).second
;
118 resource_ptr locked
= rsrc
.lock();
119 if (locked
) return locked
;
122 std::map
<std::string
,loader_ptr
>::iterator jt
;
123 jt
= type_lookup_
->find(extension
);
124 if (jt
!= type_lookup_
->end())
126 resource_ptr
rsrc((*jt
).second
->load(path
));
127 rsrc
->set_loader(path
, (*jt
).second
);
128 resource_table_
[path
] = rsrc
;
130 #ifdef USE_HOTLOADING
131 int wd
= inotify_add_watch(monitor_fd_
,
132 path
.c_str(), IN_MODIFY
);
133 rsrc
->set_watch_descriptor(wd
);
134 monitor_lookup_
[wd
] = path
;
137 log_info("loaded", rsrc
.get());
141 return resource_ptr();
145 resource_ptr
resource::reload(std::string
& path
)
147 log_info("reloading...", path
);
148 hash
<std::string
,resource_weakptr
,hash_function
>::iterator it
;
149 it
= resource_table_
.find(path
);
150 if (it
!= resource_table_
.end())
152 resource_weakptr rsrc
= (*it
).second
;
153 resource_ptr locked
= rsrc
.lock();
164 void resource::reload()
166 log_info("reloaded", path_
);
168 resource
* resource
= loader_
->load(path_
);
170 resource_
= resource
->resource_
;
171 typeinfo_
= resource
->typeinfo_
;
172 unloader_
= resource
->unloader_
;
174 #ifdef USE_HOTLOADING
175 int wd
= inotify_add_watch(monitor_fd_
,
176 path_
.c_str(), IN_MODIFY
);
177 set_watch_descriptor(wd
);
178 monitor_lookup_
[wd
] = path_
;
185 void resource::add_search_paths(const std::string
& paths
)
187 search_paths_
= paths
;
191 bool resource::find(const std::string
& path
)
193 return !stlplus::lookup(path
, search_paths_
, ":").empty();
196 FILE* resource::open_file(const std::string
& path
, const std::string
& mode
)
198 std::string file
= stlplus::lookup(path
, search_paths_
, ":");
199 return fopen(file
.c_str(), mode
.c_str());
This page took 0.047305 seconds and 5 git commands to generate.