示例#1
0
func Send(ts tuplespace.RawTupleSpace, r *http.Request, req SendRequest, resp server.ResponseSerializer) {
	err := ts.SendMany(req.Tuples, req.Timeout)

	if err != nil {
		resp.Error(http.StatusInternalServerError, err)
	} else {
		resp.Serialize(http.StatusOK, &tuplespace.SendResponse{})
	}
}
示例#2
0
func takeOrRead(take bool, ts tuplespace.RawTupleSpace, resp server.ResponseSerializer, req tuplespace.ReadRequest, w http.ResponseWriter) {
	var tuples []tuplespace.Tuple
	var err error

	actions := 0
	if take {
		actions |= tuplespace.ActionTake
	}
	if !req.All {
		actions |= tuplespace.ActionOne
	}

	match, err := tuplespace.Match(req.Match)
	if err != nil {
		resp.Error(http.StatusBadRequest, err)
		return
	}
	handle := ts.ReadOperation(match, req.Timeout, actions)

	select {
	case <-w.(http.CloseNotifier).CloseNotify():
		err = tuplespace.CancelledReader
		handle.Cancel()
	case err = <-handle.Error():
	case tuples = <-handle.Get():
	}

	if err != nil {
		status := http.StatusInternalServerError
		if err == tuplespace.ReaderTimeout {
			status = http.StatusGatewayTimeout
		} else if err == tuplespace.CancelledReader {
			status = http.StatusRequestTimeout
		}
		resp.Error(status, err)
	} else {
		resp.Serialize(http.StatusOK, tuples)
	}
}