// TakeAll (read and remove) tuples from the tuplespace, with an optional // timeout. The "tuples" argument must be an array of values. func (t *TupleSpaceClient) TakeAll(match string, timeout time.Duration, tuples interface{}) error { m, err := tuplespace.Match(match) if err != nil { return err } return t.read(m, timeout, tuplespace.ActionTake, tuples) }
// Read a tuple from the tuplespace, with an optional timeout. func (t *TupleSpaceClient) Read(match string, timeout time.Duration, tuple interface{}) error { m, err := tuplespace.Match(match) if err != nil { return err } out := []interface{}{tuple} return t.read(m, timeout, tuplespace.ActionOne, &out) }
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) } }