/
userdef.go
100 lines (80 loc) · 2.2 KB
/
userdef.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package tengu
import (
"chord"
"github.com/sirupsen/logrus"
"strconv"
)
/* In this file, you should implement function "NewNode" and
* a struct which implements the interface "dhtNode".
*/
func NewNode(port int) dhtNode {
// Todo: create a node and then return it.
ret := NewPubNode(":" + strconv.Itoa(port))
return ret
}
// Todo: implement a struct which implements the interface "dhtNode".
/* PubNode which implements the interface "dhtNode"
* PubNode -> Receiver, Receiver (RCVR) is for RPC
* Receiver -> Node, Node is the true node in DHT network
*/
type PubNodeType struct {
receiver *chord.ReceiverType
}
func NewPubNode(_addr string) *PubNodeType{
return &PubNodeType{
receiver: chord.NewReceiver(_addr),
}
}
func (this *PubNodeType) Run() {
var err error
err = this.receiver.Server.Register(this.receiver)
if err != nil {
chord.Log.WithFields(logrus.Fields{
}).Error("Register Error. " + err.Error())
}
this.receiver.Node.Running = true
go this.receiver.Node.Updating()
chord.Log.WithFields(logrus.Fields{
"addr" : this.receiver.Node.Addr,
}).Info("Running Success!")
this.receiver.Server.Accept(this.receiver.Listener)
}
func (this *PubNodeType) Create() {
this.receiver.Node.Create()
}
func (this PubNodeType) Join(addr string) bool {
return this.receiver.Node.Join(addr)
}
func (this PubNodeType) Quit() {
this.receiver.Node.Quit()
this.receiver.Node.Running = false
err := this.receiver.Listener.Close()
if err == nil {
chord.Log.WithFields(logrus.Fields{
"ip" : this.receiver.Node.Addr,
}).Info("Quit Success.")
}
}
func (this PubNodeType) ForceQuit() {
this.receiver.Node.ForceQuit()
this.receiver.Node.Running = false
err := this.receiver.Listener.Close()
if err == nil {
chord.Log.WithFields(logrus.Fields{
"ip" : this.receiver.Node.Addr,
}).Info("Node Force Quit.")
}
}
func (this PubNodeType) Ping(addr string) bool {
err := chord.Ping(addr)
return err == nil
}
func (this PubNodeType) Put(key string, value string) bool {
return this.receiver.Node.Put(key, value)
}
func (this PubNodeType) Get(key string) (bool, string) {
return this.receiver.Node.Get(key)
}
func (this *PubNodeType) Delete(key string) bool {
return this.receiver.Node.Delete(key)
}