Skip to content

Commit

Permalink
Improve commandline output in stats/verbose/debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
gombadi committed Sep 13, 2015
1 parent 10bc716 commit ae8212f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 26 deletions.
33 changes: 17 additions & 16 deletions crawler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func crawlNode(s *dnsseeder, nd *node) {
defer crawlEnd(nd)

if config.debug {
log.Printf("debug - start crawl: node %s status: %v:%v lastcrawl: %s\n",
log.Printf("%s - debug - start crawl: node %s status: %v:%v lastcrawl: %s\n",
s.name,
net.JoinHostPort(nd.na.IP.String(),
strconv.Itoa(int(nd.na.Port))),
nd.status,
Expand All @@ -39,7 +40,7 @@ func crawlNode(s *dnsseeder, nd *node) {
}

// connect to the remote ip and ask them for their addr list
rna, e := crawlIP(s.pver, s.id, nd, s.isFull())
rna, e := s.crawlIP(nd)

if e != nil {
// update the fact that we have not connected to this node
Expand Down Expand Up @@ -140,7 +141,7 @@ func crawlEnd(nd *node) {
}

// crawlIP retrievs a slice of ip addresses from a client
func crawlIP(pver uint32, netID wire.BitcoinNet, nd *node, full bool) ([]*wire.NetAddress, *crawlError) {
func (s *dnsseeder) crawlIP(nd *node) ([]*wire.NetAddress, *crawlError) {

ip := nd.na.IP.String()
port := strconv.Itoa(int(nd.na.Port))
Expand All @@ -150,14 +151,14 @@ func crawlIP(pver uint32, netID wire.BitcoinNet, nd *node, full bool) ([]*wire.N
conn, err := net.DialTimeout("tcp", dialString, time.Second*10)
if err != nil {
if config.debug {
log.Printf("debug - Could not connect to %s - %v\n", ip, err)
log.Printf("%s - debug - Could not connect to %s - %v\n", s.name, ip, err)
}
return nil, &crawlError{"", err}
}

defer conn.Close()
if config.debug {
log.Printf("debug - Connected to remote address: %s Last connect was %v ago\n", ip, time.Since(nd.lastConnect).String())
log.Printf("%s - debug - Connected to remote address: %s Last connect was %v ago\n", s.name, ip, time.Since(nd.lastConnect).String())
}

// set a deadline for all comms to be done by. After this all i/o will error
Expand All @@ -170,14 +171,14 @@ func crawlIP(pver uint32, netID wire.BitcoinNet, nd *node, full bool) ([]*wire.N
return nil, &crawlError{"Create NewMsgVersionFromConn", err}
}

err = wire.WriteMessage(conn, msgver, pver, netID)
err = wire.WriteMessage(conn, msgver, s.pver, s.id)
if err != nil {
// Log and handle the error
return nil, &crawlError{"Write Version Message", err}
}

// first message received should be version
msg, _, err := wire.ReadMessage(conn, pver, netID)
msg, _, err := wire.ReadMessage(conn, s.pver, s.id)
if err != nil {
// Log and handle the error
return nil, &crawlError{"Read message after sending Version", err}
Expand All @@ -187,7 +188,7 @@ func crawlIP(pver uint32, netID wire.BitcoinNet, nd *node, full bool) ([]*wire.N
case *wire.MsgVersion:
// The message is a pointer to a MsgVersion struct.
if config.debug {
log.Printf("%s - Remote version: %v\n", ip, msg.ProtocolVersion)
log.Printf("%s - debug - %s - Remote version: %v\n", s.name, ip, msg.ProtocolVersion)
}
// fill the node struct with the remote details
nd.version = msg.ProtocolVersion
Expand All @@ -205,35 +206,35 @@ func crawlIP(pver uint32, netID wire.BitcoinNet, nd *node, full bool) ([]*wire.N
// send verack command
msgverack := wire.NewMsgVerAck()

err = wire.WriteMessage(conn, msgverack, pver, netID)
err = wire.WriteMessage(conn, msgverack, s.pver, s.id)
if err != nil {
return nil, &crawlError{"writing message VerAck", err}
}

// second message received should be verack
msg, _, err = wire.ReadMessage(conn, pver, netID)
msg, _, err = wire.ReadMessage(conn, s.pver, s.id)
if err != nil {
return nil, &crawlError{"reading expected Ver Ack from remote client", err}
}

switch msg.(type) {
case *wire.MsgVerAck:
if config.debug {
log.Printf("%s - received Version Ack\n", ip)
log.Printf("%s - debug - %s - received Version Ack\n", s.name, ip)
}
default:
return nil, &crawlError{"Did not receive expected Ver Ack message from remote client", errors.New("")}
}

// if we get this far and if the seeder is full then don't ask for addresses. This will reduce bandwith usage while still
// confirming that we can connect to the remote node
if full {
if s.isFull() {
return nil, nil
}
// send getaddr command
msgGetAddr := wire.NewMsgGetAddr()

err = wire.WriteMessage(conn, msgGetAddr, pver, netID)
err = wire.WriteMessage(conn, msgGetAddr, s.pver, s.id)
if err != nil {
return nil, &crawlError{"writing Addr message to remote client", err}
}
Expand All @@ -245,19 +246,19 @@ func crawlIP(pver uint32, netID wire.BitcoinNet, nd *node, full bool) ([]*wire.N
// Using the Bitcoin lib for the some networks means it does not understand some
// of the commands and will error. We can ignore these as we are only
// interested in the addr message and its content.
msgaddr, _, _ := wire.ReadMessage(conn, pver, netID)
msgaddr, _, _ := wire.ReadMessage(conn, s.pver, s.id)
if msgaddr != nil {
switch msg := msgaddr.(type) {
case *wire.MsgAddr:
// received the addr message so return the result
if config.debug {
log.Printf("%s - received valid addr message\n", ip)
log.Printf("%s - debug - %s - received valid addr message\n", s.name, ip)
}
dowhile = false
return msg.AddrList, nil
default:
if config.debug {
log.Printf("%s - ignoring message - %v\n", ip, msg.Command())
log.Printf("%s - debug - %s - ignoring message - %v\n", s.name, ip, msg.Command())
}
}
}
Expand Down
15 changes: 13 additions & 2 deletions dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,19 @@ func updateDNS(s *dnsseeder) {

config.dnsmtx.Unlock()

if config.debug {
log.Printf("debug - DNS update complete - rr4std: %v rr4non: %v rr6std: %v rr6non: %v\n", len(rr4std), len(rr4non), len(rr6std), len(rr6non))
if config.stats {
s.counts.mtx.RLock()
log.Printf("%s - DNS available: v4std: %v v4non: %v v6std: %v v6non: %v\n", s.name, len(rr4std), len(rr4non), len(rr6std), len(rr6non))
log.Printf("%s - DNS counts: v4std: %v v4non: %v v6std: %v v6non: %v total: %v\n",
s.name,
s.counts.DNSCounts[dnsV4Std],
s.counts.DNSCounts[dnsV4Non],
s.counts.DNSCounts[dnsV6Std],
s.counts.DNSCounts[dnsV6Non],
s.counts.DNSCounts[dnsV4Std]+s.counts.DNSCounts[dnsV4Non]+s.counts.DNSCounts[dnsV6Std]+s.counts.DNSCounts[dnsV6Non])

s.counts.mtx.RUnlock()

}
}

Expand Down
10 changes: 5 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func main() {
var j bool

// FIXME - update with git hash during build
config.version = "0.6.0"
config.version = "0.8.0"
config.uptime = time.Now()

flag.StringVar(&netfile, "netfile", "", "List of json config files to load")
Expand Down Expand Up @@ -101,12 +101,12 @@ func main() {
config.stats = true
}

for _, v := range config.seeders {
log.Printf("status - system is configured for network: %s\n", v.name)
}

if config.verbose == false {
log.Printf("status - Running in quiet mode with limited output produced\n")
} else {
for _, v := range config.seeders {
log.Printf("status - system is configured for network: %s\n", v.name)
}
}

// start the web interface if we want it running
Expand Down
10 changes: 7 additions & 3 deletions seeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (s *dnsseeder) startCrawlers() {
tcount := len(s.theList)
if tcount == 0 {
if config.debug {
log.Printf("debug - startCrawlers fail: no node ailable\n")
log.Printf("%s - debug - startCrawlers fail: no node ailable\n", s.name)
}
return
}
Expand Down Expand Up @@ -171,14 +171,18 @@ func (s *dnsseeder) startCrawlers() {
c.started++
}

log.Printf("%s: started crawler: %s total: %v started: %v\n", s.name, c.desc, c.totalCount, c.started)
if config.stats {
log.Printf("%s: started crawler: %s total: %v started: %v\n", s.name, c.desc, c.totalCount, c.started)
}

// update the global stats in another goroutine to free the main goroutine
// for other work
go updateNodeCounts(s, c.status, c.totalCount, c.started)
}

log.Printf("%s: crawlers started. total clients: %d\n", s.name, tcount)
if config.stats {
log.Printf("%s: crawlers started. total clients: %d\n", s.name, tcount)
}

// returns and read lock released
}
Expand Down

0 comments on commit ae8212f

Please sign in to comment.