func forwardUserRequest(dispatcherHost string, m *piazza.Message) (*piazza.Message, error) { log.Printf("forwarding user request") buf, err := m.ToBytes() var buff = bytes.NewBuffer(buf) if err != nil { return nil, err } resp, err := http.Post(dispatcherHost, "application/json", buff) if err != nil { return nil, err } defer resp.Body.Close() // resp has a message of type CreateJobResponsePayload, with the id filled in buff = new(bytes.Buffer) buff.ReadFrom(resp.Body) m2, err := piazza.NewMessageFromBytes(buff.Bytes()) if err != nil { return nil, err } log.Printf("returning request response") return m2, nil }
// send a message to the dispatcher func (proxy *GatewayProxy) Send(m *piazza.Message) (piazza.PiazzaId, error) { log.Print("(GatewayProxy.Send)") url := fmt.Sprintf("%s/message", proxy.DispatcherHost) buf, err := m.ToBytes() if err != nil { return "", err } buff := bytes.NewBuffer(buf) resp, err := http.Post(url, "application/json", buff) if err != nil { return "", err } defer resp.Body.Close() buf, err = ioutil.ReadAll(resp.Body) if err != nil { return "", err } var obj piazza.Message err = json.Unmarshal(buf, &obj) if err != nil { return "", err } status := obj.CreateJobResponsePayload.JobStatus if status != piazza.JobStatusSubmitted { return "", errors.New(fmt.Sprintf("bad job status: %v", status)) } pid := obj.CreateJobResponsePayload.JobId return pid, nil }