// Fetch an event of watching list, if there's no event available the function would blocked func (man *Watchman) PullEvent() (router.Message, error) { raw, err := man.client.Read() if err != nil { return router.Message{}, err } m, err := router.ParseMessage(raw) if err != nil { return router.Message{}, err } fn := "" for name, _ := range man.paths { if inWatch(m.FileName, name) { fn = name break } } if _, ok := man.paths[fn]; ok && m.Event == 0x0 { return m, errors.New("SYSTEM") } if m.Event == 0x0 || fn == "" || man.paths[fn]&m.Event == 0 { return m, errors.New("You dont' need it.") } m.Event = m.Event & man.paths[fn] return m, nil }
// The request message must like: //{ // "Event":0, // "FileName":"+/path/to/file", or "FileName":"-/path/to/file" // +/- means add or remove watch // .... //} func (em *Distributer) PullRequest() (map[string]string, error) { str, err := em.Read() if err != nil { logging.Debug("Pull request:", err) return nil, err } m, err := router.ParseMessage(str) if err != nil { logging.Debug("Pull request:", err) return nil, err } if m.Event != 0 { return nil, errors.New("Invalid request") } msg := make(map[string]string) if strings.HasPrefix(m.FileName, "+") { msg["ACTION"] = "ADD" msg["PATH"] = strings.TrimLeft(m.FileName, "+") return msg, nil } else if strings.HasPrefix(m.FileName, "-") { msg["ACTION"] = "REMOVE" msg["PATH"] = strings.TrimLeft(m.FileName, "-") return msg, nil } return nil, errors.New("Shouldn't come here") }