saving work

pull/61/head
ak 6 years ago
parent 4a97f541cd
commit e4187dec5e
  1. 58
      identitychain/identitychain.go
  2. 2
      identitychain/identitychain_test.go
  3. 19
      runid/run_identity.go
  4. 18
      runwait/run_wait.go
  5. 22
      waitnode/wait_node.go

@ -1,14 +1,15 @@
package identitychain package identitychain
import ( import (
"bufio"
"fmt" "fmt"
"net" "net"
"os" "os"
"strings"
"sync" "sync"
"github.com/simple-rules/harmony-benchmark/log" "github.com/simple-rules/harmony-benchmark/log"
"github.com/simple-rules/harmony-benchmark/p2p" "github.com/simple-rules/harmony-benchmark/p2p"
"github.com/simple-rules/harmony-benchmark/proto"
"github.com/simple-rules/harmony-benchmark/waitnode" "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. // This could have been its seperate package like consensus, but am avoiding creating a lot of packages.
func (IDC *IdentityChain) IdentityChainHandler(conn net.Conn) { func (IDC *IdentityChain) IdentityChainHandler(conn net.Conn) {
// Read p2p message payload // Read p2p message payload
content, err := p2p.ReadMessageContent(conn) message, _ := bufio.NewReader(conn).ReadString('\n')
if err != nil { // output message received
IDC.log.Error("Read p2p data failed") fmt.Print("Message Received by IDC", string(message))
return // sample process for string received
} newmessage := strings.ToUpper(message)
fmt.Printf("content is %b", content) // send new string back to client
msgCategory, err := proto.GetMessageCategory(content) conn.Write([]byte(newmessage + "\n"))
if err != nil { /////////////////////////////////
IDC.log.Error("Read message category failed", "err", err) // content, err := p2p.ReadMessageContent(conn)
return // if err != nil {
} // IDC.log.Error("Read p2p data failed")
if msgCategory != proto.IDENTITY { // return
IDC.log.Error("Identity Chain Recieved incorrect protocol message") // }
} // fmt.Printf("content is %b", content)
fmt.Println(msgCategory) // 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) // msgType, err := proto.GetMessageType(content)
// if err != nil { // if err != nil {
// IDC.log.Error("Read action type failed", "err", err, "node", node) // 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() { func (IDC *IdentityChain) listenOnPort() {
listen, err := net.Listen("tcp4", ":"+IDC.Peer.Port) listen, err := net.Listen("tcp4", ":"+IDC.Peer.Port)
if err != nil { if err != nil {
IDC.log.Crit("Socket listen port failed") IDC.log.Crit("Socket listen port failed")
os.Exit(1) os.Exit(1)
} else { } 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() defer listen.Close()
for { for {
conn, err := listen.Accept() conn, err := listen.Accept()
if err != nil { 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 continue
} else {
fmt.Println("I am accepting connections now")
} }
go IDC.IdentityChainHandler(conn) go IDC.IdentityChainHandler(conn)
} }

@ -16,3 +16,5 @@ func TestIDCFormed(test *testing.T) {
os.Exit(1) os.Exit(1)
} }
} }
//TODO Mock netconnection to test whether identitychain is listening.

@ -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()
}

@ -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)
}

@ -1,13 +1,16 @@
package waitnode package waitnode
import ( import (
"bufio"
"bytes" "bytes"
"crypto/sha256" "crypto/sha256"
"encoding/gob" "encoding/gob"
"fmt"
"log" "log"
"net"
"os"
"github.com/simple-rules/harmony-benchmark/p2p" "github.com/simple-rules/harmony-benchmark/p2p"
"github.com/simple-rules/harmony-benchmark/proto/identity"
"github.com/simple-rules/harmony-benchmark/utils" "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) log.Printf("Starting waitnode on server %s and port %s", node.Peer.Ip, node.Peer.Port)
} }
func (node *WaitNode) connectIdentityChain(peer p2p.Peer) { //ConnectIdentityChain connects to identity chain
p2p.SendMessage(peer, identity.ConstructIdentityMessage(identity.REGISTER, node.SerializeWaitNode())) 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. //Constructs node-id by hashing the IP.

Loading…
Cancel
Save