@ -10,6 +10,7 @@ import (
"time"
"time"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/p2p/nat"
"github.com/ethereum/go-ethereum/p2p/nat"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rlp"
@ -29,7 +30,6 @@ const (
frameWriteTimeout = 5 * time . Second
frameWriteTimeout = 5 * time . Second
)
)
var srvlog = logger . NewLogger ( "P2P Server" )
var srvjslog = logger . NewJsonLogger ( )
var srvjslog = logger . NewJsonLogger ( )
// Server manages all peer connections.
// Server manages all peer connections.
@ -161,7 +161,7 @@ func (srv *Server) Start() (err error) {
if srv . running {
if srv . running {
return errors . New ( "server already running" )
return errors . New ( "server already running" )
}
}
srvlog . Infoln ( "Starting Server" )
glog . V ( logger . Info ) . Infoln ( "Starting Server" )
// static fields
// static fields
if srv . PrivateKey == nil {
if srv . PrivateKey == nil {
@ -204,7 +204,7 @@ func (srv *Server) Start() (err error) {
go srv . dialLoop ( )
go srv . dialLoop ( )
}
}
if srv . NoDial && srv . ListenAddr == "" {
if srv . NoDial && srv . ListenAddr == "" {
srvlog . Warn ln( "I will be kind-of useless, neither dialing nor listening." )
glog . V ( logger . Warn ) . Info ln( "I will be kind-of useless, neither dialing nor listening." )
}
}
srv . running = true
srv . running = true
@ -242,7 +242,7 @@ func (srv *Server) Stop() {
srv . running = false
srv . running = false
srv . lock . Unlock ( )
srv . lock . Unlock ( )
srvlog . Infoln ( "Stopping Server" )
glog . V ( logger . Info ) . Infoln ( "Stopping Server" )
srv . ntab . Close ( )
srv . ntab . Close ( )
if srv . listener != nil {
if srv . listener != nil {
// this unblocks listener Accept
// this unblocks listener Accept
@ -263,13 +263,13 @@ func (srv *Server) Stop() {
// main loop for adding connections via listening
// main loop for adding connections via listening
func ( srv * Server ) listenLoop ( ) {
func ( srv * Server ) listenLoop ( ) {
defer srv . loopWG . Done ( )
defer srv . loopWG . Done ( )
srvlog . Infoln ( "Listening on" , srv . listener . Addr ( ) )
glog . V ( logger . Info ) . Infoln ( "Listening on" , srv . listener . Addr ( ) )
for {
for {
conn , err := srv . listener . Accept ( )
conn , err := srv . listener . Accept ( )
if err != nil {
if err != nil {
return
return
}
}
srvlog . Debug f( "Accepted conn %v\n" , conn . RemoteAddr ( ) )
glog . V ( logger . Debug ) . Info f( "Accepted conn %v\n" , conn . RemoteAddr ( ) )
srv . peerWG . Add ( 1 )
srv . peerWG . Add ( 1 )
go srv . startPeer ( conn , nil )
go srv . startPeer ( conn , nil )
}
}
@ -328,10 +328,10 @@ func (srv *Server) dialLoop() {
func ( srv * Server ) dialNode ( dest * discover . Node ) {
func ( srv * Server ) dialNode ( dest * discover . Node ) {
addr := & net . TCPAddr { IP : dest . IP , Port : dest . TCPPort }
addr := & net . TCPAddr { IP : dest . IP , Port : dest . TCPPort }
srvlog . Debug f( "Dialing %v\n" , dest )
glog . V ( logger . Debug ) . Info f( "Dialing %v\n" , dest )
conn , err := srv . Dialer . Dial ( "tcp" , addr . String ( ) )
conn , err := srv . Dialer . Dial ( "tcp" , addr . String ( ) )
if err != nil {
if err != nil {
srvlog . DebugDetail f( "dial error: %v" , err )
glog . V ( logger . Detail ) . Info f( "dial error: %v" , err )
return
return
}
}
srv . startPeer ( conn , dest )
srv . startPeer ( conn , dest )
@ -365,7 +365,7 @@ func (srv *Server) startPeer(fd net.Conn, dest *discover.Node) {
conn , err := srv . setupFunc ( fd , srv . PrivateKey , srv . ourHandshake , dest )
conn , err := srv . setupFunc ( fd , srv . PrivateKey , srv . ourHandshake , dest )
if err != nil {
if err != nil {
fd . Close ( )
fd . Close ( )
srvlog . Debug f( "Handshake with %v failed: %v" , fd . RemoteAddr ( ) , err )
glog . V ( logger . Debug ) . Info f( "Handshake with %v failed: %v" , fd . RemoteAddr ( ) , err )
return
return
}
}
@ -375,12 +375,12 @@ func (srv *Server) startPeer(fd net.Conn, dest *discover.Node) {
}
}
p := newPeer ( fd , conn , srv . Protocols )
p := newPeer ( fd , conn , srv . Protocols )
if ok , reason := srv . addPeer ( conn . ID , p ) ; ! ok {
if ok , reason := srv . addPeer ( conn . ID , p ) ; ! ok {
srvlog . DebugDetail f( "Not adding %v (%v)\n" , p , reason )
glog . V ( logger . Detail ) . Info f( "Not adding %v (%v)\n" , p , reason )
p . politeDisconnect ( reason )
p . politeDisconnect ( reason )
return
return
}
}
srvlog . Debug f( "Added %v\n" , p )
glog . V ( logger . Debug ) . Info f( "Added %v\n" , p )
srvjslog . LogJson ( & logger . P2PConnected {
srvjslog . LogJson ( & logger . P2PConnected {
RemoteId : fmt . Sprintf ( "%x" , conn . ID [ : ] ) ,
RemoteId : fmt . Sprintf ( "%x" , conn . ID [ : ] ) ,
RemoteAddress : fd . RemoteAddr ( ) . String ( ) ,
RemoteAddress : fd . RemoteAddr ( ) . String ( ) ,
@ -394,7 +394,7 @@ func (srv *Server) startPeer(fd net.Conn, dest *discover.Node) {
discreason := p . run ( )
discreason := p . run ( )
srv . removePeer ( p )
srv . removePeer ( p )
srvlog . Debug f( "Removed %v (%v)\n" , p , discreason )
glog . V ( logger . Debug ) . Info f( "Removed %v (%v)\n" , p , discreason )
srvjslog . LogJson ( & logger . P2PDisconnected {
srvjslog . LogJson ( & logger . P2PDisconnected {
RemoteId : fmt . Sprintf ( "%x" , conn . ID [ : ] ) ,
RemoteId : fmt . Sprintf ( "%x" , conn . ID [ : ] ) ,
NumConnections : srv . PeerCount ( ) ,
NumConnections : srv . PeerCount ( ) ,