Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
Distributed-Systems-Mps
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
owenw2
Distributed-Systems-Mps
Commits
d4f4f739
There was an error fetching the commit references. Please try again later.
Commit
d4f4f739
authored
1 month ago
by
owenw2
Browse files
Options
Downloads
Patches
Plain Diff
completed client code
parent
522da8d3
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
mp3/Client/client.go
+18
-11
18 additions, 11 deletions
mp3/Client/client.go
with
18 additions
and
11 deletions
mp3/Client/client.go
+
18
−
11
View file @
d4f4f739
...
...
@@ -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
"
,
""
)
// formatte
e
d 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
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment