Diarkis は並列分散しているため、必ずしも同じサーバにルームがあるとは限りません。ここでは、別のサーバにあるルームのプロパティの操作について説明します。
リモート Room のプロパティを更新する方法
Diarkis の内部通信機構であるメッシュを利用することで、リモート Room のプロパティを読み込んだり、更新したりすることができます。
対象となる Room があるサーバノードへのアップデートコマンドの送信
addrlist, err := room.GetRoomNodeAddressList(roomID)
if err != nil {
// Handle error
return
}
req := make(map[string]interface{})
req["roomID"] = roomID
addr := addrlist[0] // the rest in the array are the backups
mesh.SendRequest(cmdID, addr, req, func(err error, res map[string]interface{}) {
if err != nil {
// Handle error
return
}
// Remote room property updated successfully
})
リモート・サーバ・ノードでのロジックの更新
// This sets up handleRemoteRoomPropUpdate function to be invoked when mesh.SendRequest sends
// the command message as shown above
mesh.Command(cmdID, handleRemoteRoomPropUpdate)
func handleRemoteRoomPropUpdate(req map[string]interface{}) (error, map[string]interface{}) {
roomID := mesh.GetString(req, "roomID")
var err error
_ := room.UpdateProperties(roomID, func(props map[string]interface{}) bool {
if _, ok := props["booked"]; !ok {
props["booked"] = true
// Booked the room successfully
return true
}
// The room has already been booked...
err = errors.New("Room has already been booked")
return false
})
res := make(map[string]interface{})
return res, err
}