Commit 71bae140 authored by Bharath Radhakrishnan's avatar Bharath Radhakrishnan

RPC skeleton

parent cab533d6
package main package main
import ( import (
//"log"
//"net"
//"net/rpc"
"fmt" "fmt"
"log"
"net"
"net/rpc"
"os" "os"
"reflect" "reflect"
"strconv" "strconv"
...@@ -26,7 +26,7 @@ type LogEntry interface { ...@@ -26,7 +26,7 @@ type LogEntry interface {
} }
type LogEntryData struct { type LogEntryData struct {
id uint64 id Lsn
data []byte data []byte
committed bool committed bool
} }
...@@ -54,7 +54,7 @@ type Raft struct { ...@@ -54,7 +54,7 @@ type Raft struct {
var cluster_config *ClusterConfig var cluster_config *ClusterConfig
//make LogEntryData implement the //make LogEntryData implement the
func (entry *LogEntryData) Lsn() uint64 { func (entry *LogEntryData) Lsn() Lsn {
return entry.id return entry.id
} }
...@@ -96,6 +96,34 @@ func (e ErrRedirect) Error() string { ...@@ -96,6 +96,34 @@ func (e ErrRedirect) Error() string {
return "Redirect to server " + strconv.Itoa(cluster_config.Servers[0].Id) return "Redirect to server " + strconv.Itoa(cluster_config.Servers[0].Id)
} }
type Args struct {
X int
}
type AppendEntries struct{}
func (t *AppendEntries) AppendEntriesRPC(args *Args, reply *int) error {
*reply = args.X
return nil
}
func initializeInterServerCommunication(this_server *ServerConfig) {
appendRpc := new(AppendEntries)
rpc.Register(appendRpc)
listener, e := net.Listen("tcp", ":"+strconv.Itoa(this_server.LogPort))
if e != nil {
log.Fatal("listen error:", e)
}
for {
if conn, err := listener.Accept(); err != nil {
log.Fatal("accept error: " + err.Error())
} else {
log.Printf("new connection established\n")
go rpc.ServeConn(conn)
}
}
}
func main() { func main() {
server_id, err := strconv.Atoi(os.Args[1]) server_id, err := strconv.Atoi(os.Args[1])
if err != nil { if err != nil {
...@@ -111,4 +139,5 @@ func main() { ...@@ -111,4 +139,5 @@ func main() {
fmt.Println(reflect.TypeOf(this_server)) fmt.Println(reflect.TypeOf(this_server))
fmt.Println(reflect.TypeOf(cluster_config)) fmt.Println(reflect.TypeOf(cluster_config))
initializeInterServerCommunication(this_server)
} }
...@@ -14,19 +14,21 @@ const ( ...@@ -14,19 +14,21 @@ const (
NUM_SERVERS int = 5 NUM_SERVERS int = 5
) )
func TestServersCommunic() { func TestServersCommunic(i int) {
for i := 0; i < NUM_SERVERS; i++ { cmd := exec.Command("go", "run", "replic_kvstore.go", strconv.Itoa(i+1), strconv.Itoa(NUM_SERVERS))
cmd := exec.Command("go", "run", "replic_kvstore.go", strconv.Itoa(i+1), strconv.Itoa(NUM_SERVERS)) out, err := cmd.CombinedOutput()
out, err := cmd.CombinedOutput() if err != nil {
if err != nil { fmt.Println(err)
fmt.Println(err)
}
fmt.Println(string(out))
} }
fmt.Println(string(out))
} }
func main() { func main() {
TestServersCommunic() for i := 0; i < NUM_SERVERS; i++ {
go TestServersCommunic(i)
}
var dummy_input string var dummy_input string
fmt.Scanln(&dummy_input) fmt.Scanln(&dummy_input)
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment