refactor: separate object creation from activation

This commit is contained in:
Matthias Radestock committed 2015-01-19 13:23:25 +00:00
1 parent ebe48f5e58
commit 48c8f9ea01
7 files changed
+63 -46

No files matched your search

+12 -10
View File
@@ -33,31 +33,32 @@ func NewRouter(iface *net.Interface, name PeerName, password []byte, connLimit i
if len(password) > 0 {
router.Password = &password
}
router.Ourself = StartLocalPeer(name, router)
router.Ourself = NewLocalPeer(name, router)
router.Peers = NewPeers(router.Ourself.Peer, router.Macs, onPeerGC)
router.Peers.FetchWithDefault(router.Ourself.Peer)
log.Println("Our name is", router.Ourself.Name)
router.Routes = NewRoutes(router.Ourself.Peer, router.Peers)
router.ConnectionMaker = NewConnectionMaker(router.Ourself, router.Peers)
return router
}
func (router *Router) UsingPassword() bool {
return router.Password != nil
}
func (router *Router) Start() {
// we need two pcap handles since they aren't thread-safe
pio, err := NewPcapIO(router.Iface.Name, router.BufSz)
checkFatal(err)
po, err := NewPcapO(router.Iface.Name)
checkFatal(err)
router.Routes = StartRoutes(router.Ourself.Peer, router.Peers)
router.ConnectionMaker = StartConnectionMaker(router.Ourself, router.Peers)
router.Ourself.Start()
router.Routes.Start()
router.ConnectionMaker.Start()
router.UDPListener = router.listenUDP(Port, po)
router.listenTCP(Port)
router.sniff(pio)
}
func (router *Router) UsingPassword() bool {
return router.Password != nil
}
func (router *Router) Status() string {
var buf bytes.Buffer
buf.WriteString(fmt.Sprintln("Our name is", router.Ourself.Name))
@@ -159,7 +160,8 @@ func (router *Router) acceptTCP(tcpConn *net.TCPConn) {
remoteAddrStr := tcpConn.RemoteAddr().String()
log.Printf("->[%s] connection accepted\n", remoteAddrStr)
connRemote := NewRemoteConnection(router.Ourself.Peer, nil, remoteAddrStr)
NewLocalConnection(connRemote, true, tcpConn, nil, router)
connLocal := NewLocalConnection(connRemote, tcpConn, nil, router)
connLocal.Start(true)
}
func (router *Router) listenUDP(localPort int, po PacketSink) *net.UDPConn {