From e4187dec5e272b3fba3854988fa5896f1996e695 Mon Sep 17 00:00:00 2001 From: ak Date: Wed, 22 Aug 2018 22:39:03 -0700 Subject: [PATCH] saving work --- identitychain/identitychain.go | 58 ++++++++++++++++++++--------- identitychain/identitychain_test.go | 2 + runid/run_identity.go | 19 ++++++++++ runwait/run_wait.go | 18 +++++++++ waitnode/wait_node.go | 22 +++++++++-- 5 files changed, 98 insertions(+), 21 deletions(-) create mode 100644 runid/run_identity.go create mode 100644 runwait/run_wait.go diff --git a/identitychain/identitychain.go b/identitychain/identitychain.go index 1ebf21238..8e82f15d4 100644 --- a/identitychain/identitychain.go +++ b/identitychain/identitychain.go @@ -1,14 +1,15 @@ package identitychain import ( + "bufio" "fmt" "net" "os" + "strings" "sync" "github.com/simple-rules/harmony-benchmark/log" "github.com/simple-rules/harmony-benchmark/p2p" - "github.com/simple-rules/harmony-benchmark/proto" "github.com/simple-rules/harmony-benchmark/waitnode" ) @@ -31,21 +32,32 @@ func (IDC *IdentityChain) shard() { // This could have been its seperate package like consensus, but am avoiding creating a lot of packages. func (IDC *IdentityChain) IdentityChainHandler(conn net.Conn) { // Read p2p message payload - content, err := p2p.ReadMessageContent(conn) - if err != nil { - IDC.log.Error("Read p2p data failed") - return - } - fmt.Printf("content is %b", content) - msgCategory, err := proto.GetMessageCategory(content) - if err != nil { - IDC.log.Error("Read message category failed", "err", err) - return - } - if msgCategory != proto.IDENTITY { - IDC.log.Error("Identity Chain Recieved incorrect protocol message") - } - fmt.Println(msgCategory) + message, _ := bufio.NewReader(conn).ReadString('\n') + // output message received + fmt.Print("Message Received by IDC", string(message)) + // sample process for string received + newmessage := strings.ToUpper(message) + // send new string back to client + conn.Write([]byte(newmessage + "\n")) + ///////////////////////////////// + // content, err := p2p.ReadMessageContent(conn) + // if err != nil { + // IDC.log.Error("Read p2p data failed") + // return + // } + // fmt.Printf("content is %b", content) + // msgCategory, err := proto.GetMessageCategory(content) + // if err != nil { + // IDC.log.Error("Read message category failed", "err", err) + // return + // } + // if msgCategory != proto.IDENTITY { + // IDC.log.Error("Identity Chain Recieved incorrect protocol message") + // } + // fmt.Println(msgCategory) + + /////////////////////////////////// + // msgType, err := proto.GetMessageType(content) // if err != nil { // IDC.log.Error("Read action type failed", "err", err, "node", node) @@ -92,20 +104,30 @@ func (IDC *IdentityChain) UpdateIdentityChain() { } +//StartServer a server and process the request by a handler. +func (IDC *IdentityChain) StartServer() { + fmt.Println("Starting server...") + IDC.log.Info("Starting IDC server...") //log.Info does nothing for me! (ak) + IDC.listenOnPort() +} + func (IDC *IdentityChain) listenOnPort() { listen, err := net.Listen("tcp4", ":"+IDC.Peer.Port) if err != nil { IDC.log.Crit("Socket listen port failed") os.Exit(1) } else { - IDC.log.Info("Identity chain is now listening ..") + fmt.Println("Starting server...now listening") + IDC.log.Info("Identity chain is now listening ..") //log.Info does nothing for me! (ak) remove this } defer listen.Close() for { conn, err := listen.Accept() if err != nil { - IDC.log.Crit("Error listening on port. Exiting.", "port", IDC.Peer.Port) + IDC.log.Crit("Error listening on port. Exiting", IDC.Peer.Port) continue + } else { + fmt.Println("I am accepting connections now") } go IDC.IdentityChainHandler(conn) } diff --git a/identitychain/identitychain_test.go b/identitychain/identitychain_test.go index 6628ad142..10599f8ac 100644 --- a/identitychain/identitychain_test.go +++ b/identitychain/identitychain_test.go @@ -16,3 +16,5 @@ func TestIDCFormed(test *testing.T) { os.Exit(1) } } + +//TODO Mock netconnection to test whether identitychain is listening. diff --git a/runid/run_identity.go b/runid/run_identity.go new file mode 100644 index 000000000..37de5082e --- /dev/null +++ b/runid/run_identity.go @@ -0,0 +1,19 @@ +package main + +import ( + "flag" + "fmt" + + "github.com/simple-rules/harmony-benchmark/identitychain" + "github.com/simple-rules/harmony-benchmark/p2p" +) + +func main() { + ip := flag.String("ip", "127.0.0.0", "IP of the node") + port := flag.String("port", "9000", "port of the node.") + flag.Parse() + peer := p2p.Peer{Ip: *ip, Port: *port} + IDC := identitychain.New(peer) + fmt.Println(IDC) + IDC.StartServer() +} diff --git a/runwait/run_wait.go b/runwait/run_wait.go new file mode 100644 index 000000000..94ca8aefd --- /dev/null +++ b/runwait/run_wait.go @@ -0,0 +1,18 @@ +package main + +import ( + "flag" + + "github.com/simple-rules/harmony-benchmark/p2p" + "github.com/simple-rules/harmony-benchmark/waitnode" +) + +func main() { + ip := flag.String("ip", "127.0.0.0", "IP of the node") + port := flag.String("port", "8080", "port of the node") + flag.Parse() + peer := p2p.Peer{Ip: *ip, Port: *port} + idcpeer := p2p.Peer{Ip: "127.0.0.0", Port: "9000"} //Hardcoded here. + node := waitnode.New(peer) + node.ConnectIdentityChain(idcpeer) +} diff --git a/waitnode/wait_node.go b/waitnode/wait_node.go index 8970584f7..b48a5b571 100644 --- a/waitnode/wait_node.go +++ b/waitnode/wait_node.go @@ -1,13 +1,16 @@ package waitnode import ( + "bufio" "bytes" "crypto/sha256" "encoding/gob" + "fmt" "log" + "net" + "os" "github.com/simple-rules/harmony-benchmark/p2p" - "github.com/simple-rules/harmony-benchmark/proto/identity" "github.com/simple-rules/harmony-benchmark/utils" ) @@ -22,8 +25,21 @@ func (node *WaitNode) StartServer() { log.Printf("Starting waitnode on server %s and port %s", node.Peer.Ip, node.Peer.Port) } -func (node *WaitNode) connectIdentityChain(peer p2p.Peer) { - p2p.SendMessage(peer, identity.ConstructIdentityMessage(identity.REGISTER, node.SerializeWaitNode())) +//ConnectIdentityChain connects to identity chain +func (node *WaitNode) ConnectIdentityChain(peer p2p.Peer) { + fmt.Println("Connecting to Identity Chain") + + conn, err := net.Dial("tcp", ":"+peer.Port) + if err != nil { + fmt.Println("connection") + os.Exit(1) + } + text := "Hi I am alok" + fmt.Fprintf(conn, text+"\n") + message, _ := bufio.NewReader(conn).ReadString('\n') + fmt.Print("Message from server: " + message) + + //p2p.SendMessage(peer, identity.ConstructIdentityMessage(identity.REGISTER, node.SerializeWaitNode())) } //Constructs node-id by hashing the IP.