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 { ...@@ -23,6 +23,7 @@ type Client struct {
type Request struct { type Request struct {
ClientName string ClientName string
PeerName string
Transaction string // type of transaction Transaction string // type of transaction
Branch string Branch string
Account string Account string
...@@ -34,6 +35,9 @@ type Response struct { ...@@ -34,6 +35,9 @@ type Response struct {
Message string Message string
} }
var NUM_SERVERS int = 5 // number of servers
// fills client response channel
func (client *Client) run_server() { func (client *Client) run_server() {
// place server responses into channel // place server responses into channel
for { for {
...@@ -44,20 +48,24 @@ func (client *Client) run_server() { ...@@ -44,20 +48,24 @@ func (client *Client) run_server() {
} }
} }
// finds random server to elect as coordinator
func (client *Client) connect() { func (client *Client) connect() {
request := Request{} request := Request{}
response := Response{} response := Response{}
request.ClientName = client.Name request.ClientName = client.Name
request.PeerName = ""
request.Transaction = "BEGIN" request.Transaction = "BEGIN"
request.Branch = client.Name request.Branch = client.Name
request.Account = client.Name request.Account = client.Name
for _, server := range client.Servers { 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) connection, err := net.Dial("tcp", server)
if err != nil { if err != nil {
fmt.Println("Failed to connect to", server)
continue continue
} }
...@@ -71,17 +79,19 @@ func (client *Client) connect() { ...@@ -71,17 +79,19 @@ func (client *Client) connect() {
if response.Result { if response.Result {
client.Connection = connection client.Connection = connection
client.ResponseChan <- response client.ResponseChan <- response
fmt.Println("Client connected to", server)
go client.run_server() // start server if valid connection response go client.run_server() // start server if valid connection response
return return
} }
} }
fmt.Println("Was not able to connect to any servers")
client.ResponseChan <- response client.ResponseChan <- response
} }
// send request to coordinator server
func (client *Client) send_request(transaction string, account string, amount int) { func (client *Client) send_request(transaction string, account string, amount int) {
request := Request{} request := Request{}
request.ClientName = client.Name request.ClientName = client.Name
request.PeerName = ""
request.Transaction = transaction request.Transaction = transaction
request.Amount = amount request.Amount = amount
...@@ -97,6 +107,7 @@ func (client *Client) send_request(transaction string, account string, amount in ...@@ -97,6 +107,7 @@ func (client *Client) send_request(transaction string, account string, amount in
client.Encoder.Encode(&request) client.Encoder.Encode(&request)
} }
// read file and fill list of server ip:port
func (client *Client) read_file(config_name string) { func (client *Client) read_file(config_name string) {
config, error := os.Open(config_name) config, error := os.Open(config_name)
if error != nil { if error != nil {
...@@ -107,16 +118,14 @@ func (client *Client) read_file(config_name string) { ...@@ -107,16 +118,14 @@ func (client *Client) read_file(config_name string) {
reader := bufio.NewReader(config) reader := bufio.NewReader(config)
// assumption that only ever have 5 servers (no more, no less) // assumption that only ever have 5 servers (no more, no less)
for i := 0; i < 1; i++ { for i := 0; i < NUM_SERVERS; i++ {
text, serr := reader.ReadString('\n') text, _ := reader.ReadString('\n')
substrings := strings.Split(text, " ") substrings := strings.Split(text, " ")
if serr != nil && i < 0 {
fmt.Println("failed to read config")
}
// remove new line char from port // remove new line char from port
substrings[2] = strings.ReplaceAll(substrings[2], "\n", "") 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]) client.Servers = append(client.Servers, substrings[1]+":"+substrings[2])
} }
...@@ -138,8 +147,6 @@ func main() { ...@@ -138,8 +147,6 @@ func main() {
client.Name = clientName client.Name = clientName
client.read_file(configPath) 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) reader := bufio.NewReader(os.Stdin)
// get user inputs // 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