Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions default_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("<node>")
Expand All @@ -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
Expand Down
16 changes: 6 additions & 10 deletions export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

Expand Down