Beispiel #1
0
// Accept forward requests
func StreamForwardRequests(x0 x.Listener) <-chan *request {
	ch := make(chan *request)
	go func() {
		for {
			conn := x0.Accept()
			req, err := conn.Read()
			if err != nil {
				conn.Write(&proto.XError{err.Error()})
				conn.Close()
				continue
			}
			fwdreq, ok := req.(*proto.XDashboardQuery)
			if !ok {
				conn.Write(&proto.XError{"unknown dashboard request"})
				conn.Close()
				continue
			}
			ch <- &request{
				Query: fwdreq,
				ReturnResponse: func(posts []*proto.Post, err error) {
					if err != nil {
						conn.Write(&proto.XError{err.Error()})
					} else {
						conn.Write(&proto.XDashboardQuerySuccess{Posts: posts})
					}
					conn.Close()
				},
			}
		}
	}()
	return ch
}
Beispiel #2
0
func StreamX(x0 x.Listener) (<-chan *createRequest, <-chan *queryRequest) {
	cch, qch := make(chan *createRequest), make(chan *queryRequest)
	go func() {
		for {
			conn := x0.Accept()
			req, err := conn.Read()
			if err != nil {
				conn.Write(&proto.XError{err.Error()})
				conn.Close()
				continue
			}
			switch q := req.(type) {
			case *proto.XCreatePost:
				cch <- &createRequest{
					Forwarded: true,
					Post:      q,
					ReturnResponse: func(err error) {
						if err != nil {
							conn.Write(&proto.XError{err.Error()})
							conn.Close()
							return
						}
						conn.Write(&proto.XSuccess{})
						conn.Close()
					},
				}
			case *proto.XTimelineQuery:
				qch <- &queryRequest{
					Query: q,
					ReturnResponse: func(posts []int64, err error) {
						if err != nil {
							conn.Write(&proto.XError{err.Error()})
							conn.Close()
							return
						}
						conn.Write(&proto.XTimelineQuerySuccess{Posts: posts})
						conn.Close()
					},
				}
			default:
				panic(fmt.Sprintf("unknown request to timeline: %#v", req))
				conn.Write(&proto.XError{"unknown request"})
				conn.Close()
			}
		}
	}()
	return cch, qch
}