Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
cs733
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Sushant Mahajan
cs733
Commits
bf5df9d4
Commit
bf5df9d4
authored
Feb 08, 2015
by
Bharath Radhakrishnan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added src folder
parent
35ce3a0f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
146 additions
and
0 deletions
+146
-0
assignment2/server.go
assignment2/server.go
+110
-0
assignment2/utils.go
assignment2/utils.go
+36
-0
No files found.
assignment2/server.go
0 → 100644
View file @
bf5df9d4
// server.go
package
main
import
(
"cs733/assignment2/handler"
"cs733/assignment2/kvstore"
"cs733/assignment2/raft"
"fmt"
"io/ioutil"
"log"
"net"
"net/rpc"
"os"
"reflect"
"strconv"
"time"
)
// Logger
var
Info
*
log
.
Logger
// Flag for enabling/disabling logging functionality
var
DEBUG
=
true
type
Args
struct
{
X
int
}
type
Reply
struct
{
X
int
}
type
AppendEntries
struct
{}
func
(
t
*
AppendEntries
)
AppendEntriesRPC
(
args
*
Args
,
reply
*
Reply
)
error
{
reply
.
X
=
args
.
X
return
nil
}
func
initInterServerCommunication
(
server
*
raft
.
ServerConfig
,
rft
*
raft
.
SharedLog
)
{
appendRpc
:=
new
(
AppendEntries
)
rpc
.
Register
(
appendRpc
)
listener
,
e
:=
net
.
Listen
(
"tcp"
,
":"
+
strconv
.
Itoa
(
this_server
.
LogPort
))
if
e
!=
nil
{
Info
.
Fatal
(
"listen error:"
,
e
)
}
for
{
if
conn
,
err
:=
listener
.
Accept
();
err
!=
nil
{
Info
.
Fatal
(
"accept error: "
+
err
.
Error
())
}
else
{
Info
.
Printf
(
"new connection established
\n
"
)
go
rpc
.
ServeConn
(
conn
)
}
}
}
// Initialize Logger
func
initLogger
(
serverId
int
)
{
// Logger Initializaion
if
!
DEBUG
{
Info
=
log
.
New
(
ioutil
.
Discard
,
"INFO: "
,
log
.
Ldate
|
log
.
Ltime
|
log
.
Lshortfile
)
}
else
{
Info
=
log
.
New
(
os
.
Stdout
,
"INFO: "
,
log
.
Ldate
|
log
.
Ltime
|
log
.
Lshortfile
)
}
Info
.
Println
(
"Initialized server"
)
}
func
initClientCommunication
(
server
*
raft
.
ServerConfig
,
rft
*
raft
.
SharedLog
)
{
listener
,
e
:=
net
.
Listen
(
"tcp"
,
":"
+
strconv
.
Itoa
(
server
.
ClientPort
))
if
e
!=
nil
{
Info
.
Fatal
(
"client listen error:"
,
e
)
}
for
{
if
conn
,
err
:=
listener
.
Accept
();
err
!=
nil
{
Info
.
Fatal
(
"client accept error: "
+
err
.
Error
())
}
else
{
Info
.
Printf
(
"client new connection established
\n
"
)
go
handler
.
HandleClient
(
conn
,
rft
)
}
}
}
func
main
()
{
sid
,
err
:=
strconv
.
Atoi
(
os
.
Args
[
1
])
if
err
!=
nil
{
Info
.
Println
(
"argument "
,
os
.
Args
[
1
],
"is not string"
)
}
initLogger
(
sid
)
Info
.
Println
(
"Start"
)
serverCount
,
err2
:=
strconv
.
Atoi
((
os
.
Args
[
2
]))
if
err2
!=
nil
{
Info
.
Println
(
"argument "
,
os
.
Args
[
2
],
"is not string"
)
}
server
,
_
:=
raft
.
NewServerConfig
(
sid
)
clusterConfig
,
_
:=
raft
.
NewClusterConfig
(
serverCount
)
commitCh
:=
make
(
chan
raft
.
LogEntry
)
rft
:=
raft
.
NewRaft
(
clusterConfig
,
sid
,
commitCh
)
initClientCommunication
(
server
,
rft
)
initInterServerCommunication
(
server
,
rft
)
var
dummy
string
fmt
.
Scanln
(
&
dummy
)
}
assignment2/utils.go
0 → 100644
View file @
bf5df9d4
// utils
package
utils
import
(
"encoding/gob"
"fmt"
)
type
Command
struct
{
cmd
[]
byte
val
[]
byte
}
func
(
d
*
Command
)
GobEncode
()
([]
byte
,
error
)
{
w
:=
new
(
bytes
.
Buffer
)
encoder
:=
gob
.
NewEncoder
(
w
)
err
:=
encoder
.
Encode
(
d
.
cmd
)
if
err
!=
nil
{
return
nil
,
err
}
err
=
encoder
.
Encode
(
d
.
val
)
if
err
!=
nil
{
return
nil
,
err
}
return
w
.
Bytes
(),
nil
}
func
(
d
*
Command
)
GobDecode
(
buf
[]
byte
)
error
{
r
:=
bytes
.
NewBuffer
(
buf
)
decoder
:=
gob
.
NewDecoder
(
r
)
err
:=
decoder
.
Decode
(
&
d
.
cmd
)
if
err
!=
nil
{
return
err
}
return
decoder
.
Decode
(
&
d
.
val
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment