makeResponseCallback prepares and returns a callback function sent to the server. The caller of the Tell() is blocked until the server calls this callback function. Sets theResponse and notifies the caller by sending to done channel.
(doneChan chan *response, removeCallback <-chan uint64, method string, args []interface{})
| 947 | // The caller of the Tell() is blocked until the server calls this callback function. |
| 948 | // Sets theResponse and notifies the caller by sending to done channel. |
| 949 | func (c *Client) makeResponseCallback(doneChan chan *response, removeCallback <-chan uint64, method string, args []interface{}) dnode.Function { |
| 950 | return dnode.Callback(func(arguments *dnode.Partial) { |
| 951 | // Single argument of response callback. |
| 952 | var resp struct { |
| 953 | Result *dnode.Partial `json:"result"` |
| 954 | Err *Error `json:"error"` |
| 955 | } |
| 956 | |
| 957 | // Notify that the callback is finished. |
| 958 | defer func() { |
| 959 | if resp.Err != nil { |
| 960 | c.LocalKite.Log.Debug("Error received from kite: %q method: %q args: %#v err: %s", c.Kite.Name, method, args, resp.Err.Error()) |
| 961 | doneChan <- &response{resp.Result, resp.Err} |
| 962 | } else { |
| 963 | doneChan <- &response{resp.Result, nil} |
| 964 | } |
| 965 | }() |
| 966 | |
| 967 | // Remove the callback function from the map so we do not |
| 968 | // consume memory for unused callbacks. |
| 969 | if id, ok := <-removeCallback; ok { |
| 970 | c.scrubber.RemoveCallback(id) |
| 971 | } |
| 972 | |
| 973 | // We must only get one argument for response callback. |
| 974 | arg, err := arguments.SliceOfLength(1) |
| 975 | if err != nil { |
| 976 | resp.Err = &Error{Type: "invalidResponse", Message: err.Error()} |
| 977 | return |
| 978 | } |
| 979 | |
| 980 | // Unmarshal callback response argument. |
| 981 | err = arg[0].Unmarshal(&resp) |
| 982 | if err != nil { |
| 983 | resp.Err = &Error{Type: "invalidResponse", Message: err.Error()} |
| 984 | return |
| 985 | } |
| 986 | |
| 987 | // At least result or error must be sent. |
| 988 | keys := make(map[string]interface{}) |
| 989 | err = arg[0].Unmarshal(&keys) |
| 990 | _, ok1 := keys["result"] |
| 991 | _, ok2 := keys["error"] |
| 992 | if !ok1 && !ok2 { |
| 993 | resp.Err = &Error{ |
| 994 | Type: "invalidResponse", |
| 995 | Message: "Server has sent invalid response arguments", |
| 996 | } |
| 997 | return |
| 998 | } |
| 999 | }) |
| 1000 | } |
| 1001 | |
| 1002 | // onError is called when an error happened in a method handler. |
| 1003 | func onError(err error) { |
no test coverage detected