// Composes a request to be sent to the exchange func (ex *ExchangeClient) call(method string, params interface{}) (json.RawMessage, error) { req := Request{ Version: "2.0", Method: method, Params: []interface{}{params}, Id: int(atomic.AddInt64(&requestSeq, 1)), } res := RawResponse{} if err := ex.send(&req, &res); err != nil { return nil, err } var reserr interface{} json.Unmarshal(res.Error, &reserr) if reserr != nil { logging.Log(reserr) return nil, fmt.Errorf("Command failed: %#v", reserr) } logging.Log(res) return res.Result, nil }
// Send a request to the exchange func (ex *ExchangeClient) send(req *Request, res *RawResponse) error { logging.Log(req) body, err := json.Marshal(req) if err != nil { return err } hreq, err := http.NewRequest("POST", ex.rpcUrl, bytes.NewReader(body)) if err != nil { return err } hreq.Header.Add("Content-Type", "application/json") hreq.Header.Add("Content-Length", string(len(body))) hres, err := ex.rpc.Do(hreq) if err != nil { return err } hbody, err := ioutil.ReadAll(hres.Body) if err != nil { return err } json.Unmarshal(hbody, res) return nil }
// Listen for messages from the server func (ex *ExchangeClient) Listen() error { ws, err := websocket.Dial(ex.wsUrl, "", ex.rpcUrl) if err != nil { return err } ex.ws = ws for { raw := make([]byte, 1024) if err := websocket.Message.Receive(ex.ws, &raw); err != nil { fmt.Println("error:", err.Error()) ex.Done <- true return nil } var rawNotice RawNotification json.Unmarshal(raw, &rawNotice) notice := &Notification{ Version: rawNotice.Version, Method: rawNotice.Method, Params: nil, } switch rawNotice.Method { case "Offer": var params Offer json.Unmarshal(rawNotice.Params, ¶ms) notice.Params = params case "Bid": var params Bid json.Unmarshal(rawNotice.Params, ¶ms) notice.Params = params case "Close": var params Bid json.Unmarshal(rawNotice.Params, ¶ms) notice.Params = params case "Cancel": var params int json.Unmarshal(rawNotice.Params, ¶ms) notice.Params = params } logging.Log(notice) ex.Messages <- notice } return nil }
func main() { runtime.GOMAXPROCS(runtime.NumCPU()) // parse flags flag.IntVar(&broker, "broker", broker, "Broker Id") flag.StringVar(&exHost, "host", exHost, "Exchange host") flag.IntVar(&exPort, "port", exPort, "Exchange port") flag.StringVar(&dbHost, "dbhost", dbHost, "Aerospike host") flag.IntVar(&dbPort, "dbport", dbPort, "Aerospike port") flag.BoolVar(&logging.Enabled, "verbose", logging.Enabled, "Enable verbose logging") flag.Parse() // Log listener go logging.Listen() defer logging.Close() // Announce we're running logging.Log("Broker is running") // Resuse error var err error // Connect to database connectToDatabase(dbHost, dbPort) // Connect to exchange ex, err := NewExchangeClient(broker, exHost, uint16(exPort)) if err != nil { fmt.Printf("err: %#v\n", err.Error()) } // Close connections to the exchange defer ex.Close() // Open connections to the exchange go ex.Listen() // Process notifications go processNotifications(ex) // Run custom logic run(ex) // Wait for done to exit <-ex.Done }