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

completed server code

parent 172946eb
No related branches found
No related tags found
No related merge requests found
...@@ -57,6 +57,7 @@ type Request struct { ...@@ -57,6 +57,7 @@ type Request struct {
Branch string Branch string
Account string Account string
Amount int Amount int
Message string
} }
type Response struct { type Response struct {
...@@ -64,7 +65,7 @@ type Response struct { ...@@ -64,7 +65,7 @@ type Response struct {
Message string Message string
} }
var NUM_SERVERS int = 2 // number of servers var NUM_SERVERS int = 1 // number of servers
////////////////////////////// //////////////////////////////
// CLIENT FUNCTIONS // CLIENT FUNCTIONS
...@@ -272,7 +273,6 @@ func (server *Server) run_commit(clientName string) { ...@@ -272,7 +273,6 @@ func (server *Server) run_commit(clientName string) {
fmt.Println("Updated Accounts", server.ClientsAffected[clientName]) fmt.Println("Updated Accounts", server.ClientsAffected[clientName])
for { for {
fmt.Println("WAITING FOR VOTE ON", clientName)
vote := <-server.VoteClientChans[clientName] // get vote vote := <-server.VoteClientChans[clientName] // get vote
fmt.Println("Received vote", vote) fmt.Println("Received vote", vote)
...@@ -327,6 +327,8 @@ func (server *Server) run_commit(clientName string) { ...@@ -327,6 +327,8 @@ func (server *Server) run_commit(clientName string) {
server.handle_transaction(prepareRequest) server.handle_transaction(prepareRequest)
} }
} }
server.send_response_to_client(clientName, true, "COMMIT")
server.print_all_balances()
} }
} }
} }
...@@ -757,7 +759,13 @@ func (server *Server) handle_prepare(request Request) { ...@@ -757,7 +759,13 @@ func (server *Server) handle_prepare(request Request) {
server.Lock.Unlock() server.Lock.Unlock()
if !exists { if !exists {
server.VoteClientChans[request.ClientName] <- "ABORT" request.Transaction = "PLACEMESSAGE"
request.Message = "ABORT"
if request.PeerName == server.BranchName {
server.handle_transaction(request)
} else {
server.PeerEncs[server.BranchNameToAddress[request.PeerName]].Encode(&request)
}
return return
} }
...@@ -765,7 +773,25 @@ func (server *Server) handle_prepare(request Request) { ...@@ -765,7 +773,25 @@ func (server *Server) handle_prepare(request Request) {
entry, ok := account.TW[request.ClientName] entry, ok := account.TW[request.ClientName]
if !ok { if !ok {
server.VoteClientChans[request.ClientName] <- "ABORT" request.Transaction = "PLACEMESSAGE"
request.Message = "ABORT"
if request.PeerName == server.BranchName {
server.handle_transaction(request)
} else {
server.PeerEncs[server.BranchNameToAddress[request.PeerName]].Encode(&request)
}
account.AccountLock.Unlock()
return
}
if account.Balance < 0 {
request.Transaction = "PLACEMESSAGE"
request.Message = "ABORT"
if request.PeerName == server.BranchName {
server.handle_transaction(request)
} else {
server.PeerEncs[server.BranchNameToAddress[request.PeerName]].Encode(&request)
}
account.AccountLock.Unlock() account.AccountLock.Unlock()
return return
} }
...@@ -782,7 +808,13 @@ func (server *Server) handle_prepare(request Request) { ...@@ -782,7 +808,13 @@ func (server *Server) handle_prepare(request Request) {
if prevAllCommitted { if prevAllCommitted {
fmt.Println("PLACED COMMIT INTO CHANNEL", request.ClientName) fmt.Println("PLACED COMMIT INTO CHANNEL", request.ClientName)
server.VoteClientChans[request.ClientName] <- "COMMIT" request.Transaction = "PLACEMESSAGE"
request.Message = "COMMIT"
if request.PeerName == server.BranchName {
server.handle_transaction(request)
} else {
server.PeerEncs[server.BranchNameToAddress[request.PeerName]].Encode(&request)
}
break break
} else { } else {
account.AccountLock.Unlock() account.AccountLock.Unlock()
...@@ -814,6 +846,11 @@ func (server *Server) handle_transaction(request Request) { ...@@ -814,6 +846,11 @@ func (server *Server) handle_transaction(request Request) {
case "SEND": case "SEND":
fmt.Println("PEER", request.PeerName) fmt.Println("PEER", request.PeerName)
go server.send_response_to_client(request.ClientName, request.Amount == 1, request.Account) go server.send_response_to_client(request.ClientName, request.Amount == 1, request.Account)
case "PLACEMESSAGE":
server.Lock.Lock()
fmt.Println("PUTTING IN", request.Message, "INTO", request.ClientName)
server.VoteClientChans[request.ClientName] <- request.Message
server.Lock.Unlock()
} }
} }
......
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