Skip to content

Commit

Permalink
Do not panic if function data is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
DerAndereAndi committed Jan 20, 2024
1 parent 94b5254 commit dcaf393
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
38 changes: 34 additions & 4 deletions spine/feature_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,21 @@ func (r *FeatureLocalImpl) DataCopy(function model.FunctionType) any {
r.mux.Lock()
defer r.mux.Unlock()

return r.functionData(function).DataCopyAny()
fctData := r.functionData(function)
if fctData == nil {
return nil
}

return fctData.DataCopyAny()
}

func (r *FeatureLocalImpl) SetData(function model.FunctionType, data any) {
r.mux.Lock()

fd := r.functionData(function)
if fd == nil {
return
}
fd.UpdateDataAny(data, nil, nil)

r.mux.Unlock()
Expand All @@ -91,7 +99,12 @@ func (r *FeatureLocalImpl) UpdateData(function model.FunctionType, data any, fil
r.mux.Lock()
defer r.mux.Unlock()

r.functionData(function).UpdateDataAny(data, filterPartial, filterDelete)
fctData := r.functionData(function)
if fctData == nil {
return
}

fctData.UpdateDataAny(data, filterPartial, filterDelete)
}

func (r *FeatureLocalImpl) AddResultHandler(handler api.FeatureResult) {
Expand Down Expand Up @@ -150,6 +163,10 @@ func (r *FeatureLocalImpl) RequestData(
elements any,
destination api.FeatureRemote) (*model.MsgCounterType, *model.ErrorType) {
fd := r.functionData(function)
if fd == nil {
return nil, model.NewErrorTypeFromString("function data not found")
}

cmd := fd.ReadCmdType(selector, elements)

return r.RequestDataBySenderAddress(cmd, destination.Sender(), destination.Device().Ski(), destination.Address(), destination.MaxResponseDelayDuration())
Expand Down Expand Up @@ -305,6 +322,10 @@ func (r *FeatureLocalImpl) NotifyData(
deleteElements any,
destination api.FeatureRemote) (*model.MsgCounterType, *model.ErrorType) {
fd := r.functionData(function)
if fd == nil {
return nil, model.NewErrorTypeFromString("function data not found")
}

cmd := fd.NotifyCmdType(deleteSelector, partialSelector, partialWithoutSelector, deleteElements)

msgCounter, err := destination.Sender().Request(model.CmdClassifierTypeRead, r.Address(), destination.Address(), false, []model.CmdType{cmd})
Expand All @@ -321,6 +342,9 @@ func (r *FeatureLocalImpl) WriteData(
deleteElements any,
destination api.FeatureRemote) (*model.MsgCounterType, *model.ErrorType) {
fd := r.functionData(function)
if fd == nil {
return nil, model.NewErrorTypeFromString("function data not found")
}
cmd := fd.WriteCmdType(deleteSelector, partialSelector, deleteElements)

msgCounter, err := destination.Sender().Write(r.Address(), destination.Address(), cmd)
Expand Down Expand Up @@ -420,7 +444,12 @@ func (r *FeatureLocalImpl) processRead(function model.FunctionType, requestHeade
return model.NewErrorTypeFromNumber(model.ErrorNumberTypeCommandRejected)
}

cmd := r.functionData(function).ReplyCmdType(false)
fd := r.functionData(function)
if fd == nil {
return model.NewErrorTypeFromString("function data not found")
}

cmd := fd.ReplyCmdType(false)
if err := featureRemote.Sender().Reply(requestHeader, r.Address(), cmd); err != nil {
return model.NewErrorTypeFromString(err.Error())
}
Expand Down Expand Up @@ -506,7 +535,8 @@ func (r *FeatureLocalImpl) processWrite(function model.FunctionType, data any, f
func (r *FeatureLocalImpl) functionData(function model.FunctionType) api.FunctionDataCmd {
fd, found := r.functionDataMap[function]
if !found {
panic(fmt.Errorf("Data was not found for function '%s'", function))
logging.Log().Errorf("Data was not found for function '%s'", function)
return nil
}
return fd
}
15 changes: 13 additions & 2 deletions spine/feature_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,21 @@ func (r *FeatureRemoteImpl) DataCopy(function model.FunctionType) any {
r.mux.Lock()
defer r.mux.Unlock()

fd := r.functionData(function)
if fd == nil {
return nil
}

return r.functionData(function).DataCopyAny()
}

func (r *FeatureRemoteImpl) SetData(function model.FunctionType, data any) {
r.mux.Lock()

fd := r.functionData(function)
fd.UpdateDataAny(data, nil, nil)
if fd != nil {
fd.UpdateDataAny(data, nil, nil)
}

r.mux.Unlock()
}
Expand All @@ -64,7 +71,11 @@ func (r *FeatureRemoteImpl) UpdateData(function model.FunctionType, data any, fi
r.mux.Lock()
defer r.mux.Unlock()

r.functionData(function).UpdateDataAny(data, filterPartial, filterDelete)
fd := r.functionData(function)
if fd == nil {
return
}
fd.UpdateDataAny(data, filterPartial, filterDelete)
// TODO: fire event
}

Expand Down
9 changes: 8 additions & 1 deletion spine/nodemanagement_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,20 @@ func (r *NodeManagementImpl) NotifyUseCaseData(remoteDevice api.DeviceRemote) (*
featureRemote := remoteDevice.FeatureByEntityTypeAndRole(rEntity, model.FeatureTypeTypeNodeManagement, model.RoleTypeSpecial)

fd := r.functionData(model.FunctionTypeNodeManagementUseCaseData)
if fd == nil {
return nil, errors.New("function data not found")
}
cmd := fd.NotifyCmdType(nil, nil, false, nil)

return featureRemote.Sender().Notify(r.Address(), rfAdress, cmd)
}

func (r *NodeManagementImpl) processReadUseCaseData(featureRemote api.FeatureRemote, requestHeader *model.HeaderType) error {
cmd := r.functionData(model.FunctionTypeNodeManagementUseCaseData).ReplyCmdType(false)
fd := r.functionData(model.FunctionTypeNodeManagementUseCaseData)
if fd == nil {
return errors.New("function data not found")
}
cmd := fd.ReplyCmdType(false)

return featureRemote.Sender().Reply(requestHeader, r.Address(), cmd)
}
Expand Down

0 comments on commit dcaf393

Please sign in to comment.