浏览代码

Merge remote-tracking branch 'origin/master'

# Conflicts:
#	src/broker/modules/init.go
master
kidjung 4 年前
父节点
当前提交
d02a70a59e

+ 47
- 46
src/broker/modules/init.go 查看文件

@@ -128,49 +128,7 @@ func (receiver Receiver) MmReceive(args Args, reply *Reply) error { //
128 128
 	//reply.CompleteLog = "received completely"
129 129
 	return nil
130 130
 }
131
-
132
-func (moscato *Moscato) preProcessMsg(originalMsg MsgUnit) MsgUnit {
133
-	if originalMsg.CheckType() == PM {
134
-		pubMsg := originalMsg.(PublishMsg)
135
-		for index := 0; index < len(pubMsg.Topic); index++ {
136
-			pubMsg.Topic[index] = pubMsg.Topic[index] - moscato.SecureManager.GetNodeKey(pubMsg.From)
137
-		}
138
-		for index := 0; index < len(pubMsg.Value); index++ {
139
-			pubMsg.Value[index] = pubMsg.Value[index] - moscato.SecureManager.GetNodeKey(pubMsg.From)
140
-		}
141
-		return pubMsg
142
-	} else if originalMsg.CheckType() == SM {
143
-		subMsg := originalMsg.(SubscriptionMsg)
144
-		for index := 0; index < len(subMsg.Topic); index++ {
145
-			subMsg.Topic[index] = subMsg.Topic[index] - moscato.SecureManager.GetNodeKey(subMsg.From)
146
-		}
147
-		for index := 0; index < len(subMsg.Value); index++ {
148
-			subMsg.Value[index] = subMsg.Value[index] - moscato.SecureManager.GetNodeKey(subMsg.From)
149
-		}
150
-		return subMsg
151
-	}
152
-	return nil
153
-}
154
-
155
-func (moscato *Moscato) SendWithEncrypt() MsgUnit {
156
-	for {
157
-		mt := <-moscato.SendQueue
158
-		//fmt.Println(mt)
159
-		if mt.err == nil {
160
-			for index := 0; index < len(mt.subList); index++ {
161
-				tmpNode := mt.subList[index]
162
-				tmpNodeIpAddr, _ := moscato.MicroServiceManager.GetIpaddr(tmpNode)
163
-				//moscato.SecureManager.ReEncPubMsg(mt.pubMsg.(PublishMsg), tmpNode)
164
-				//fmt.Println("publish: ", mt.pubMsg)
165
-				//moscato.Send2MS(tmpNodeIpAddr, mt.pubMsg)
166
-				moscato.Send2MS(tmpNodeIpAddr, moscato.SecureManager.ReEncPubMsg(mt.pubMsg.(PublishMsg), tmpNode))
167
-			}
168
-		}
169
-		return nil
170
-	}
171
-}
172
-
173
-//Recieve - MM가 msg전달 받음
131
+//Recieve - MM가 MS로부터 메세지 전달받음
174 132
 func (moscato *Moscato) Receive(msg MsgUnit) (MsgUnit, error) {
175 133
 	l4g.LoadConfiguration("modules/logConfig.json")
176 134
 	//rpc call
@@ -252,7 +210,7 @@ MM→MS
252 210
 */
253 211
 
254 212
 func (moscato *Moscato) Send2MS(ipaddress string, msg MsgUnit) {
255
-	//log.LoadConfiguration("logConfig.json")
213
+	l4g.LoadConfiguration("logConfig.json")
256 214
 	client, err := rpc.Dial("tcp", ipaddress+":8150")
257 215
 	if err != nil {
258 216
 		fmt.Println(err)
@@ -277,6 +235,48 @@ func (moscato *Moscato) Send2MS(ipaddress string, msg MsgUnit) {
277 235
 	l4g.LOGGER("Test").Info(reply.CompleteLog)
278 236
 }
279 237
 
238
+//Matching을 용이하게 하기위한 메세지 가공 과정
239
+func (moscato *Moscato) preProcessMsg(originalMsg MsgUnit) MsgUnit {
240
+	if originalMsg.CheckType() == PM {
241
+		pubMsg := originalMsg.(PublishMsg)
242
+		for index := 0; index < len(pubMsg.Topic); index++ {
243
+			pubMsg.Topic[index] = pubMsg.Topic[index] - moscato.SecureManager.GetNodeKey(pubMsg.From)
244
+		}
245
+		for index := 0; index < len(pubMsg.Value); index++ {
246
+			pubMsg.Value[index] = pubMsg.Value[index] - moscato.SecureManager.GetNodeKey(pubMsg.From)
247
+		}
248
+		return pubMsg
249
+	} else if originalMsg.CheckType() == SM {
250
+		subMsg := originalMsg.(SubscriptionMsg)
251
+		for index := 0; index < len(subMsg.Topic); index++ {
252
+			subMsg.Topic[index] = subMsg.Topic[index] - moscato.SecureManager.GetNodeKey(subMsg.From)
253
+		}
254
+		for index := 0; index < len(subMsg.Value); index++ {
255
+			subMsg.Value[index] = subMsg.Value[index] - moscato.SecureManager.GetNodeKey(subMsg.From)
256
+		}
257
+		return subMsg
258
+	}
259
+	return nil
260
+}
261
+
262
+//암호화 해서 보내기
263
+func (moscato *Moscato) SendWithEncrypt() MsgUnit {
264
+	for {
265
+		mt := <-moscato.SendQueue
266
+		if mt.err == nil {
267
+			for index := 0; index < len(mt.subList); index++ { //sublist들을 돌면서 매세지를 encrypt하여 메세지 보냄
268
+				tmpNode := mt.subList[index]
269
+				tmpNodeIpAddr, _ := moscato.MicroServiceManager.GetIpaddr(tmpNode)
270
+				//moscato.SecureManager.ReEncPubMsg(mt.pubMsg.(PublishMsg), tmpNode)
271
+				//fmt.Println("publish: ", mt.pubMsg)
272
+				//moscato.Send2MS(tmpNodeIpAddr, mt.pubMsg)
273
+				moscato.Send2MS(tmpNodeIpAddr, moscato.SecureManager.ReEncPubMsg(mt.pubMsg.(PublishMsg), tmpNode))
274
+			}
275
+		}
276
+		return nil
277
+	}
278
+}
279
+
280 280
 func (moscato *Moscato) Run() {
281 281
 
282 282
 	config := AppConfig{moscato}
@@ -313,14 +313,15 @@ func (moscato *Moscato) Run() {
313 313
 
314 314
 	go Listen()
315 315
 	color.Blue("initializing complete.")
316
+
316 317
 	fmt.Scanln()
317 318
 }
318 319
 
319 320
 func Listen() {
320 321
 	/*
321
-		MS→MM일때 ⇒ port : 8160으로 열기
322
+	MS→MM일때 ⇒ port : 8160으로 열기
322 323
 
323
-		(MM이 Server, MS가 Client)
324
+	(MM이 Server, MS가 Client)
324 325
 	*/
325 326
 
326 327
 	l, err1 := net.Listen("tcp", ":8160")

+ 4
- 5
src/broker/modules/list.go 查看文件

@@ -22,7 +22,7 @@ type valueList struct {
22 22
 }
23 23
 
24 24
 type valueNode struct {
25
-	val  []int64 // Encrypted Topic
25
+	val  []int64 // Encrypted value
26 26
 	next *valueNode
27 27
 	prev *valueNode
28 28
 
@@ -40,7 +40,7 @@ type valueNode struct {
40 40
 	range2sub_eb []int // (sub_val >= pub_val) sub#s
41 41
 }
42 42
 
43
-// To delete slice Array
43
+// To delete element in slice Array
44 44
 func remove(ary []int, i int) []int {
45 45
 	return append(ary[:i], ary[i+1:]...)
46 46
 }
@@ -56,7 +56,7 @@ func findSub(ary []int, sub int) int {
56 56
 }
57 57
 
58 58
 
59
-// Compare ->  To compare (topics, values) in a node
59
+// Compare -> To compare two encrypted arrays
60 60
 func Compare(a []int64, b []int64) int {
61 61
 	if len(a) < len(b) {
62 62
 		return 1
@@ -104,7 +104,6 @@ func (l *topicList) addTopicNode(topic []int64) {
104 104
 	l.size++
105 105
 }
106 106
 
107
-
108 107
 // To add a Value node to the Value list
109 108
 func (l *valueList) addValueNode(value []int64) {
110 109
 	newValNode := &valueNode{value, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil}
@@ -164,7 +163,7 @@ func (node *valueNode) insertSub(op string, sub int, issingle bool) {
164 163
 	}
165 164
 }
166 165
 
167
-// op와 같은 node내의 operator리스트에서 sub가 있다면 리턴
166
+// To check if sub# is in the Value node's operator list
168 167
 func (node *valueNode) findOperatorList(op string, sub int, issingle bool) int {
169 168
 	ret := -1
170 169
 	if issingle == true {

+ 15
- 8
src/broker/modules/manage.go 查看文件

@@ -7,21 +7,24 @@ import (
7 7
 
8 8
 //각 Microservice에 대한 정보 저장 노드
9 9
 type MSNode struct {
10
-	nodeName string
10
+	nodeName string //Nodename- 현재 데모에서는 IpAddress와 같음
11 11
 	ipAddr   string
12 12
 }
13 13
 
14
+//Nodename 반환
14 15
 func (node *MSNode) GetName() string {
15 16
 	return node.nodeName
16 17
 }
18
+
19
+//IpAddress 반환
17 20
 func (node *MSNode) GetIpaddr() string {
18 21
 	return node.ipAddr
19 22
 }
20 23
 
21 24
 type NodeManager interface {
22
-	GetIpaddr(nodeName string) (string, bool)
23
-	AddMicroservice(node MSNode) bool
24
-	RemoveMicroservice(nodeName string) bool
25
+	GetIpaddr(nodeName string) (string, bool) //IpAddress반환
26
+	AddMicroservice(node MSNode) bool //MS추가
27
+	RemoveMicroservice(nodeName string) bool //MS삭제
25 28
 }
26 29
 
27 30
 //모든 Microservice정보 저장
@@ -29,11 +32,13 @@ type MStable struct {
29 32
 	NodeTable map[string]MSNode
30 33
 }
31 34
 
35
+//MStable 생성자
32 36
 func NewMStable() *MStable {
33 37
 	defer fmt.Println("node manager setting complete.")
34 38
 	return &MStable{NodeTable: make(map[string]MSNode)}
35 39
 }
36 40
 
41
+//IpAddress반환
37 42
 func (manager *MStable) GetIpaddr(nodeName string) (string, bool) {
38 43
 	//해당 이름의 노드이름이 존재하는지 확인
39 44
 	node, exists := manager.NodeTable[nodeName]
@@ -46,24 +51,26 @@ func (manager *MStable) GetIpaddr(nodeName string) (string, bool) {
46 51
 	}
47 52
 }
48 53
 
54
+//MS추가
49 55
 func (manager *MStable) AddMicroservice(node MSNode) bool {
50 56
 	//삽입 전 존재여부 확인
51
-	_, exists := manager.NodeTable[node.GetName()]
57
+	_, exists := manager.NodeTable[node.GetName()] ////해당 Node의 이름이 있는지 검색
52 58
 
53 59
 	if exists {
54 60
 		return false
55
-	} else {
61
+	} else { //존재안한다면 추가 (존재할경우 이미 있는것이기 때문에 추가할 필요 없음)
56 62
 		manager.NodeTable[node.GetName()] = node
57 63
 		return true
58 64
 	}
59 65
 }
60 66
 
67
+//MS삭제
61 68
 func (manager *MStable) RemoveMicroservice(nodeName string) bool {
62 69
 	//삭제 전 존재여부 확인
63
-	_, exists := manager.NodeTable[nodeName]
70
+	_, exists := manager.NodeTable[nodeName] //해당 이름을 가진 Node가 있는지 검색
64 71
 
65 72
 	if exists {
66
-		delete(manager.NodeTable, nodeName)
73
+		delete(manager.NodeTable, nodeName) //존재한다면 삭제
67 74
 		log.Println("[" + nodeName + "] : service quit")
68 75
 		return true
69 76
 	} else {

+ 37
- 22
src/broker/modules/matching.go 查看文件

@@ -8,24 +8,30 @@ import (
8 8
 )
9 9
 
10 10
 type match_manager struct {
11
-	match_count int
11
+	match_count int // Number of successful matches
12 12
 }
13 13
 
14
-// Matching -> Return (list of IP addresses of matched subs, Pub Msg, error)
14
+// Matching operation method
15
+// 1. Traverse TopicList for match (msg.topic == topicNode.topic)
16
+// 2. Traverse TopicNode.ValueList for match (msg.value <operator> valueNode.value)
17
+// 3. Add ipaddr to ipList when successful matching
18
+// 4. Add ipaddr to ipList when range matching
19
+// 5. Delete Duplicated Ipaddrs
20
+// @Return (list of IP addresses of matched subs, Pub Msg, error)
15 21
 func (moscato *Moscato) Matching(msg MsgUnit) {
16
-	//msg := moscato.queue.pop(true)
22
+
17 23
 	topic := msg.(PublishMsg).Topic
18 24
 	value := msg.(PublishMsg).Value
19 25
 	sub_mng := moscato.SubscriptionManager
20 26
 
21
-	// listß
22
-	ret := make([]string, 0)
27
+	// iplist for return
28
+	ipList := make([]string, 0)
23 29
 
24 30
 	// list for matched range subscriptions
25 31
 	big := make([]int, 0)
26 32
 	small := make([]int, 0)
27 33
 
28
-	// 1. Find (topicNode[Topic] == msg.Topic) Node
34
+	// Find (topicNode[Topic] == msg.Topic) Node
29 35
 	topicPtr := sub_mng.list
30 36
 	pos := topicPtr.getTopicNodePos(topic)
31 37
 
@@ -33,7 +39,7 @@ func (moscato *Moscato) Matching(msg MsgUnit) {
33 39
 	if pos == nil {
34 40
 		moscato.SendQueue <- myType{nil, msg, errors.New("Don't Exist Matching Topic")}
35 41
 	} else {
36
-		// 2. Traverse all valueNode -> and Match
42
+		// Traverse all valueNode -> and Match
37 43
 		valPtr := pos.list.head
38 44
 		for valPtr != nil {
39 45
 			compare := Compare(valPtr.val, value)
@@ -44,14 +50,14 @@ func (moscato *Moscato) Matching(msg MsgUnit) {
44 50
 				for i := 0; i < len(valPtr.single2sub_b); i++ {
45 51
 					sub := valPtr.single2sub_b[i]
46 52
 					ip := sub_mng.sub2ip[sub]
47
-					ret = append(ret, ip)
53
+					ipList = append(ipList, ip)
48 54
 				}
49 55
 
50 56
 				// (2) case : >=
51 57
 				for i := 0; i < len(valPtr.single2sub_eb); i++ {
52 58
 					sub := valPtr.single2sub_eb[i]
53 59
 					ip := sub_mng.sub2ip[sub]
54
-					ret = append(ret, ip)
60
+					ipList = append(ipList, ip)
55 61
 				}
56 62
 				// range : { >, >= }
57 63
 
@@ -75,14 +81,14 @@ func (moscato *Moscato) Matching(msg MsgUnit) {
75 81
 				for i := 0; i < len(valPtr.single2sub_s); i++ {
76 82
 					sub := valPtr.single2sub_s[i]
77 83
 					ip := sub_mng.sub2ip[sub]
78
-					ret = append(ret, ip)
84
+					ipList = append(ipList, ip)
79 85
 				}
80 86
 
81 87
 				// (2) case : <=
82 88
 				for i := 0; i < len(valPtr.single2sub_es); i++ {
83 89
 					sub := valPtr.single2sub_es[i]
84 90
 					ip := sub_mng.sub2ip[sub]
85
-					ret = append(ret, ip)
91
+					ipList = append(ipList, ip)
86 92
 				}
87 93
 
88 94
 				// range : { <, <= }
@@ -106,21 +112,21 @@ func (moscato *Moscato) Matching(msg MsgUnit) {
106 112
 				for i := 0; i < len(valPtr.single2sub_es); i++ {
107 113
 					sub := valPtr.single2sub_es[i]
108 114
 					ip := sub_mng.sub2ip[sub]
109
-					ret = append(ret, ip)
115
+					ipList = append(ipList, ip)
110 116
 				}
111 117
 
112 118
 				// (2) case : >=
113 119
 				for i := 0; i < len(valPtr.single2sub_eb); i++ {
114 120
 					sub := valPtr.single2sub_eb[i]
115 121
 					ip := sub_mng.sub2ip[sub]
116
-					ret = append(ret, ip)
122
+					ipList = append(ipList, ip)
117 123
 				}
118 124
 
119 125
 				// (3) case : ==
120 126
 				for i := 0; i < len(valPtr.single2sub_e); i++ {
121 127
 					sub := valPtr.single2sub_e[i]
122 128
 					ip := sub_mng.sub2ip[sub]
123
-					ret = append(ret, ip)
129
+					ipList = append(ipList, ip)
124 130
 				}
125 131
 
126 132
 				// range : { <=, >= }
@@ -142,17 +148,26 @@ func (moscato *Moscato) Matching(msg MsgUnit) {
142 148
 		}
143 149
 
144 150
 		// Add the intersection IP address of two sets (large and small) to the return list
145
-		hash := intersect.Hash(small, big)
146
-		list := reflect.ValueOf(hash)
151
+		intersectSubnumbers := intersect.Hash(small, big)
152
+		list := reflect.ValueOf(intersectSubnumbers)
147 153
 		for i := 0; i < list.Len(); i++ {
148 154
 			sub := list.Index(i).Interface().(int)
149 155
 			ip := sub_mng.sub2ip[sub]
150
-			ret = append(ret, ip)
156
+			ipList = append(ipList, ip)
151 157
 		}
152
-		//fmt.Println("big = ", big)
153
-		//fmt.Println("small = ", small)
154
-		//fmt.Println("ret = ", ret)
155 158
 		moscato.MatchingManager.match_count++
156
-		moscato.SendQueue <- myType{ret, msg, nil}
159
+
160
+		// To delete Duplicated Ipaddr
161
+		keys := make(map[string]bool)
162
+		retIpList := []string{}
163
+		for _, val := range ipList {
164
+			if _, saveVal := keys[val]; !saveVal{
165
+				keys[val] = true
166
+				retIpList = append(retIpList, val)
167
+			}
168
+		}
169
+
170
+		// Return {IpaddressList, PubMsg, ErrorMsg} to SendQueue
171
+		moscato.SendQueue <- myType{retIpList, msg, nil}
157 172
 	}
158
-}
173
+}

+ 6
- 6
src/broker/modules/queue.go 查看文件

@@ -11,14 +11,12 @@ type MsgQueue struct {
11 11
 }
12 12
 
13 13
 type QueueOperate interface {
14
-	//queue 초기화
15
-	queue_init() error
16
-	//queue push
17
-	push(msg Message) bool
18
-	//pop message (wait -> busy waiting 여부 결정)
19
-	pop(wait bool) (Message, error)
14
+	queue_init() error	//Queue 초기화 멤버함수
15
+	push(msg Message) bool //Queue push 멤버함수
16
+	pop(wait bool) (Message, error)	//Queue pop 멤버함수 (wait -> busy waiting 여부 결정)
20 17
 }
21 18
 
19
+//Message Queue를 초기화 해주는 함수
22 20
 func (mq *MsgQueue) queue_init() error {
23 21
 	if mq.queue != nil && len(mq.queue) != 0 {
24 22
 		return errors.New("Queue Hadlerer Error: Already initialized.")
@@ -34,11 +32,13 @@ func (mq *MsgQueue) queue_init() error {
34 32
 	}
35 33
 }
36 34
 
35
+//push 함수
37 36
 func (mq *MsgQueue) push(msg MsgUnit) bool {
38 37
 	mq.queue <- msg
39 38
 	return true
40 39
 }
41 40
 
41
+//pop 함수
42 42
 func (mq *MsgQueue) pop(block bool) MsgUnit {
43 43
 	if block == false {
44 44
 		if len(mq.queue) == 0 {

+ 4
- 45
src/broker/modules/secure.go 查看文件

@@ -11,6 +11,7 @@ type Security struct {
11 11
 	KeyMap map[string]string
12 12
 }
13 13
 
14
+//Security 생성자
14 15
 func NewSecurity() *Security {
15 16
 	security := &Security{map[string]string{}}
16 17
 	fmt.Println("security setting complete.")
@@ -28,7 +29,7 @@ type SecurityManager interface {
28 29
 	//CompareAlpha(topic1 []int64, topic2 []int64) int
29 30
 }
30 31
 
31
-/**
32
+/*
32 33
 keyShareMsg 에서 각 노드의 private 키를 받아 keyMap 에 저장
33 34
 */
34 35
 func (sc Security) RegKey(rm RegisterMsg) {
@@ -48,7 +49,7 @@ func (sc Security) GetNodeKey(nodeName string) int64 {
48 49
 	return mKey
49 50
 }
50 51
 
51
-/**
52
+/*
52 53
 reEncrypt 해서 슬라이스 반환
53 54
 */
54 55
 func (sc Security) ReEncrypt(fromKey int64, toKey int64, target []int64) []int64 {
@@ -83,6 +84,7 @@ func (sc Security) ReEncPubMsg(fromPubMsg PublishMsg, nodeName string) PublishMs
83 84
 	return toPubMsg
84 85
 }
85 86
 
87
+//Key제거 함수
86 88
 func (sc *Security) RemoveSecureKey(nodeName string) bool {
87 89
 	//삭제 전 존재여부 확인
88 90
 	_, exists := sc.KeyMap[nodeName]
@@ -95,46 +97,3 @@ func (sc *Security) RemoveSecureKey(nodeName string) bool {
95 97
 		return false
96 98
 	}
97 99
 }
98
-
99
-/**
100
-Compare 함수들은 같으면 0 다르면 -1 (비교가 필요한 경우 오름차순 1 내림차순 -1)
101
-*/
102
-//func (sc Security) CompareTopic(topic1 []int64, topic2 []int64) int {
103
-//	// 길이 같은지 체크
104
-//	if len(topic1) != len(topic2) {
105
-//		return -1
106
-//	}
107
-//	for i := 0; i < len(topic2); i++ {
108
-//		if topic1[i] != topic2[i] {
109
-//			return -1
110
-//		}
111
-//	}
112
-//	return 0
113
-//}
114
-//
115
-//func (sc Security) CompareDigit(topic1 int64, topic2 int64) int {
116
-//	if topic1 < topic2 {
117
-//		return 1
118
-//	} else if topic1 > topic2 {
119
-//		return -1
120
-//	}
121
-//	return 0
122
-//}
123
-//
124
-//func (sc Security) CompareAlpha(topic1 []int64, topic2 []int64) int {
125
-//	// 길이 같은지 체크
126
-//	if len(topic1) != len(topic2) {
127
-//		return -1
128
-//	}
129
-//	for i := 0; i < len(topic2); i++ {
130
-//		if topic1[i] != topic2[i] {
131
-//			return -1
132
-//		}
133
-//	}
134
-//	return 0
135
-//}
136
-
137
-// private key 생성 메세지
138
-//func (sc Security) keyGenPrivate() KeyGenMsg{
139
-//
140
-//}

正在加载...
取消
保存