This repository was archived by the owner on Nov 23, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
129 lines (108 loc) · 2.55 KB
/
main.go
File metadata and controls
129 lines (108 loc) · 2.55 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright © 2019 Developer Network, LLC
//
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of this source code package.
package main
import (
"bufio"
"context"
"encoding/json"
"flag"
"fmt"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"github.com/google/uuid"
"go.atomizer.io/amqp"
"go.atomizer.io/engine"
"go.atomizer.io/montecarlopi"
)
type output struct {
In int `json:"in"`
Tosses int `json:"tosses"`
Errors int `json:"errors"`
PI float64 `json:"pi"`
}
func main() {
c := flag.String("conn", "amqp://guest:guest@localhost:5672/", "connection string used for rabbit mq")
q := flag.String("queue", "atomizer", "queue is the queue for atom messages to be passed across in the message queue")
flag.Parse()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
ctx, cancel := context.WithCancel(context.Background())
go sigterm(ctx, cancel, sigs)
// Create the amqp conductor for the agent
conductor, err := amqp.Connect(ctx, *c, *q)
if err != nil || conductor == nil {
fmt.Println("error while initializing amqp | " + err.Error())
os.Exit(1)
}
reader := bufio.NewReader(os.Stdin)
// TODO: read in tosses here
for {
select {
case <-ctx.Done():
return
default:
fmt.Printf("Enter Tosses: ")
text, _ := reader.ReadString('\n')
text = strings.Replace(text, "\r", "", -1)
text = strings.Replace(text, "\n", "", -1)
v, err := strconv.Atoi(text)
if err != nil || v < 1 {
fmt.Printf("Invalid number [%s]\n", text)
continue
}
e, err := electron(v)
if err != nil {
panic(err)
}
p, err := conductor.Send(ctx, e)
if err != nil {
panic(err)
}
select {
case <-ctx.Done():
return
case res, ok := <-p:
if !ok {
continue
}
o := output{}
err := json.Unmarshal(res.Result, &o)
if err != nil {
fmt.Println(err.Error())
}
fmt.Printf("Pi Estimation: %v\n", o.PI)
}
}
}
}
type tosspayload struct {
Tosses int `json:"tosses"`
}
func electron(tosses int) (*engine.Electron, error) {
e, err := json.Marshal(tosspayload{tosses})
if err != nil {
return &engine.Electron{}, err
}
electron := &engine.Electron{
ID: uuid.New().String(),
AtomID: engine.ID(montecarlopi.MonteCarlo{}),
Payload: e,
}
return electron, nil
}
// Setup interrupt monitoring for the agent
func sigterm(ctx context.Context, cancel context.CancelFunc, sigs chan os.Signal) {
select {
case <-ctx.Done():
cancel()
return
case <-sigs:
cancel()
os.Exit(1)
}
}