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

@ -16,3 +16,5 @@ func TestIDCFormed(test *testing.T) {
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
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.

Loading…
Cancel
Save