6
6
"github.com/jonstout/ogo/openflow/ofp10"
7
7
"net"
8
8
"sync"
9
+ "runtime"
9
10
)
10
11
11
12
// Structure to track hosts that we discover.
@@ -14,8 +15,7 @@ type Host struct {
14
15
port uint16
15
16
}
16
17
17
- // A thread safe map to store our hosts. We are unlikely to
18
- // actually need a thread safe data structure in this demo.
18
+ // A thread safe map to store our hosts.
19
19
type HostMap struct {
20
20
hosts map [string ]Host
21
21
sync.RWMutex
@@ -27,30 +27,34 @@ func NewHostMap() *HostMap {
27
27
return h
28
28
}
29
29
30
+ // Returns the host associated with mac.
30
31
func (m * HostMap ) Host (mac net.HardwareAddr ) (h Host , ok bool ) {
31
32
m .RLock ()
32
33
defer m .RUnlock ()
33
34
h , ok = m .hosts [mac .String ()]
34
35
return
35
36
}
36
37
38
+ // Records the host mac address and the port where mac was discovered.
37
39
func (m * HostMap ) SetHost (mac net.HardwareAddr , port uint16 ) {
38
40
m .Lock ()
39
41
defer m .Unlock ()
40
42
m .hosts [mac .String ()] = Host {mac , port }
41
43
}
42
44
45
+ var hostMap HostMap
46
+
43
47
// Returns a new instance that implements one of the many
44
48
// interfaces found in ofp/ofp10/interface.go. One
45
49
// DemoInstance will be created for each switch that connects
46
50
// to the network.
47
51
func NewDemoInstance () interface {} {
48
- return & DemoInstance {* NewHostMap () }
52
+ return & DemoInstance {& hostMap }
49
53
}
50
54
51
55
// Acts as a simple learning switch.
52
56
type DemoInstance struct {
53
- HostMap
57
+ * HostMap
54
58
}
55
59
56
60
func (b * DemoInstance ) PacketIn (dpid net.HardwareAddr , pkt * ofp10.PacketIn ) {
@@ -92,8 +96,9 @@ func (b *DemoInstance) PacketIn(dpid net.HardwareAddr, pkt *ofp10.PacketIn) {
92
96
93
97
func main () {
94
98
fmt .Println ("Ogo 2013" )
95
-
99
+ runtime . GOMAXPROCS ( runtime . NumCPU ())
96
100
ctrl := core .NewController ()
101
+ hostMap = * NewHostMap ()
97
102
ctrl .RegisterApplication (NewDemoInstance )
98
103
ctrl .Listen (":6633" )
99
104
}
0 commit comments