2013-12-30 00:54:50 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-01-11 16:27:08 +02:00
|
|
|
"container/list"
|
|
|
|
"github.com/ethereum/ethdb-go"
|
|
|
|
"github.com/ethereum/ethutil-go"
|
2014-01-12 17:50:59 +02:00
|
|
|
"github.com/ethereum/ethwire-go"
|
2014-01-11 16:27:08 +02:00
|
|
|
"log"
|
|
|
|
"net"
|
2014-01-12 17:50:59 +02:00
|
|
|
"time"
|
2014-01-13 00:46:03 +02:00
|
|
|
"sync/atomic"
|
2013-12-30 00:54:50 +02:00
|
|
|
)
|
|
|
|
|
2014-01-13 00:46:03 +02:00
|
|
|
func eachPeer(peers *list.List, callback func(*Peer, *list.Element)) {
|
2014-01-12 23:14:19 +02:00
|
|
|
// Loop thru the peers and close them (if we had them)
|
|
|
|
for e := peers.Front(); e != nil; e = e.Next() {
|
|
|
|
if peer, ok := e.Value.(*Peer); ok {
|
2014-01-13 00:46:03 +02:00
|
|
|
callback(peer, e)
|
2014-01-12 23:14:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-30 00:54:50 +02:00
|
|
|
type Server struct {
|
2014-01-11 16:27:08 +02:00
|
|
|
// Channel for shutting down the server
|
|
|
|
shutdownChan chan bool
|
|
|
|
// DB interface
|
2014-01-12 17:50:59 +02:00
|
|
|
//db *ethdb.LDBDatabase
|
|
|
|
db *ethdb.MemDatabase
|
2014-01-11 16:27:08 +02:00
|
|
|
// Block manager for processing new blocks and managing the block chain
|
|
|
|
blockManager *BlockManager
|
|
|
|
// Peers (NYI)
|
|
|
|
peers *list.List
|
2014-01-12 17:50:59 +02:00
|
|
|
// Nonce
|
|
|
|
Nonce uint64
|
2013-12-30 00:54:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewServer() (*Server, error) {
|
2014-01-12 17:50:59 +02:00
|
|
|
//db, err := ethdb.NewLDBDatabase()
|
|
|
|
db, err := ethdb.NewMemDatabase()
|
2014-01-11 16:27:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-12-30 00:54:50 +02:00
|
|
|
|
2014-01-11 16:27:08 +02:00
|
|
|
ethutil.SetConfig(db)
|
2014-01-09 00:43:20 +02:00
|
|
|
|
2014-01-12 17:50:59 +02:00
|
|
|
nonce, _ := ethutil.RandomUint64()
|
2014-01-11 16:27:08 +02:00
|
|
|
server := &Server{
|
|
|
|
shutdownChan: make(chan bool),
|
|
|
|
blockManager: NewBlockManager(),
|
|
|
|
db: db,
|
|
|
|
peers: list.New(),
|
2014-01-12 17:50:59 +02:00
|
|
|
Nonce: nonce,
|
2014-01-11 16:27:08 +02:00
|
|
|
}
|
2013-12-30 00:54:50 +02:00
|
|
|
|
2014-01-11 16:27:08 +02:00
|
|
|
return server, nil
|
2013-12-30 00:54:50 +02:00
|
|
|
}
|
|
|
|
|
2014-01-10 00:15:51 +02:00
|
|
|
func (s *Server) AddPeer(conn net.Conn) {
|
2014-01-12 17:50:59 +02:00
|
|
|
peer := NewPeer(conn, s, true)
|
|
|
|
|
|
|
|
if peer != nil {
|
|
|
|
s.peers.PushBack(peer)
|
|
|
|
peer.Start()
|
2014-01-10 01:39:16 +02:00
|
|
|
|
2014-01-12 17:50:59 +02:00
|
|
|
log.Println("Peer connected ::", conn.RemoteAddr())
|
|
|
|
}
|
2014-01-10 01:39:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ConnectToPeer(addr string) error {
|
2014-01-12 23:14:19 +02:00
|
|
|
peer := NewOutboundPeer(addr, s)
|
2014-01-10 01:39:16 +02:00
|
|
|
|
2014-01-11 16:27:08 +02:00
|
|
|
s.peers.PushBack(peer)
|
2014-01-12 23:14:19 +02:00
|
|
|
|
2014-01-11 16:27:08 +02:00
|
|
|
return nil
|
2014-01-10 01:39:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Broadcast(msgType string, data []byte) {
|
2014-01-13 00:46:03 +02:00
|
|
|
eachPeer(s.peers, func(p *Peer, e *list.Element) {
|
2014-01-12 23:14:19 +02:00
|
|
|
p.QueueMessage(ethwire.NewMessage(msgType, 0, data))
|
|
|
|
})
|
2014-01-10 00:15:51 +02:00
|
|
|
}
|
|
|
|
|
2014-01-13 00:46:03 +02:00
|
|
|
const (
|
|
|
|
processReapingTimeout = 10 // TODO increase
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) ReapDeadPeers() {
|
|
|
|
for {
|
|
|
|
eachPeer(s.peers, func(p *Peer, e *list.Element) {
|
|
|
|
if atomic.LoadInt32(&p.disconnect) == 1 {
|
|
|
|
log.Println("Dead peer found .. reaping")
|
|
|
|
|
|
|
|
s.peers.Remove(e)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
time.Sleep(processReapingTimeout * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-30 00:54:50 +02:00
|
|
|
// Start the server
|
|
|
|
func (s *Server) Start() {
|
2014-01-11 16:27:08 +02:00
|
|
|
// For now this function just blocks the main thread
|
|
|
|
ln, err := net.Listen("tcp", ":12345")
|
|
|
|
if err != nil {
|
2014-01-13 00:46:03 +02:00
|
|
|
// This is mainly for testing to create a "network"
|
|
|
|
if Debug {
|
|
|
|
log.Println("Connection listening disabled. Acting as client")
|
|
|
|
|
|
|
|
err = s.ConnectToPeer("localhost:12345")
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error starting server", err)
|
|
|
|
|
|
|
|
s.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-01-11 16:27:08 +02:00
|
|
|
}
|
|
|
|
|
2014-01-13 00:46:03 +02:00
|
|
|
// Start the reaping processes
|
|
|
|
go s.ReapDeadPeers()
|
|
|
|
|
2014-01-11 16:27:08 +02:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
conn, err := ln.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
2014-01-12 17:50:59 +02:00
|
|
|
|
2014-01-11 16:27:08 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
go s.AddPeer(conn)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// TMP
|
2014-01-12 17:50:59 +02:00
|
|
|
go func() {
|
2014-01-13 00:16:33 +02:00
|
|
|
//time.Sleep(500 * time.Millisecond)
|
|
|
|
|
2014-01-12 17:50:59 +02:00
|
|
|
for {
|
|
|
|
s.Broadcast("block", s.blockManager.bc.GenesisBlock().MarshalRlp())
|
|
|
|
|
|
|
|
time.Sleep(1000 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}()
|
2013-12-30 00:54:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Stop() {
|
2014-01-11 16:27:08 +02:00
|
|
|
// Close the database
|
|
|
|
defer s.db.Close()
|
2013-12-30 00:54:50 +02:00
|
|
|
|
2014-01-13 00:46:03 +02:00
|
|
|
eachPeer(s.peers, func(p *Peer, e *list.Element) {
|
2014-01-12 23:14:19 +02:00
|
|
|
p.Stop()
|
|
|
|
})
|
2013-12-30 00:54:50 +02:00
|
|
|
|
2014-01-11 16:27:08 +02:00
|
|
|
s.shutdownChan <- true
|
2013-12-30 00:54:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function will wait for a shutdown and resumes main thread execution
|
|
|
|
func (s *Server) WaitForShutdown() {
|
2014-01-11 16:27:08 +02:00
|
|
|
<-s.shutdownChan
|
2013-12-30 00:54:50 +02:00
|
|
|
}
|