-
Notifications
You must be signed in to change notification settings - Fork 0
/
submission_agent.go
67 lines (53 loc) · 1.23 KB
/
submission_agent.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
65
66
67
// MsgBox Submission Agent
//
// The Submission Agent is responsible for writing a message
// to an outhoing queue that can eventually be delivered to a recipient.
//
// It uses protocol buffers to pass messages around and should
// encode a message into a protocol buffer before pushing it to
// the queue.
package submission_agent
import (
"fmt"
"github.com/msgbox/queue"
"github.com/msgbox/submission-agent/submission_agent"
"github.com/streadway/amqp"
"io/ioutil"
"net"
)
type session struct {
queue *amqp.Connection
}
func CreateAgent(port string) {
tcpAddr, err := net.ResolveTCPAddr("tcp4", port)
checkError(err)
listener, err := net.ListenTCP("tcp", tcpAddr)
checkError(err)
// Create an AMQP Connection
queueConn, err := queue.Connect()
checkError(err)
defer queueConn.Close()
s := &session{
queue: queueConn,
}
for {
conn, err := listener.Accept()
if err != nil {
continue
}
go handleMessage(conn, s)
}
}
func handleMessage(conn net.Conn, s *session) {
// close connection on exit
defer conn.Close()
result, err := ioutil.ReadAll(conn)
checkError(err)
err = agent.Send(result, s.queue)
checkError(err)
}
func checkError(err error) {
if err != nil {
fmt.Printf("Fatal error: %s", err.Error())
}
}