From d4f4f7392782bc877bf574fc410ecbc4ffc4153f Mon Sep 17 00:00:00 2001 From: owenw2 <owenw2@sp25-cs425-0601.cs.illinois.edu> Date: Thu, 8 May 2025 22:20:35 -0500 Subject: [PATCH] completed client code --- mp3/Client/client.go | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/mp3/Client/client.go b/mp3/Client/client.go index 51c0863..310cd8e 100644 --- a/mp3/Client/client.go +++ b/mp3/Client/client.go @@ -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 -- GitLab