Skip to content
Snippets Groups Projects
Commit d4f4f739 authored by owenw2's avatar owenw2
Browse files

completed client code

parent 522da8d3
No related branches found
No related tags found
No related merge requests found
......@@ -23,6 +23,7 @@ type Client struct {
type Request struct {
ClientName string
PeerName string
Transaction string // type of transaction
Branch string
Account string
......@@ -34,6 +35,9 @@ type Response struct {
Message string
}
var NUM_SERVERS int = 5 // number of servers
// fills client response channel
func (client *Client) run_server() {
// place server responses into channel
for {
......@@ -44,20 +48,24 @@ func (client *Client) run_server() {
}
}
// finds random server to elect as coordinator
func (client *Client) connect() {
request := Request{}
response := Response{}
request.ClientName = client.Name
request.PeerName = ""
request.Transaction = "BEGIN"
request.Branch = client.Name
request.Account = client.Name
for _, server := range client.Servers {
fmt.Println("Sending connection request to", server)
ip := strings.Split(server, ":")[0]
port, _ := strconv.Atoi(strings.Split(server, ":")[1])
server := ip + ":" + strconv.Itoa(port+10)
connection, err := net.Dial("tcp", server)
if err != nil {
fmt.Println("Failed to connect to", server)
continue
}
......@@ -71,17 +79,19 @@ func (client *Client) connect() {
if response.Result {
client.Connection = connection
client.ResponseChan <- response
fmt.Println("Client connected to", server)
go client.run_server() // start server if valid connection response
return
}
}
fmt.Println("Was not able to connect to any servers")
client.ResponseChan <- response
}
// send request to coordinator server
func (client *Client) send_request(transaction string, account string, amount int) {
request := Request{}
request.ClientName = client.Name
request.PeerName = ""
request.Transaction = transaction
request.Amount = amount
......@@ -97,6 +107,7 @@ func (client *Client) send_request(transaction string, account string, amount in
client.Encoder.Encode(&request)
}
// read file and fill list of server ip:port
func (client *Client) read_file(config_name string) {
config, error := os.Open(config_name)
if error != nil {
......@@ -107,16 +118,14 @@ func (client *Client) read_file(config_name string) {
reader := bufio.NewReader(config)
// assumption that only ever have 5 servers (no more, no less)
for i := 0; i < 1; i++ {
text, serr := reader.ReadString('\n')
for i := 0; i < NUM_SERVERS; i++ {
text, _ := reader.ReadString('\n')
substrings := strings.Split(text, " ")
if serr != nil && i < 0 {
fmt.Println("failed to read config")
}
// remove new line char from port
substrings[2] = strings.ReplaceAll(substrings[2], "\n", "")
// formatteed server address as sp25-cs425-0601.cs.illinois.edu:1234
// formatted server address as sp25-cs425-0601.cs.illinois.edu:1234
client.Servers = append(client.Servers, substrings[1]+":"+substrings[2])
}
......@@ -138,8 +147,6 @@ func main() {
client.Name = clientName
client.read_file(configPath)
fmt.Println("Client State After Config:", client.Name, client.Connection, client.Connected, client.Servers, len(client.Servers))
reader := bufio.NewReader(os.Stdin)
// get user inputs
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment