From fb6ff61730ed92ada68c9c5a5b3a6f9976a78161 Mon Sep 17 00:00:00 2001 From: Maran Date: Mon, 2 Jun 2014 15:20:27 +0200 Subject: Implemented Public Peer interface --- ethpub/pub.go | 13 +++++++++++++ ethpub/types.go | 30 +++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) (limited to 'ethpub') diff --git a/ethpub/pub.go b/ethpub/pub.go index a9a962f14..6d4c230ad 100644 --- a/ethpub/pub.go +++ b/ethpub/pub.go @@ -6,6 +6,7 @@ import ( "github.com/ethereum/eth-go/ethutil" "math/big" "strings" + "sync/atomic" ) type PEthereum struct { @@ -51,6 +52,18 @@ func (lib *PEthereum) GetPeerCount() int { return lib.manager.PeerCount() } +func (lib *PEthereum) GetPeers() []PPeer { + var peers []PPeer + for peer := lib.manager.Peers().Front(); peer != nil; peer = peer.Next() { + p := peer.Value.(ethchain.Peer) + if atomic.LoadInt32(p.Connected()) != 0 { + peers = append(peers, *NewPPeer(p)) + } + } + + return peers +} + func (lib *PEthereum) GetIsMining() bool { return lib.manager.IsMining() } diff --git a/ethpub/types.go b/ethpub/types.go index 4e7c44ed4..1079f09b4 100644 --- a/ethpub/types.go +++ b/ethpub/types.go @@ -3,12 +3,40 @@ package ethpub import ( "encoding/hex" "encoding/json" + "fmt" "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethutil" - _ "log" "strings" ) +// Peer interface exposed to QML + +type PPeer struct { + ref *ethchain.Peer + Inbound bool `json:"isInbound"` + LastSend int64 `json:"lastSend"` + LastPong int64 `json:"lastPong"` + Ip string `json:"ip"` + Port int `json:"port"` + Version string `json:"version"` + LastResponse string `json:"lastResponse"` +} + +func NewPPeer(peer ethchain.Peer) *PPeer { + if peer == nil { + return nil + } + + // TODO: There must be something build in to do this? + var ip []string + for _, i := range peer.Host() { + ip = append(ip, fmt.Sprintf("%d", i)) + } + ipAddress := strings.Join(ip, ".") + + return &PPeer{ref: &peer, Inbound: peer.Inbound(), LastSend: peer.LastSend().Unix(), LastPong: peer.LastPong(), Version: peer.Version(), Ip: ipAddress, Port: int(peer.Port())} +} + // Block interface exposed to QML type PBlock struct { ref *ethchain.Block -- cgit v1.2.3