2013-12-29 22:54:50 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-01-11 14:27:08 +00:00
|
|
|
"container/list"
|
|
|
|
"github.com/ethereum/ethdb-go"
|
|
|
|
"github.com/ethereum/ethutil-go"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
_ "time"
|
2013-12-29 22:54:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2014-01-11 14:27:08 +00:00
|
|
|
// Channel for shutting down the server
|
|
|
|
shutdownChan chan bool
|
|
|
|
// DB interface
|
|
|
|
db *ethdb.LDBDatabase
|
|
|
|
// Block manager for processing new blocks and managing the block chain
|
|
|
|
blockManager *BlockManager
|
|
|
|
// Peers (NYI)
|
|
|
|
peers *list.List
|
2013-12-29 22:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewServer() (*Server, error) {
|
2014-01-11 14:27:08 +00:00
|
|
|
db, err := ethdb.NewLDBDatabase()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-12-29 22:54:50 +00:00
|
|
|
|
2014-01-11 14:27:08 +00:00
|
|
|
ethutil.SetConfig(db)
|
2014-01-08 22:43:20 +00:00
|
|
|
|
2014-01-11 14:27:08 +00:00
|
|
|
server := &Server{
|
|
|
|
shutdownChan: make(chan bool),
|
|
|
|
blockManager: NewBlockManager(),
|
|
|
|
db: db,
|
|
|
|
peers: list.New(),
|
|
|
|
}
|
2013-12-29 22:54:50 +00:00
|
|
|
|
2014-01-11 14:27:08 +00:00
|
|
|
return server, nil
|
2013-12-29 22:54:50 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 22:15:51 +00:00
|
|
|
func (s *Server) AddPeer(conn net.Conn) {
|
2014-01-11 14:27:08 +00:00
|
|
|
peer := NewPeer(conn, s)
|
|
|
|
s.peers.PushBack(peer)
|
|
|
|
peer.Start()
|
2014-01-09 23:39:16 +00:00
|
|
|
|
2014-01-11 14:27:08 +00:00
|
|
|
log.Println("Peer connected ::", conn.RemoteAddr())
|
2014-01-09 23:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ConnectToPeer(addr string) error {
|
2014-01-11 14:27:08 +00:00
|
|
|
conn, err := net.Dial("tcp", addr)
|
2014-01-09 23:39:16 +00:00
|
|
|
|
2014-01-11 14:27:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-09 23:39:16 +00:00
|
|
|
|
2014-01-11 14:27:08 +00:00
|
|
|
peer := NewPeer(conn, s)
|
|
|
|
s.peers.PushBack(peer)
|
|
|
|
peer.Start()
|
2014-01-09 23:39:16 +00:00
|
|
|
|
2014-01-11 14:27:08 +00:00
|
|
|
log.Println("Connected to peer ::", conn.RemoteAddr())
|
2014-01-09 23:39:16 +00:00
|
|
|
|
2014-01-11 14:27:08 +00:00
|
|
|
return nil
|
2014-01-09 23:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Broadcast(msgType string, data []byte) {
|
2014-01-11 14:27:08 +00:00
|
|
|
for e := s.peers.Front(); e != nil; e = e.Next() {
|
|
|
|
if peer, ok := e.Value.(*Peer); ok {
|
|
|
|
peer.QueueMessage(msgType, data)
|
|
|
|
}
|
|
|
|
}
|
2014-01-09 22:15:51 +00:00
|
|
|
}
|
|
|
|
|
2013-12-29 22:54:50 +00:00
|
|
|
// Start the server
|
|
|
|
func (s *Server) Start() {
|
2014-01-11 14:27:08 +00:00
|
|
|
// For now this function just blocks the main thread
|
|
|
|
ln, err := net.Listen("tcp", ":12345")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
conn, err := ln.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
go s.AddPeer(conn)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// TMP
|
|
|
|
//go func() {
|
|
|
|
// for {
|
|
|
|
// s.Broadcast("block", Encode("blockdata"))
|
|
|
|
//
|
|
|
|
// time.Sleep(100 * time.Millisecond)
|
|
|
|
// }
|
|
|
|
// }()
|
2013-12-29 22:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Stop() {
|
2014-01-11 14:27:08 +00:00
|
|
|
// Close the database
|
|
|
|
defer s.db.Close()
|
2013-12-29 22:54:50 +00:00
|
|
|
|
2014-01-11 14:27:08 +00:00
|
|
|
// Loop thru the peers and close them (if we had them)
|
|
|
|
for e := s.peers.Front(); e != nil; e = e.Next() {
|
|
|
|
if peer, ok := e.Value.(*Peer); ok {
|
|
|
|
peer.Stop()
|
|
|
|
}
|
|
|
|
}
|
2013-12-29 22:54:50 +00:00
|
|
|
|
2014-01-11 14:27:08 +00:00
|
|
|
s.shutdownChan <- true
|
2013-12-29 22:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function will wait for a shutdown and resumes main thread execution
|
|
|
|
func (s *Server) WaitForShutdown() {
|
2014-01-11 14:27:08 +00:00
|
|
|
<-s.shutdownChan
|
2013-12-29 22:54:50 +00:00
|
|
|
}
|