func (self *remoteService) run(ws *websocket.Conn) error { for { mt, msg, err := ws.ReadMessage() if err != nil { fmt.Printf("remoteService.run: %#v\n", err) break } if mt != websocket.BinaryMessage { continue } cmd := cmdResult{} if err := json.NewDecoder(bytes.NewReader(msg)).Decode(&cmd); err != nil { return err } if cmd.Id == nil { if err := self.handleEvent(cmd.Back, cmd.Sid, stream.Event([]byte(cmd.Data))); err != nil { return err } } else { if err := self.handleCmdRes(*cmd.Id, cmd.Data); err != nil { return err } } } return nil }
func (self *httpBackendStream) Read(from uint, to uint) (stream.Stream, error) { if from == to { return stream.Empty(), nil } resp, err := self.p.Post(fmt.Sprintf(self.readUrl, from, to), nil) if err != nil { return nil, err } defer resp.Body.Close() res := arrErrorObj{} if err := json.NewDecoder(resp.Body).Decode(&res); err != nil { return nil, err } if res.Err != "" { return nil, errors.New(res.Err) } r := make([]stream.Event, len(res.Events)) for i, v := range res.Events { r[i] = stream.Event([]byte(v)) } return stream.List(r), nil }
func (self *ledisListStream) Next() (stream.Event, error) { if self.num >= self.l { return nil, stream.EOI } res, err := self.db.LIndex(self.key, self.num) if err != nil { self.delLock.RUnlock() return nil, err } self.num += 1 if self.num >= self.l { self.delLock.RUnlock() } return stream.Event(res), nil }
func addCmdHandler(s Service, d []byte) error { data := addCmdData{} if err := json.NewDecoder(bytes.NewReader(d)).Decode(&data); err != nil { return err } b, err := s.GetBackend(data.Back) if err != nil { return err } str, _, err := b.GetStream(data.Name) if err != nil { return err } return str.Add(stream.Event([]byte(data.Evt))) }
/* Create a http.Handler that maps URLs from HTTP backend to a methods of an object implementing Backend interface. */ func NewHandler(b Backend, errorCb func(error)) http.Handler { r := mux.NewRouter() r.HandleFunc("/config", func(w http.ResponseWriter, r *http.Request) { cfg, err := b.Config() if err != nil { sendErr(w, err, errorCb) return } if err := json.NewEncoder(w).Encode(&configRes{Cfg: cfg}); err != nil { sendErr(w, err, errorCb) return } }).Methods("POST") r.HandleFunc("/streams", func(w http.ResponseWriter, r *http.Request) { ss, err := b.Streams() if err != nil { sendErr(w, err, errorCb) return } if err := json.NewEncoder(w).Encode(&sarrErrorObj{Streams: ss}); err != nil { sendErr(w, err, errorCb) return } }).Methods("POST") r.HandleFunc("/drop", func(w http.ResponseWriter, r *http.Request) { sendErr(w, b.Drop(), errorCb) }).Methods("POST") r.HandleFunc("/streams/{name}/push", func(w http.ResponseWriter, r *http.Request) { data, err := ioutil.ReadAll(r.Body) if err != nil { sendErr(w, err, errorCb) return } s, err := b.GetStream(mux.Vars(r)["name"]) if err != nil { sendErr(w, err, errorCb) return } defer func() { if err := s.Close(); err != nil { errorCb(err) } }() sendErr(w, s.Add(stream.Event(data)), errorCb) }).Methods("POST") r.HandleFunc("/streams/{name}/interval/{from}:{to}", func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) s, err := b.GetStream(vars["name"]) if err != nil { sendErr(w, err, errorCb) return } defer func() { if err := s.Close(); err != nil { errorCb(err) } }() from, err := strconv.Atoi(vars["from"]) if err != nil { sendErr(w, err, errorCb) return } to, err := strconv.Atoi(vars["to"]) if err != nil { sendErr(w, err, errorCb) return } f, t, err := s.Interval(from, to) if err != nil { sendErr(w, err, errorCb) return } if err := json.NewEncoder(w).Encode(&interErrorObj{From: f, To: t}); err != nil { sendErr(w, err, errorCb) return } }).Methods("POST") r.HandleFunc("/streams/{name}/read/{from}:{to}", func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) s, err := b.GetStream(vars["name"]) if err != nil { sendErr(w, err, errorCb) return } defer func() { if err := s.Close(); err != nil { errorCb(err) } }() from, err := strconv.Atoi(vars["from"]) if err != nil { sendErr(w, err, errorCb) return } if from < 0 { sendErr(w, errors.New(fmt.Sprintf("Expected \"from\" to be >= 0, got %v", from)), errorCb) return } to, err := strconv.Atoi(vars["to"]) if err != nil { sendErr(w, err, errorCb) return } if to < 0 { sendErr(w, errors.New(fmt.Sprintf("Expected \"to\" to be >= 0, got %v", to)), errorCb) return } str, err := s.Read(uint(from), uint(to)) if err != nil { sendErr(w, err, errorCb) return } l := 0 if to >= 0 { l = to - from } res := make([]json.RawMessage, 0, l) for { evt, err := str.Next() if err == stream.EOI { break } if err != nil { sendErr(w, err, errorCb) return } bs, ok := evt.([]byte) if !ok { sendErr(w, errors.New(fmt.Sprintf("Expected []byte event, got %v", evt)), errorCb) return } res = append(res, json.RawMessage(bs)) } if err := json.NewEncoder(w).Encode(&arrErrorObj{Events: res}); err != nil { sendErr(w, err, errorCb) return } }).Methods("POST") r.HandleFunc("/streams/{name}/del/{from}:{to}", func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) s, err := b.GetStream(vars["name"]) if err != nil { sendErr(w, err, errorCb) return } defer func() { if err := s.Close(); err != nil { errorCb(err) } }() from, err := strconv.Atoi(vars["from"]) if err != nil { sendErr(w, err, errorCb) return } if from < 0 { sendErr(w, errors.New(fmt.Sprintf("Expected \"from\" to be >= 0, got %v", from)), errorCb) return } to, err := strconv.Atoi(vars["to"]) if err != nil { sendErr(w, err, errorCb) return } if to < 0 { sendErr(w, errors.New(fmt.Sprintf("Expected \"to\" to be >= 0, got %v", to)), errorCb) return } ok, err := s.Del(uint(from), uint(to)) if err != nil { sendErr(w, err, errorCb) return } if err := json.NewEncoder(w).Encode(&boolErrorObj{Ok: ok}); err != nil { sendErr(w, err, errorCb) return } }).Methods("POST") r.HandleFunc("/streams/{name}/len", func(w http.ResponseWriter, r *http.Request) { s, err := b.GetStream(mux.Vars(r)["name"]) if err != nil { sendErr(w, err, errorCb) return } defer func() { if err := s.Close(); err != nil { errorCb(err) } }() l, err := s.Len() if err != nil { sendErr(w, err, errorCb) return } if err := json.NewEncoder(w).Encode(&lenErrorObj{Len: l}); err != nil { sendErr(w, err, errorCb) return } }).Methods("POST") return r }