func ExecuteCmd(cmd string, param json.JSONObject, callback function_callback) { call, ok := func_map[cmd] if ok { p := json.NewJSONEmpty() if param.HasMember("aux") || param.HasMember("args") { p = param } else { p.Put("args", param) } id := util.CreateID() is, aux := p.GetJSON("aux") if is && aux.HasMember("id") { } else { aux = json.NewJSONEmpty() p.Put("aux", aux) aux.Put("id", id) } if callback != nil { call_map[id] = callback aux.AddToArray("from", id) aux.Put("action", "callback") } else { aux.Put("action", "direct") } aux.Put("to", cmd) call(p) } }
func CallbackWithSuccessResult(param json.JSONObject, data json.JSONObject) { if param != nil { if !param.HasMember("result") { result := json.NewJSONEmpty() param.Put("result", result) } ok, result := param.GetJSON("result") if ok { result.Put("success", true) if data != nil { result.Put("data", data) } else { result.Put("data", json.NewJSONEmpty()) } } Callback(param) } }
func MicroServer2MinorIndentify(id int, serverType byte) []byte { j := json.NewJSONEmpty() j.Put(MSG_TYPE, MSG_TYPE_VALUE_IDENTIFICATION) j.Put(IDENTITY, IDENTITY_VALUE_MICRO_SERVER) j.Put(CLIENT_ID, id) j.Put(MICRO_SERVER_TYPE, serverType) str := j.ToString() return []byte(str) }
func Minor2MajorIdentify(minor_id int, server_type byte) []byte { j := json.NewJSONEmpty() j.Put(MSG_TYPE, MSG_TYPE_VALUE_IDENTIFICATION) j.Put(IDENTITY, IDENTITY_VALUE_MINOR_CONFIG) j.Put(CLIENT_ID, minor_id) j.Put(MICRO_SERVER_TYPE, server_type) str := j.ToString() return []byte(str) }
func Minor2MajorUpdateMicroServerNum(minor_id int, server_num int, address []string) []byte { j := json.NewJSONEmpty() j.Put(MSG_TYPE, MSG_TYPE_VALUE_UPDATE_IDENTIFICATION) j.Put(IDENTITY, IDENTITY_VALUE_MINOR_CONFIG) j.Put(CLIENT_ID, minor_id) j.Put(MICRO_SERVER_NUM, server_num) for _, val := range address { j.AddToArray(MICRO_SERVER_ADDRESS, val) } str := j.ToString() return []byte(str) }
func CallbackWithFailResult(param json.JSONObject, reason string) { if param != nil { if !param.HasMember("result") { result := json.NewJSONEmpty() param.Put("result", result) } ok, result := param.GetJSON("result") if ok { result.Put("success", false) result.Put("reason", reason) } Callback(param) } }
func Test_ExecuteCmd(t *testing.T) { RegisterCmd("hello", func(param json.JSONObject) { fmt.Println("run hello", param.ToString()) param.Put("ret", 1) Callback(param) }) RegisterCmd("world", func(param json.JSONObject) { fmt.Println("run hello", param.ToString()) param.Put("ret", 1) Callback(param) }) js := json.NewJSONEmpty() js.Put("a", "a1") ExecuteCmdDirect("hello", js) ExecuteCmd("hello", js, func(value json.JSONObject) { fmt.Println("callback ", value.ToString()) }) }