diff --git a/default_handler.go b/default_handler.go index 3da16b1..28dc9e0 100644 --- a/default_handler.go +++ b/default_handler.go @@ -38,12 +38,29 @@ type defaultHandler struct { defaultIntf map[string]*exportedIntf } -func (h *defaultHandler) PathExists(path ObjectPath) bool { - _, ok := h.objects[path] - return ok +func (h *defaultHandler) GetExportedObject(path ObjectPath) (*exportedObj, bool) { + h.RLock() + defer h.RUnlock() + obj, ok := h.objects[path] + return obj, ok +} + +// GetOrAddExportedObject returns an exportedObj for an specific ObjectPath +// A new exportedObj is created if none existed for ObjectPath +func (h *defaultHandler) GetOrAddExportedObject(path ObjectPath) *exportedObj { + h.RLock() + defer h.RUnlock() + obj, ok := h.objects[path] + if !ok { + obj = newExportedObject() + h.objects[path] = obj + } + return obj } func (h *defaultHandler) introspectPath(path ObjectPath) string { + h.RLock() + defer h.RUnlock() subpath := make(map[string]struct{}) var xml bytes.Buffer xml.WriteString("") @@ -65,8 +82,8 @@ func (h *defaultHandler) introspectPath(path ObjectPath) string { } func (h *defaultHandler) LookupObject(path ObjectPath) (ServerObject, bool) { - h.RLock() - defer h.RUnlock() + h.Lock() + defer h.Unlock() object, ok := h.objects[path] if ok { return object, ok diff --git a/export.go b/export.go index fa009d2..43ee11e 100644 --- a/export.go +++ b/export.go @@ -363,8 +363,7 @@ func (conn *Conn) exportMethodTable(methods map[string]interface{}, path ObjectP } func (conn *Conn) unexport(h *defaultHandler, path ObjectPath, iface string) error { - if h.PathExists(path) { - obj := h.objects[path] + if obj, ok := h.GetExportedObject(path); ok { obj.DeleteInterface(iface) if len(obj.interfaces) == 0 { h.DeleteObject(path) @@ -391,21 +390,18 @@ func (conn *Conn) export(methods map[string]reflect.Value, path ObjectPath, ifac return conn.unexport(h, path, iface) } - // If this is the first handler for this path, make a new map to hold all - // handlers for this path. - if !h.PathExists(path) { - h.AddObject(path, newExportedObject()) - } - + // Get all Methods that should be exported + // Get all Methods that should be exported exportedMethods := make(map[string]Method) for name, method := range methods { exportedMethods[name] = exportedMethod{method} } + // Get a handler for the path, either an existing or a new fresh one + obj := h.GetOrAddExportedObject(path) + // Finally, save this handler - obj := h.objects[path] obj.AddInterface(iface, newExportedIntf(exportedMethods, includeSubtree)) - return nil }