-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcontroller.go
103 lines (83 loc) · 1.91 KB
/
controller.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
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
package gofc
import (
"fmt"
"net"
"github.com/Kmotiko/gofc/ofprotocol/ofp13"
)
var DEFAULT_PORT = 6653
/**
* basic controller
*/
type OFController struct {
echoInterval int32 // echo interval
}
func NewOFController() *OFController {
ofc := new(OFController)
ofc.echoInterval = 60
return ofc
}
// func (c *OFController) HandleHello(msg *ofp13.OfpHello, dp *Datapath) {
// fmt.Println("recv Hello")
// // send feature request
// featureReq := ofp13.NewOfpFeaturesRequest()
// Send(dp, featureReq)
// }
func (c *OFController) HandleSwitchFeatures(msg *ofp13.OfpSwitchFeatures, dp *Datapath) {
fmt.Println("recv SwitchFeatures")
// handle FeatureReply
dp.datapathId = msg.DatapathId
}
func (c *OFController) HandleEchoRequest(msg *ofp13.OfpHeader, dp *Datapath) {
fmt.Println("recv EchoReq")
// send EchoReply
echo := ofp13.NewOfpEchoReply()
(*dp).Send(echo)
}
func (c *OFController) ConnectionUp() {
// handle connection up
}
func (c *OFController) ConnectionDown() {
// handle connection down
}
func (c *OFController) sendEchoLoop() {
// send echo request forever
}
func ServerLoop(listenPort int) {
var port int
if listenPort <= 0 || listenPort >= 65536 {
fmt.Println("Invalid port was specified. listen port must be between 0 - 65535.")
return
}
port = listenPort
tcpAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf(":%d", port))
listener, err := net.ListenTCP("tcp", tcpAddr)
ofc := NewOFController()
GetAppManager().RegistApplication(ofc)
if err != nil {
return
}
// wait for connect from switch
for {
conn, err := listener.AcceptTCP()
if err != nil {
return
}
go handleConnection(conn)
}
}
/**
*
*/
func handleConnection(conn *net.TCPConn) {
// send hello
hello := ofp13.NewOfpHello()
_, err := conn.Write(hello.Serialize())
if err != nil {
fmt.Println(err)
}
// create datapath
dp := NewDatapath(conn)
// launch goroutine
go dp.recvLoop()
go dp.sendLoop()
}