Exemple #1
0
// RPC : transmit data via UDP
func (m *Module) RPC(host string, method string, args ...string) ([]byte, error) {

	// parse UDP addresses
	udpRemoteAddress, err := net.ResolveUDPAddr("udp", host)
	if err != nil {
		return nil, err
	}
	udpClientAddress, err := net.ResolveUDPAddr("udp", "0.0.0.0:0")
	if err != nil {
		return nil, err
	}

	// open client socket
	conn, err := net.DialUDP("udp", udpClientAddress, udpRemoteAddress)
	if err != nil {
		return nil, err
	}
	defer conn.Close()
	reader := bufio.NewReader(conn)
	writer := bufio.NewWriter(conn)

	var a api.RemoteCall
	a.Action = method
	a.Args = args
	b, err := json.Marshal(a)
	if err != nil {
		return nil, err
	}

	// send data
	writer.Write(b)
	writer.WriteByte(DELIM)
	writer.Flush()

	// get response
	resp, err := reader.ReadBytes(DELIM)
	if err != nil {
		log.Println(err.Error())
		return nil, err
	}
	return resp, nil
}
Exemple #2
0
// RPC : client interface
func (h *Module) RPC(host string, method string, args ...string) ([]byte, error) {
	var a api.RemoteCall
	a.Action = method
	a.Args = args

	b, err := json.Marshal(a)
	if err != nil {
		return nil, err
	}
	//log.Println("POSTING: " + string(b))
	req, _ := http.NewRequest("POST", "https://"+host, bytes.NewReader(b))
	//req.Close = true
	req.Header.Add("Accept", "application/json")

	resp, err := h.client.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	buf, err := ioutil.ReadAll(resp.Body)
	return buf, err
}