Browse Source

cleanup

pull/9/head
Jae Kwon 11 years ago
parent
commit
2d39e463a5
4 changed files with 39 additions and 17 deletions
  1. +14
    -14
      p2p/addrbook.go
  2. +4
    -1
      p2p/connection.go
  3. +19
    -0
      p2p/log.go
  4. +2
    -2
      p2p/util.go

+ 14
- 14
p2p/addrbook.go View File

@ -120,7 +120,7 @@ func (a *AddrBook) Start() {
log.Trace("Starting address manager")
a.loadFromFile(a.filePath)
a.wg.Add(1)
go a.addressHandler()
go a.saveHandler()
}
func (a *AddrBook) Stop() {
@ -344,7 +344,7 @@ func (a *AddrBook) loadFromFile(filePath string) {
/* Private methods */
func (a *AddrBook) addressHandler() {
func (a *AddrBook) saveHandler() {
dumpAddressTicker := time.NewTicker(dumpAddressInterval)
out:
for {
@ -384,7 +384,7 @@ func (a *AddrBook) addAddress(addr, src *NetAddress) {
return
}
} else {
ka = NewknownAddress(addr, src)
ka = newKnownAddress(addr, src)
a.addrIndex[key] = ka
a.nNew++
}
@ -517,19 +517,19 @@ func (a *AddrBook) pickOld(bucket int) int {
func (a *AddrBook) getNewBucket(addr, src *NetAddress) int {
data1 := []byte{}
data1 = append(data1, a.key[:]...)
data1 = append(data1, []byte(GroupKey(addr))...)
data1 = append(data1, []byte(GroupKey(src))...)
hash1 := DoubleSha256(data1)
data1 = append(data1, []byte(groupKey(addr))...)
data1 = append(data1, []byte(groupKey(src))...)
hash1 := doubleSha256(data1)
hash64 := binary.LittleEndian.Uint64(hash1)
hash64 %= newBucketsPerGroup
var hashbuf [8]byte
binary.LittleEndian.PutUint64(hashbuf[:], hash64)
data2 := []byte{}
data2 = append(data2, a.key[:]...)
data2 = append(data2, GroupKey(src)...)
data2 = append(data2, groupKey(src)...)
data2 = append(data2, hashbuf[:]...)
hash2 := DoubleSha256(data2)
hash2 := doubleSha256(data2)
return int(binary.LittleEndian.Uint64(hash2) % newBucketCount)
}
@ -538,17 +538,17 @@ func (a *AddrBook) getOldBucket(addr *NetAddress) int {
data1 := []byte{}
data1 = append(data1, a.key[:]...)
data1 = append(data1, []byte(addr.String())...)
hash1 := DoubleSha256(data1)
hash1 := doubleSha256(data1)
hash64 := binary.LittleEndian.Uint64(hash1)
hash64 %= oldBucketsPerGroup
var hashbuf [8]byte
binary.LittleEndian.PutUint64(hashbuf[:], hash64)
data2 := []byte{}
data2 = append(data2, a.key[:]...)
data2 = append(data2, GroupKey(addr)...)
data2 = append(data2, groupKey(addr)...)
data2 = append(data2, hashbuf[:]...)
hash2 := DoubleSha256(data2)
hash2 := doubleSha256(data2)
return int(binary.LittleEndian.Uint64(hash2) % oldBucketCount)
}
@ -556,7 +556,7 @@ func (a *AddrBook) getOldBucket(addr *NetAddress) int {
// This is the /16 for IPv6, the /32 (/36 for he.net) for IPv6, the string
// "local" for a local address and the string "unroutable for an unroutable
// address.
func GroupKey(na *NetAddress) string {
func groupKey(na *NetAddress) string {
if na.Local() {
return "local"
}
@ -617,7 +617,7 @@ type knownAddress struct {
OldBucket Int16
}
func NewknownAddress(addr *NetAddress, src *NetAddress) *knownAddress {
func newKnownAddress(addr *NetAddress, src *NetAddress) *knownAddress {
return &knownAddress{
Addr: addr,
Src: src,
@ -627,7 +627,7 @@ func NewknownAddress(addr *NetAddress, src *NetAddress) *knownAddress {
}
}
func ReadknownAddress(r io.Reader) *knownAddress {
func readKnownAddress(r io.Reader) *knownAddress {
return &knownAddress{
Addr: ReadNetAddress(r),
Src: ReadNetAddress(r),


+ 4
- 1
p2p/connection.go View File

@ -43,7 +43,7 @@ type Connection struct {
errored uint32
}
var (
const (
PacketTypePing = UInt8(0x00)
PacketTypePong = UInt8(0x01)
PacketTypeMessage = UInt8(0x10)
@ -158,9 +158,11 @@ FOR_LOOP:
c.flush()
case <-c.pingRepeatTimer.Ch:
_, err = PacketTypePing.WriteTo(c.bufWriter)
log.Debugf("[%v] Sending Ping", c)
c.flush()
case <-c.pong:
_, err = PacketTypePong.WriteTo(c.bufWriter)
log.Debugf("[%v] Sending Pong", c)
c.flush()
case <-c.quit:
break FOR_LOOP
@ -206,6 +208,7 @@ FOR_LOOP:
c.pong <- struct{}{}
case PacketTypePong:
// do nothing
log.Debugf("[%v] Received Pong", c)
case PacketTypeMessage:
pkt, err := ReadPacketSafe(c.bufReader)
if err != nil {


+ 19
- 0
p2p/log.go View File

@ -6,6 +6,25 @@ import (
var log seelog.LoggerInterface
func init() {
config := `
<seelog type="asyncloop" minlevel="debug">
<outputs formatid="colored">
<console/>
</outputs>
<formats>
<format id="main" format="%Date/%Time [%LEV] %Msg%n"/>
<format id="colored" format="%Time %EscM(46)%Level%EscM(49) %EscM(36)%File%EscM(39) %Msg%n%EscM(0)"/>
</formats>
</seelog>`
var err error
log, err = seelog.LoggerFromConfigAsBytes([]byte(config))
if err != nil {
panic(err)
}
}
func SetLogger(l seelog.LoggerInterface) {
log = l
}

+ 2
- 2
p2p/util.go View File

@ -4,8 +4,8 @@ import (
"crypto/sha256"
)
// DoubleSha256 calculates sha256(sha256(b)) and returns the resulting bytes.
func DoubleSha256(b []byte) []byte {
// doubleSha256 calculates sha256(sha256(b)) and returns the resulting bytes.
func doubleSha256(b []byte) []byte {
hasher := sha256.New()
hasher.Write(b)
sum := hasher.Sum(nil)


Loading…
Cancel
Save