/
client.go
64 lines (59 loc) · 1.69 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"fmt"
"flag"
"bytes"
"strconv"
"net/http"
"github.com/gorilla/rpc/json"
)
type Args struct {
StockSymbolAndPercentage string
Budget float32
TradeId int
}
type Reply struct {
Message string
}
func ClientRequest(method string, args Args) (reply Reply, err error) {
req, err := json.EncodeClientRequest(method, args)
if err != nil {
fmt.Println("ClientRequest Error")
fmt.Println(err)
return reply, err
}
res, err := http.Post("http://127.0.0.1:8080/rpc", "application/json", bytes.NewBuffer(req))
if err != nil{
fmt.Println("Post Error")
fmt.Println(err)
return reply, err
}
defer res.Body.Close()
err = json.DecodeClientResponse(res.Body, &reply)
return reply, err
}
func main() {
var buyargs Args
var checkargs Args
flag.Parse() // get the arguments from command line
method := "Service."+flag.Arg(0) // get the source directory from 1st argument
if flag.Arg(0) == "Buying" {
fmt.Println("Buying Now...")
fmt.Println(method)
buyargs.StockSymbolAndPercentage = flag.Arg(1)
budget,_ := strconv.ParseFloat(flag.Arg(2), 64)
id,_ := strconv.Atoi(flag.Arg(3))
buyargs.TradeId = id;
buyargs.Budget = float32(budget)
reply,_ := ClientRequest(method, buyargs)
fmt.Println(reply.Message)
}
if flag.Arg(0) == "Checking" {
fmt.Println("Checking Now...")
fmt.Println(method)
id,_ := strconv.Atoi(flag.Arg(1))
checkargs.TradeId = id
reply,_ := ClientRequest(method, checkargs)
fmt.Println(reply.Message)
}
}