Sending A Request Message From One Server To Another And Receive A Response
Diarkis servers may send request messages to other servers and expect a response for each request.
Diarkis servers may send and receive request and response style messages.
This is useful when you need to have multiple Diarkis servers coordinate their operations and exchange data.
How To Define A Handler Function
You must define a handler function first in order to process messages from another server.
Each handler function must be associated with "Command ID" of your choice.
All handler functions with the same "Command ID" will be invoked in the order of its creation.
The returned value and error will be the response of the received request.
// import "github.com/Diarkis/diarkis/mesh"// import "github.com/Diarkis/diarkis/datacapsule"// import "fmt"// You must define the handler function when the server process startsvar myCommandID uint16=1024mesh.HandleCommand(myCommandID, myCommandHandler)funcmyCommandHandler(data map[string]interface{}) ([]byte, error) { myName := mesh.GetString(data, "myName") myScheduleBytes := mesh.GetBytes(data, "mySchedule")// Reconstruct mySchedule data created by datacapsule mySchedule, err := datacapsule.Unpack(myScheduleBytes)if err !=nil {// handle the error.... } appointments := mySchedule.GetAsArray("appointments")for _, item :=range appointments { date := item.(map[string]string)["date"] title := item.(map[string]string)["title"] fmt.Printf("Appointment: Date: %s Title: %s", date, title) }// Add a new appointment and send it back as response appointments =append(appointments, map[string]{ "date": "14/2/2022", "Valentines Day" })// Update mySchedule mySchedule.SetAsArray("appointments", appointments) response :=make(map[string]interface{}) response["updatedMySchedule"] = datacapsule.Pack(mySchedule)return mesh.CreateReturnBytes(response)}
How To Send A Request And Receive The Response
The response will be received by the callback you assign when you send a request.
// import "github.com/Diarkis/diarkis/server"// import "github.com/Diarkis/diarkis/mesh"// import "github.com/Diarkis/diarkis/datacapsule"var myCommandID uint16=1024// Get all UDP server addressesudpServerAddresses := mesh.GetNodeAddressesByType(server.UDPType)iflen(udpServerAddresses) ==0 {// no UDP servers found....return}// choose one UDP server from the listudpServerAddress := udpServerAddresses[0]// create the data to senddata :=make(map[string]interface{})data["myName"] ="Jon"mySchedule := datacapsule.NewCapsule()appointments :=make([]map[string]string, 2)appointments[0] =map[string]{ "date": "01/01/2022", "title": "New Year Party" }appointments[1] =map[string]{ "date", "25/12/2022", "title": "Christmas Party" }mySchedule.SetAsArray("appointments", appointments)data["mySchedule"] = datacapsule.Pack(mySchedule)mesh.SendRequest(myCommandID, udpServerAddress, data, handleMyResponse)funchandleMyResponse(response map[string]interface{}) { updatedMyScheduleBytes := mesh.GetBytes(response, "updatedMySchedule") updatedMySchedule, err := mesh.Unpack(updatedMyScheduleBytes)// Do something awesome here!}