Comparison

core/portmanager.lua @ 4897:1a90e5225b22

portmanager: Fix to deactivate services when they are unregistered (metatable:iter() wins)
author Matthew Wild <mwild1@gmail.com>
date Sat, 19 May 2012 20:46:44 +0100
parent 4861:2ee71fa500d6
child 4902:a7c799a7a34b
comparison
equal deleted inserted replaced
4896:27cda15104f2 4897:1a90e5225b22
132 end 132 end
133 log("info", "Activated service '%s'", service_name); 133 log("info", "Activated service '%s'", service_name);
134 return true; 134 return true;
135 end 135 end
136 136
137 function deactivate(service_name) 137 function deactivate(service_name, service_info)
138 local active = active_services:search(service_name)[1]; 138 for name, interface, port, active_service in active_services:iter(service_name, nil, nil, service_info) do
139 if not active then return; end 139 close(interface, port);
140 for interface, ports in pairs(active) do 140 end
141 for port, active_service in pairs(ports) do 141 log("info", "Deactivated service '%s'", service_name or service_info.name);
142 close(interface, port);
143 end
144 end
145 log("info", "Deactivated service '%s'", service_name);
146 end 142 end
147 143
148 function register_service(service_name, service_info) 144 function register_service(service_name, service_info)
149 table.insert(services[service_name], service_info); 145 table.insert(services[service_name], service_info);
150 146
159 fire_event("service-added", { name = service_name, service = service_info }); 155 fire_event("service-added", { name = service_name, service = service_info });
160 return true; 156 return true;
161 end 157 end
162 158
163 function unregister_service(service_name, service_info) 159 function unregister_service(service_name, service_info)
160 log("debug", "Unregistering service: %s", service_name);
164 local service_info_list = services[service_name]; 161 local service_info_list = services[service_name];
165 for i, service in ipairs(service_info_list) do 162 for i, service in ipairs(service_info_list) do
166 if service == service_info then 163 if service == service_info then
167 table.remove(service_info_list, i); 164 table.remove(service_info_list, i);
168 end 165 end
169 end 166 end
170 if active_services[service_name] == service_info then 167 deactivate(nil, service_info);
171 deactivate(service_name); 168 if #service_info_list > 0 then -- Other services registered with this name
172 if #service_info_list > 0 then -- Other services registered with this name 169 activate(service_name); -- Re-activate with the next available one
173 activate(service_name); -- Re-activate with the next available one
174 end
175 end 170 end
176 fire_event("service-removed", { name = service_name, service = service_info }); 171 fire_event("service-removed", { name = service_name, service = service_info });
177 end 172 end
178 173
179 function close(interface, port) 174 function close(interface, port)