Lab 6 Consistency With Sockets RPC and Message Passing
Lab 6 Consistency With Sockets RPC and Message Passing
Step-by-Step Instructions:
replica_eventual.go
package main
import (
"bufio"
"fmt"
"net"
"os"
"strings"
"sync"
"time"
)
func main() {
if len(os.Args) < 3 {
fmt.Println("Usage: go run replica_eventual.go
<machine_ip:port> <peer1_ip:port> [<peer2_ip:port>...]")
return
}
go func() {
for {
conn, err := listener.Accept()
if err != nil {
continue
}
go handleConnection(conn, replica)
}
}()
// Simulate an update
replica.Update("key1", "value1")
replica.propagateUpdates("key1", "value1")
Objective:
replica_strong.go
package main
import (
"fmt"
"net"
"net/rpc"
"os"
"sync"
)
func main() {
if len(os.Args) < 3 {
fmt.Println("Usage: go run replica_strong.go
<machine_ip:port> <peer1_ip:port> [<peer2_ip:port>...]")
return
}
rpc.Register(replica)
go func() {
for {
conn, err := listener.Accept()
if err != nil {
continue
}
go rpc.ServeConn(conn)
}
}()
replica_numerical.go
package main
import (
"fmt"
"math"
"net"
"strings"
"sync"
)
r.mu.Lock()
message := fmt.Sprintf("%.2f\n", r.value)
r.mu.Unlock()
conn.Write([]byte(message))
}(peer)
}
}
func main() {
if len(os.Args) < 3 {
fmt.Println("Usage: go run replica_numerical .go
<machine_ip:port> <peer1_ip:port> [<peer2_ip:port>...]")
return
}
peers := os.Args[2:]
replica := &Replica{
value: 10.0,
peers: peers,
}
delta := 5.0
listener, err := net.Listen("tcp", ":8000")
if err != nil {
panic(err)
}
defer listener.Close()
go func() {
for {
conn, err := listener.Accept()
if err != nil {
continue
}
go handleConnection(conn, replica, delta)
}
}()
// Simulate an update
replica.value = 12.0
replica.propagateUpdates(delta)
fmt.Println("Replica Value:", replica.value)
}
package main
import (
"fmt"
"log"
"net/rpc"
"time"
)
func main() {
client, err := rpc.Dial("tcp", "localhost:1234")
if err != nil {
log.Fatal("Error connecting to RPC server:", err)
}
Objective:
In this exercise, students will:
1. Extend the calculator to maintain state (e.g., store the result of the last
operation).
2. Implement a method to retrieve the last result from the server.
Exercise Steps: