Pārlūkot izejas kodu

change secure test

master
kidjung 4 gadus atpakaļ
vecāks
revīzija
a5d30e300b

+ 47
- 68
src/broker/modules/list.go Parādīt failu

@@ -1,8 +1,6 @@
1 1
 package modules
2 2
 
3
-/*
4
-	Sub정보들을 관리할 list
5
-*/
3
+/* List to manage Sub information */
6 4
 
7 5
 type topicList struct {
8 6
 	head *topicNode
@@ -11,7 +9,7 @@ type topicList struct {
11 9
 }
12 10
 
13 11
 type topicNode struct {
14
-	topic []int64 // Encrypt topic
12
+	topic []int64 // Encrypted topic
15 13
 	next  *topicNode
16 14
 	prev  *topicNode
17 15
 	list  valueList
@@ -24,29 +22,30 @@ type valueList struct {
24 22
 }
25 23
 
26 24
 type valueNode struct {
27
-	val  []int64 // Encrypt된 value
25
+	val  []int64 // Encrypted topic
28 26
 	next *valueNode
29 27
 	prev *valueNode
30 28
 
31 29
 	// single //
32
-	single2sub_s  []int // (val < x) sub#
33
-	single2sub_es []int // (val <= x) sub#
34
-	single2sub_b  []int // (val > x) sub#
35
-	single2sub_eb []int // (val >= x) sub#
36
-	single2sub_e  []int // (val == x) sub#
30
+	single2sub_s  []int // (sub_val < pub_val) sub#s
31
+	single2sub_es []int // (sub_val <= pub_val) sub#s
32
+	single2sub_b  []int // (sub_val > pub_val) sub#s
33
+	single2sub_eb []int // (sub_val >= pub_val) sub#s
34
+	single2sub_e  []int // (sub_val == pub_val) sub#s
37 35
 
38 36
 	// range //
39
-	range2sub_s  []int // (val < x and ...) sub#
40
-	range2sub_es []int // (val <= x and ...) sub#
41
-	range2sub_b  []int // (val > x and ...) sub#
42
-	range2sub_eb []int // (val >= x and ...) sub#
37
+	range2sub_s  []int // (sub_val < pub_val) sub#s
38
+	range2sub_es []int // (sub_val <= pub_val) sub#s
39
+	range2sub_b  []int // (sub_val > pub_val) sub#s
40
+	range2sub_eb []int // (sub_val >= pub_val) sub#s
43 41
 }
44 42
 
45
-// ### To delete slice Array
43
+// To delete slice Array
46 44
 func remove(ary []int, i int) []int {
47 45
 	return append(ary[:i], ary[i+1:]...)
48 46
 }
49 47
 
48
+// To find a specific sub# in a value node
50 49
 func findSub(ary []int, sub int) int {
51 50
 	for i := 0; i < len(ary); i++ {
52 51
 		if ary[i] == sub {
@@ -56,6 +55,7 @@ func findSub(ary []int, sub int) int {
56 55
 	return -1
57 56
 }
58 57
 
58
+// To add a topic node to the topic list
59 59
 func (l *topicList) addTopicNode(topic []int64){
60 60
 	newNode := &topicNode{topic, nil, nil, valueList{}}
61 61
 	if l.head == nil{
@@ -69,6 +69,7 @@ func (l *topicList) addTopicNode(topic []int64){
69 69
 	l.size++
70 70
 }
71 71
 
72
+// To check if a specific value node is empty
72 73
 func (n *valueNode) isEmpty() bool {
73 74
 	empty := true
74 75
 	if len(n.single2sub_s) != 0 {
@@ -101,35 +102,36 @@ func (n *valueNode) isEmpty() bool {
101 102
 	return empty
102 103
 }
103 104
 
104
-// 노드의 operator리스트에 sub#을 in
105
-func (l *valueNode) insertSub(op string, sub int, issingle bool) {
105
+// To insert sub# into the operator list of the value node
106
+func (n *valueNode) insertSub(op string, sub int, issingle bool) {
106 107
 	if issingle == true {
107 108
 		switch op {
108 109
 		case "<":
109
-			l.single2sub_s = append(l.single2sub_s, sub)
110
+			n.single2sub_s = append(n.single2sub_s, sub)
110 111
 		case "<=":
111
-			l.single2sub_es = append(l.single2sub_es, sub)
112
+			n.single2sub_es = append(n.single2sub_es, sub)
112 113
 		case ">":
113
-			l.single2sub_b = append(l.single2sub_b, sub)
114
+			n.single2sub_b = append(n.single2sub_b, sub)
114 115
 		case ">=":
115
-			l.single2sub_eb = append(l.single2sub_eb, sub)
116
+			n.single2sub_eb = append(n.single2sub_eb, sub)
116 117
 		case "==":
117
-			l.single2sub_e = append(l.single2sub_e, sub)
118
+			n.single2sub_e = append(n.single2sub_e, sub)
118 119
 		}
119 120
 	} else {
120 121
 		switch op {
121 122
 		case "<":
122
-			l.range2sub_s = append(l.range2sub_s, sub)
123
+			n.range2sub_s = append(n.range2sub_s, sub)
123 124
 		case "<=":
124
-			l.range2sub_es = append(l.range2sub_es, sub)
125
+			n.range2sub_es = append(n.range2sub_es, sub)
125 126
 		case ">":
126
-			l.range2sub_b = append(l.range2sub_b, sub)
127
+			n.range2sub_b = append(n.range2sub_b, sub)
127 128
 		case ">=":
128
-			l.range2sub_eb = append(l.range2sub_eb, sub)
129
+			n.range2sub_eb = append(n.range2sub_eb, sub)
129 130
 		}
130 131
 	}
131 132
 }
132 133
 
134
+// To add a value node to the value list
133 135
 func (l *valueList) addValueNode(value []int64) {
134 136
 	newValNode := &valueNode{value, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil}
135 137
 	if l.head == nil{
@@ -143,6 +145,7 @@ func (l *valueList) addValueNode(value []int64) {
143 145
 	l.size++
144 146
 }
145 147
 
148
+// To get the position of a topic node with a specific topic
146 149
 func (l *topicList) getTopicNodePos(topic []int64) *topicNode {
147 150
 	topicPtr := l.head
148 151
 	for topicPtr != nil {
@@ -150,8 +153,7 @@ func (l *topicList) getTopicNodePos(topic []int64) *topicNode {
150 153
 			topicPtr = topicPtr.next
151 154
 			continue
152 155
 		}
153
-		// * compare 완성된다면 다시보기
154
-		if l.CompareTopic(topicPtr.topic, topic) == 0 {
156
+		if Compare(topicPtr.topic, topic) == 0 {
155 157
 			return topicPtr
156 158
 		}
157 159
 		topicPtr = topicPtr.next
@@ -159,59 +161,36 @@ func (l *topicList) getTopicNodePos(topic []int64) *topicNode {
159 161
 	return nil
160 162
 }
161 163
 
162
-func (l *topicList)CompareTopic(topic1 []int64, topic2 []int64) int {
163
-	// 길이 같은지 체크
164
-	if len(topic1) != len(topic2) {
165
-		return -1
166
-	}
167
-	for i := 0; i < len(topic2); i++ {
168
-		if topic1[i] != topic2[i] {
169
-			return -1
170
-		}
171
-	}
172
-	return 0
173
-}
174
-
175
-func (l *valueList) getValueNodePos(value []int64, isAlpha bool) *valueNode {
164
+// To get the position of a value node with a specific value
165
+func (l *valueList) getValueNodePos(value []int64) *valueNode {
176 166
 	valPtr := l.head
177 167
 	for valPtr != nil {
178 168
 		if len(valPtr.val) == 0{
179 169
 			valPtr = valPtr.next
180 170
 			continue
181 171
 		}
182
-		if isAlpha{
183
-			if CompareAlpha(value, valPtr.val) == 0{
184
-				return valPtr
185
-			}
186
-		} else{
187
-			if CompareDigit(value[0], valPtr.val[0]) == 0{
188
-				return valPtr
189
-			}
172
+		if Compare(value, valPtr.val) == 0 {
173
+			return valPtr
190 174
 		}
191 175
 		valPtr = valPtr.next
192 176
 	}
193 177
 	return nil
194 178
 }
195 179
 
196
-
197
-func CompareDigit(value1 int64, value2 int64) int {
198
-	if value1 < value2 {
180
+// Compare ->  To compare (topics, values) in a node
181
+func Compare(a []int64, b []int64) int {
182
+	if len(a) < len(b) {
199 183
 		return 1
200
-	} else if value1 > value2 {
184
+	} else if len(a) > len(b){
201 185
 		return -1
202
-	}
203
-	return 0
204
-}
205
-
206
-func CompareAlpha(value1 []int64, value2 []int64) int {
207
-	// 길이 같은지 체크
208
-	if len(value1) != len(value2) {
209
-		return -1
210
-	}
211
-	for i := 0; i < len(value2); i++ {
212
-		if value1[i] != value2[i] {
213
-			return -1
186
+	} else{
187
+		for i := 0; i < len(a); i++{
188
+			if a[i] < b[i] {
189
+				return 1
190
+			} else if a[i] > b[i]{
191
+				return -1
192
+			}
214 193
 		}
194
+		return 0
215 195
 	}
216
-	return 0
217
-}
196
+}

+ 152
- 20
src/broker/modules/matching.go Parādīt failu

@@ -1,21 +1,153 @@
1 1
 package modules
2
-//
3
-//import (
4
-//	_ "errors"
5
-//	_ "fmt"
6
-//)
7
-//
8
-//type match_manager struct {
9
-//}
10
-//
11
-//func (match_mng *match_manager) matching(queue *MsgQueue) error {
12
-//	msg := queue.pop(true)
13
-//
14
-//	//Implement here ~~
15
-//	subscription := msg.(*PublishMsg).subscription
16
-//	content := msg.(*PublishMsg).content
17
-//
18
-//	println(subscription, content)
19
-//
20
-//	return nil
21
-//}
2
+
3
+import (
4
+	"errors"
5
+	_ "errors"
6
+	"github.com/juliangruber/go-intersect"
7
+)
8
+
9
+type match_manager struct {
10
+	match_count	int
11
+}
12
+
13
+// Returns a list of IP addresses of matched subs
14
+func (match_mng *match_manager) Matching(queue *MsgQueue, sub_mng *sub_manager) ([]string, MsgUnit, error) {
15
+	msg := queue.pop(true)
16
+	topic := msg.(*PublishMsg).topic
17
+	value := msg.(*PublishMsg).value
18
+
19
+	// list
20
+	ret := make([]string, 0)
21
+
22
+	// list for matched range subscriptions
23
+	big := make([]int, 0)
24
+	small := make([]int, 0)
25
+
26
+	// 1. Find (topicNode[topic] == msg.topic) Node
27
+	topicPtr := sub_mng.list
28
+	pos := topicPtr.getTopicNodePos(topic)
29
+
30
+	// Don't Exist topicNode
31
+	if pos == nil{
32
+		return nil,nil,errors.New("Don't Exist topicNode")
33
+	}
34
+
35
+	// 2. Traverse all valueNode -> and Match
36
+	valPtr := pos.list.head
37
+	for valPtr != nil{
38
+		compare := Compare(valPtr.val, value)
39
+		if compare < 0 { // sub.val > pub.val
40
+		// single : { >, >= }
41
+
42
+			// (1) case : >
43
+			for i := 0; i < len(valPtr.single2sub_b); i++{
44
+				sub := valPtr.single2sub_b[i]
45
+				ip := sub_mng.sub2ip[sub]
46
+				ret = append(ret, ip)
47
+			}
48
+
49
+			// (2) case : >=
50
+			for i := 0; i < len(valPtr.single2sub_eb); i++{
51
+				sub := valPtr.single2sub_eb[i]
52
+				ip := sub_mng.sub2ip[sub]
53
+				ret = append(ret, ip)
54
+			}
55
+		// range : { >, >= }
56
+
57
+			// (1) case : >
58
+			for i := 0; i < len(valPtr.range2sub_b); i++{
59
+				sub := valPtr.range2sub_b[i]
60
+				big = append(big, sub)
61
+			}
62
+
63
+			// (2) case : >=
64
+			for i := 0; i < len(valPtr.range2sub_eb); i++{
65
+				sub := valPtr.range2sub_eb[i]
66
+				big = append(big, sub)
67
+			}
68
+
69
+		} else if compare > 0 { // sub.val < pub.val
70
+
71
+		// single : { <, <= }
72
+
73
+			// (1) case : <
74
+			for i := 0; i < len(valPtr.single2sub_s); i++{
75
+				sub := valPtr.single2sub_s[i]
76
+				ip := sub_mng.sub2ip[sub]
77
+				ret = append(ret, ip)
78
+			}
79
+
80
+			// (2) case : <=
81
+			for i := 0; i < len(valPtr.single2sub_es); i++{
82
+				sub := valPtr.single2sub_es[i]
83
+				ip := sub_mng.sub2ip[sub]
84
+				ret = append(ret, ip)
85
+			}
86
+
87
+		// range : { <, <= }
88
+
89
+			// (1) case : <
90
+			for i := 0; i < len(valPtr.range2sub_s); i++{
91
+				sub := valPtr.range2sub_s[i]
92
+				small = append(small, sub)
93
+			}
94
+			// (2) case : <=
95
+			for i := 0; i < len(valPtr.range2sub_es); i++{
96
+				sub := valPtr.range2sub_es[i]
97
+				small = append(small, sub)
98
+			}
99
+
100
+		} else{ // sub.val == pub.val
101
+
102
+		// single : { <=, >=, ==}
103
+
104
+			// (1) case : <=
105
+			for i := 0; i < len(valPtr.single2sub_es); i++{
106
+				sub := valPtr.single2sub_es[i]
107
+				ip := sub_mng.sub2ip[sub]
108
+				ret = append(ret, ip)
109
+			}
110
+
111
+			// (2) case : >=
112
+			for i := 0; i < len(valPtr.single2sub_eb); i++{
113
+				sub := valPtr.single2sub_eb[i]
114
+				ip := sub_mng.sub2ip[sub]
115
+				ret = append(ret, ip)
116
+			}
117
+
118
+			// (3) case : ==
119
+			for i := 0; i < len(valPtr.single2sub_e); i++{
120
+				sub := valPtr.single2sub_e[i]
121
+				ip := sub_mng.sub2ip[sub]
122
+				ret = append(ret, ip)
123
+			}
124
+
125
+		// range : { <=, >= }
126
+
127
+			// (1) case : <=
128
+			for i := 0; i < len(valPtr.range2sub_es); i++{
129
+				sub := valPtr.range2sub_es[i]
130
+				small = append(small, sub)
131
+			}
132
+
133
+			// (2) case : >=
134
+			for i := 0; i < len(valPtr.range2sub_eb); i++{
135
+				sub := valPtr.range2sub_eb[i]
136
+				big = append(big, sub)
137
+			}
138
+
139
+		}
140
+	}
141
+
142
+	// Add the intersection IP address of two sets (large and small) to the return list
143
+	hash := intersect.Hash(small, big)
144
+	for i := 0; i < len(hash); i++{
145
+		sub := hash[i].(int)
146
+		ip := sub_mng.sub2ip[sub]
147
+		ret = append(ret, ip)
148
+	}
149
+
150
+	match_mng.match_count++
151
+
152
+	return ret, msg, nil
153
+}

+ 43
- 45
src/broker/modules/subscription.go Parādīt failu

@@ -2,34 +2,32 @@ package modules
2 2
 
3 3
 import (
4 4
 	"errors"
5
-	"fmt"
6 5
 )
7 6
 
8 7
 type sub_manager struct {
9
-	// NameList의 노드들이 각각의 list를 갖음
10 8
 	list topicList
11 9
 
12
-	/* sub# 관리 */
10
+	/* Manage sub# */
13 11
 	count_sub 	 int               // Subscription #
14 12
 	emptylist  []int                // To administrate sub #
15
-	ip2sub     map[string][]int     // ip2sub[ip]= sub#...
16
-	sub2node   map[int][]*valueNode // sub2node[sub#] = node_addr
17
-	israngesub map[int]bool         // delete시에 관리를 위해
13
+	ip2sub     map[string][]int     // ip2sub[ip] = sub# ...
14
+	sub2ip	   map[int]string		// sub2ip[sub#] = ip
15
+	sub2node   map[int][]*valueNode // sub2node[sub#] = node_addr ...
16
+	israngesub map[int]bool         // To manage when deleted
18 17
 }
19 18
 
20
-// 초기화 시에 하고 싶은 처리
21
-func (self *sub_manager) Initialize() {
19
+func (manager *sub_manager) Initialize() {
22 20
 	// Some initialize
23
-	self.ip2sub = make(map[string][]int)
24
-	self.sub2node = make(map[int][]*valueNode)
25
-	self.israngesub = make(map[int]bool)
21
+	manager.ip2sub = make(map[string][]int)
22
+	manager.sub2ip = make(map[int]string)
23
+	manager.sub2node = make(map[int][]*valueNode)
24
+	manager.israngesub = make(map[int]bool)
26 25
 }
27 26
 
28
-// 생성자 함수를 정의
29 27
 func newSubmng() *sub_manager {
30
-	sub_mng := &sub_manager{}
31
-	sub_mng.Initialize()
32
-	return sub_mng
28
+	subMng := &sub_manager{}
29
+	subMng.Initialize()
30
+	return subMng
33 31
 }
34 32
 
35 33
 //	### To Insert sub#
@@ -38,12 +36,10 @@ func (manager *sub_manager) addSubscription(msg MsgUnit) error {
38 36
 	topic := msg.(*SubscriptionMsg).topic
39 37
 	value := msg.(*SubscriptionMsg).value
40 38
 	operator := msg.(*SubscriptionMsg).operator
41
-	isAlpha := msg.(*SubscriptionMsg).isAlpha
42 39
 	subnumber := 0
43 40
 
44
-	//fmt.Println("message = ", msg)
41
+	// * 1. Mapping incoming IP address to sub #
45 42
 
46
-	// * 1. 들어온 ipaddr -> sub# 매핑
47 43
 	if len(manager.emptylist) == 0 {
48 44
 		manager.ip2sub[msg.(*SubscriptionMsg).from] = append(manager.ip2sub[msg.(*SubscriptionMsg).from], manager.count_sub)
49 45
 		subnumber = manager.count_sub
@@ -55,14 +51,14 @@ func (manager *sub_manager) addSubscription(msg MsgUnit) error {
55 51
 		subnumber = subidx
56 52
 	}
57 53
 
58
-	// * 2. Sub 추가
59 54
 	nameptr := manager.list.head
60 55
 	findOk := false
61 56
 
62
-	//namelist에서 name찾고, 없으면 추가
57
+	// * 2. Add Subscription
58
+
59
+	// Find name in namelist, add if not found
63 60
 	for nameptr != nil {
64
-		// *  compare함수 구현되기 전 임시로 끼워넣은 것 (이 부분은 secure.go 완성되면 다시 확인)
65
-		if nameptr.topic[0] == topic[0] {
61
+		if Compare(nameptr.topic, topic) == 0 {
66 62
 			findOk = true
67 63
 			break
68 64
 		}
@@ -74,10 +70,9 @@ func (manager *sub_manager) addSubscription(msg MsgUnit) error {
74 70
 		nameptr = manager.list.tail
75 71
 	}
76 72
 
77
-	// list[name]에 value추가
78
-	if len(operator) == 1 { // 단일 식이라면 (single)
79
-		valptr := nameptr.list.getValueNodePos(value, isAlpha)
80
-		fmt.Println("pos = " ,valptr.val)
73
+	// Add value to list[name]
74
+	if len(operator) == 1 { // if single expression
75
+		valptr := nameptr.list.getValueNodePos(value)
81 76
 		if valptr == nil {
82 77
 			nameptr.list.addValueNode(value)
83 78
 			valptr = nameptr.list.tail
@@ -86,46 +81,47 @@ func (manager *sub_manager) addSubscription(msg MsgUnit) error {
86 81
 		manager.sub2node[subnumber] = append(manager.sub2node[subnumber], valptr)
87 82
 		valptr.insertSub(operator[0], subnumber, true)
88 83
 
89
-		return nil // addSubscription ok
84
+		return nil // AddSubscription ok
90 85
 	} else {
91 86
 
92
-		// '&&' , '||'로 묶여 있는 복합식에 대하여
87
+		// For compound expressions bounded by '&&' and '||'
88
+		// (ex) { (234 < x) && (x <= 1293) } , { (234 < x) || ( x < 1293) }
93 89
 		logical_operator := operator[1]
94 90
 
95
-		// find(namelist[name].list.val == value)인 노드
96
-		// * 이 부분 다시 봐야함..
97
-		valptr1 := nameptr.list.getValueNodePos(value, isAlpha)
98
-		valptr2 := nameptr.list.getValueNodePos(value, isAlpha)
91
+		// Find ValueNode = (namelist[name].list.val == value)
92
+		valptr1 := nameptr.list.getValueNodePos([]int64{value[0]})
93
+		valptr2 := nameptr.list.getValueNodePos([]int64{value[1]})
99 94
 
100 95
 		if valptr1 == nil {
101
-			nameptr.list.addValueNode(value)
96
+			nameptr.list.addValueNode([]int64{value[0]})
102 97
 			valptr1 = nameptr.list.tail
103 98
 		}
104 99
 		if valptr2 == nil {
105
-			nameptr.list.addValueNode(value)
100
+			nameptr.list.addValueNode([]int64{value[1]})
106 101
 			valptr2 = nameptr.list.tail
107 102
 		}
108 103
 
109 104
 		manager.sub2node[subnumber] = append(manager.sub2node[subnumber], valptr1)
110 105
 		manager.sub2node[subnumber] = append(manager.sub2node[subnumber], valptr2)
111 106
 
112
-		if logical_operator == "&&" { // '&&'로 묶여있다면
113
-			// rangesub check
107
+		if logical_operator == "&&" {
108
+			// If they are enclosed in '&&' -> Insert value to range_operator_list
114 109
 			manager.israngesub[subnumber] = true
115 110
 			valptr1.insertSub(operator[0], subnumber, false)
116 111
 			valptr2.insertSub(operator[2], subnumber, false)
117 112
 
118 113
 		} else {
119
-			// '||'로 묶여 있다면 두 식 모두 single로 처리가능
114
+			// if they are enclosed in '||' -> Insert value to single_operator_list
120 115
 			valptr1.insertSub(operator[0], subnumber, true)
121 116
 			valptr2.insertSub(operator[2], subnumber, true)
122 117
 		}
118
+
123 119
 		return nil // addSubscription ok
124 120
 	}
125 121
 	return errors.New("Can't addSubscription")
126 122
 }
127 123
 
128
-//	* To delete subscription
124
+// To delete subscription
129 125
 func (manager *sub_manager) delete(from string) error {
130 126
 
131 127
 	ip := from
@@ -162,8 +158,9 @@ func (manager *sub_manager) delete(from string) error {
162 158
 					manager.emptylist = append(manager.emptylist, sub)
163 159
 				}
164 160
 
165
-				// node가 비어있으면 delete
166 161
 				isempty := node[j].isEmpty()
162
+
163
+				// Delete if Value Node is empty
167 164
 				if isempty && node[j] != nil {
168 165
 					prev_node := node[j].prev
169 166
 					next_node := node[j].next
@@ -205,17 +202,18 @@ func (manager *sub_manager) delete(from string) error {
205 202
 				}
206 203
 
207 204
 				isempty := node[j].isEmpty()
208
-				// node가 비어있으면 delete
205
+
206
+				// Delete if Value Node is empty
209 207
 				if isempty && node[j] != nil {
210
-					prev_node := node[j].prev
211
-					next_node := node[j].next
208
+					prevNode := node[j].prev
209
+					nextNode := node[j].next
212 210
 
213
-					prev_node.next = node[j].next
214
-					next_node.prev = node[j].prev
211
+					prevNode.next = node[j].next
212
+					nextNode.prev = node[j].prev
215 213
 				}
216 214
 			}
217 215
 		}
218 216
 	}
219
-	manager.ip2sub[ip]=nil
217
+	manager.ip2sub[ip]=nil // Delete sub#s mapped to Ip address
220 218
 	return nil
221 219
 }

+ 289
- 144
src/broker/modules/subscription_test.go Parādīt failu

@@ -1,145 +1,290 @@
1 1
 package modules
2
-//
3
-//import (
4
-//	"fmt"
5
-//	_ "fmt"
6
-//	"math/rand"
7
-//	"strconv"
8
-//	"testing"
9
-//	"time"
10
-//	"github.com/stretchr/testify/assert"
11
-//)
12
-//
13
-//func dataMake(isalpha bool) MsgUnit{
14
-//	rand.Seed(time.Now().UnixNano())
15
-//
16
-//	// Set Ipaddr
17
-//	msg := Message{"", "1.0", "", SM}
18
-//	for i := 0; i < 4; i++{
19
-//		itoa := strconv.Itoa(rand.Int() % 256)
20
-//
21
-//		msg.from += itoa
22
-//		if i != 3{
23
-//			msg.from += "."
24
-//		}
25
-//	}
26
-//
27
-//	// Set Time
28
-//	msg.time += strconv.Itoa(rand.Int()%24) + ":"
29
-//	msg.time += strconv.Itoa(rand.Int()%60)
30
-//
31
-//	// Set Topic, Value, operator
32
-//	topic := []int64{}
33
-//	value := []int64{}
34
-//	operator := []string{}
35
-//	candOp := []string{">", ">=", "<=" ,"<", "=="}
36
-//	logicalOp := []string{"&&", "||"}
37
-//
38
-//	topic = append(topic, rand.Int63())
39
-//
40
-//	if isalpha{
41
-//		value = append(value, rand.Int63())
42
-//		operator = append(operator, "==")
43
-//	} else{
44
-//		len := rand.Int() % 2 + 1
45
-//		value = append(value, rand.Int63())
46
-//
47
-//		if len == 1{
48
-//			operator = append(operator, candOp[rand.Int()%5])
49
-//		} else{
50
-//			value = append(value, rand.Int63())
51
-//			op := rand.Int()%2
52
-//			operator = append(operator, candOp[rand.Int()%2])
53
-//			if op == 0 {
54
-//				operator = append(operator, logicalOp[0])
55
-//			} else{
56
-//				operator = append(operator, logicalOp[1])
57
-//			}
58
-//			operator = append(operator, candOp[rand.Int()%2 + 2])
59
-//		}
60
-//	}
61
-//
62
-//	return &SubscriptionMsg{msg, topic, value, operator, isalpha}
63
-//}
64
-//
65
-//func Test_addSubscription(t *testing.T) {
66
-//	rand.Seed(time.Now().UnixNano())
67
-//
68
-//	// To Init sub_mng
69
-//	mos := Moscato{sub_mng : sub_manager{}}
70
-//
71
-//	// Make Data set(Subscription)
72
-//	msgList := []MsgUnit{}
73
-//	dataLen:= 3
74
-//	for i := 0 ; i < dataLen ; i++{
75
-//		seed := rand.Int()%2
76
-//		if seed == 0{
77
-//			msgList = append(msgList, dataMake(false))
78
-//		} else{
79
-//			msgList = append(msgList, dataMake(true))
80
-//		}
81
-//	}
82
-//
83
-//	// Watch Data set
84
-//	for i := 0; i < dataLen ; i++{
85
-//		msg := msgList[i]
86
-//		fmt.Println(
87
-//			" from = ", msg.(*SubscriptionMsg).Message.from,
88
-//			" time = ", msg.(*SubscriptionMsg).Message.time,
89
-//			" topic = ", msg.(*SubscriptionMsg).topic,
90
-//			" value = ", msg.(*SubscriptionMsg).value,
91
-//			" operator = ", msg.(*SubscriptionMsg).operator,
92
-//			" isAlpha ?= ", msg.(*SubscriptionMsg).isAlpha)
93
-//	}
94
-//
95
-//	// Test addSubScription
96
-//	for i := 0; i < dataLen ; i++{
97
-//
98
-//		msg := msgList[i]
99
-//		ip := msg.(*SubscriptionMsg).Message.from
100
-//		topic := msg.(*SubscriptionMsg).topic
101
-//		value := msg.(*SubscriptionMsg).value
102
-//		//operator := msg.(*SubscriptionMsg).operator
103
-//		//isAlpha := msg.(*SubscriptionMsg).isAlpha
104
-//		subnumber := mos.sub_mng.count_sub
105
-//
106
-//		mos.sub_mng.addSubscription(msg)
107
-//
108
-//
109
-//		// 1. Check if ip mapping is correct
110
-//		assert.Equal(t, subnumber, mos.sub_mng.ip2sub[ip][len(mos.sub_mng.ip2sub[ip]) - 1], "Ip mapping is failed")
111
-//
112
-//		// 2. Check topicNode
113
-//		nameptr := mos.sub_mng.list.head
114
-//		for nameptr != nil{
115
-//			if nameptr.topic[0] == topic[0]{
116
-//				break
117
-//			}
118
-//			nameptr = nameptr.next
119
-//		}
120
-//
121
-//		for i := 0 ; i < len(topic); i++{
122
-//			assert.Equal(t, topic[i], nameptr.topic[i], "topicNode Add is failed")
123
-//		}
124
-//
125
-//		// 3. Check Value in ValueNode
126
-//		valptr := nameptr.list.head
127
-//		for valptr != nil{
128
-//			for i:=0 ; i < len(value); i++{
129
-//				find := true
130
-//				if value[i] != valptr.val[i]{
131
-//					find = false
132
-//				}
133
-//				if find{
134
-//					break
135
-//				}
136
-//			}
137
-//			valptr = valptr.next
138
-//		}
139
-//		fmt.Println("my value = ", valptr.val, " real value = ", value)
140
-//
141
-//		for i := 0; i < len(value); i++{
142
-//			assert.Equal(t, value[i], valptr.val[i], "ValNode Add is failed")
143
-//		}
144
-//	}
145
-//}
2
+
3
+import (
4
+	"fmt"
5
+	_ "fmt"
6
+	"github.com/stretchr/testify/assert"
7
+	"math/rand"
8
+	"strconv"
9
+	"testing"
10
+	"time"
11
+)
12
+
13
+func makeData(isAlpha bool) MsgUnit{
14
+	rand.Seed(time.Now().UnixNano())
15
+	// Set Ipaddr
16
+	msg := Message{"", "1.0", "", SM}
17
+	for i := 0; i < 4; i++{
18
+		itoa := strconv.Itoa(rand.Int() % 256)
19
+		msg.from += itoa
20
+		if i != 3{
21
+			msg.from += "."
22
+		}
23
+	}
24
+
25
+	// Set Time
26
+	msg.time += strconv.Itoa(rand.Int()%24) + ":"
27
+	msg.time += strconv.Itoa(rand.Int()%60)
28
+
29
+	// Set Topic, Value, operator
30
+	topic := []int64{}
31
+	value := []int64{}
32
+	operator := []string{}
33
+	candOp := []string{">", ">=", "<=" ,"<", "=="}
34
+	logicalOp := []string{"&&", "||"}
35
+
36
+	if isAlpha {
37
+		topicLen := rand.Int() % 10 + 1
38
+		for i := 0 ; i < topicLen; i++{
39
+			topic = append(topic, rand.Int63())
40
+		}
41
+
42
+		valueLen := rand.Int() % 10 + 1
43
+		for i := 0 ; i < valueLen; i++{
44
+			value = append(value, rand.Int63())
45
+		}
46
+
47
+		operator = append(operator, "==")
48
+
49
+	} else{
50
+		valueLen := rand.Int() % 2 + 1
51
+
52
+		topic = append(topic, rand.Int63())
53
+		value = append(value, rand.Int63())
54
+
55
+		if valueLen == 1{
56
+			operator = append(operator, candOp[rand.Int()%5])
57
+		} else{
58
+			value = append(value, rand.Int63())
59
+			op := rand.Int()%2
60
+			operator = append(operator, candOp[rand.Int()%2])
61
+			if op == 0 {
62
+				operator = append(operator, logicalOp[0])
63
+			} else{
64
+				operator = append(operator, logicalOp[1])
65
+			}
66
+			operator = append(operator, candOp[rand.Int()%2 + 2])
67
+		}
68
+	}
69
+
70
+	return &SubscriptionMsg{msg, topic, value, operator, isAlpha}
71
+}
72
+
73
+func makeMsgList(dataLen int, isAlpha bool) []MsgUnit{
74
+	rand.Seed(time.Now().UnixNano())
75
+	var ret []MsgUnit
76
+	for i := 0 ; i < dataLen ; i++{
77
+		ret = append(ret, makeData(isAlpha))
78
+	}
79
+	return ret
80
+}
81
+
82
+func checkOperatorList(isSingle bool, sub int, operator string, l *valueNode) int{
83
+	ret := -1
84
+	if isSingle{
85
+		switch operator {
86
+		case "<":
87
+			ret = findSub(l.single2sub_s, sub)
88
+		case "<=":
89
+			ret = findSub(l.single2sub_es, sub)
90
+		case ">":
91
+			ret = findSub(l.single2sub_b, sub)
92
+		case ">=":
93
+			ret = findSub(l.single2sub_eb, sub)
94
+		case "==":
95
+			ret = findSub(l.single2sub_e, sub)
96
+		}
97
+
98
+	} else{
99
+		switch operator {
100
+		case "<":
101
+			ret = findSub(l.range2sub_s, sub)
102
+		case "<=":
103
+			ret = findSub(l.range2sub_es, sub)
104
+		case ">":
105
+			ret = findSub(l.range2sub_b, sub)
106
+		case ">=":
107
+			ret = findSub(l.range2sub_eb, sub)
108
+		}
109
+	}
110
+	return ret
111
+}
112
+
113
+func watchData(msgList []MsgUnit, dataLen int){
114
+	for i := 0; i < dataLen; i++{
115
+		msg := msgList[i]
116
+		fmt.Println(
117
+			"\nfrom = ", msg.(*SubscriptionMsg).Message.from,
118
+			"\ntime = ", msg.(*SubscriptionMsg).Message.time,
119
+			"\ntopic = ", msg.(*SubscriptionMsg).topic,
120
+			"\nvalue = ", msg.(*SubscriptionMsg).value,
121
+			"\noperator = ", msg.(*SubscriptionMsg).operator,
122
+			"\nisAlpha ?= ", msg.(*SubscriptionMsg).isAlpha,
123
+		)
124
+	}
125
+}
126
+
127
+//Test addSubScription(1) (dif all [topic, value, operator])
128
+func Test_addSubscription_allDif(t *testing.T) {
129
+	rand.Seed(time.Now().UnixNano())
130
+
131
+	// To Init sub_mng
132
+	s := sub_manager{
133
+		ip2sub:     make(map[string][]int),
134
+		sub2node:   make(map[int][]*valueNode),
135
+		israngesub: make(map[int]bool),
136
+	}
137
+	mos := Moscato{sub_mng: s}
138
+
139
+	// Make Data set(Subscription)
140
+	var msgList []MsgUnit
141
+	dataLen := 100
142
+	msgList = makeMsgList(dataLen, false)
143
+
144
+	//Watch Data set
145
+	//watchData(msgList, dataLen)
146
+
147
+	for i := 0; i < dataLen; i++ {
148
+		msg := msgList[i]
149
+		ip := msg.(*SubscriptionMsg).Message.from
150
+		topic := msg.(*SubscriptionMsg).topic
151
+		value := msg.(*SubscriptionMsg).value
152
+		operator := msg.(*SubscriptionMsg).operator
153
+		subnumber := mos.sub_mng.count_sub
154
+		isSingle := true
155
+
156
+		// 0. Check addSubscription
157
+		err := mos.sub_mng.addSubscription(msg)
158
+		assert.Equal(t, nil, err)
159
+
160
+		// 1. Check if ip mapping is correct
161
+		assert.Equal(t, subnumber, mos.sub_mng.ip2sub[ip][len(mos.sub_mng.ip2sub[ip])-1], "Ip mapping is failed")
162
+
163
+		// 2. Check topicNode
164
+		topicPtr := mos.sub_mng.list.head
165
+		for topicPtr != nil {
166
+			if Compare(topic, topicPtr.topic) == 0 {
167
+				break
168
+			}
169
+			topicPtr = topicPtr.next
170
+		}
171
+
172
+		assert.Equal(t, topic, topicPtr.topic, "topicNode Add is failed")
173
+
174
+		// Check isSingle ?
175
+		if len(operator) == 3 && operator[1] == "&&" {
176
+			isSingle = false
177
+		}
178
+
179
+		// 3. Check Value in ValueNode & Check Operator in ValueNode
180
+		if !isSingle || (len(operator) == 3 && operator[1] == "||") {
181
+			valptr1 := topicPtr.list.getValueNodePos([]int64{value[0]})
182
+			valptr2 := topicPtr.list.getValueNodePos([]int64{value[1]})
183
+
184
+			assert.Equal(t, []int64{value[0]}, valptr1.val)
185
+			assert.Equal(t, []int64{value[1]}, valptr2.val)
186
+
187
+			assert.NotEqual(t, -1, checkOperatorList(isSingle, subnumber, operator[0], valptr1))
188
+			assert.NotEqual(t, -1, checkOperatorList(isSingle, subnumber, operator[2], valptr2))
189
+
190
+		} else {
191
+			valptr := topicPtr.list.getValueNodePos(value)
192
+			assert.Equal(t, value, valptr.val)
193
+			assert.NotEqual(t, -1, checkOperatorList(isSingle, subnumber, operator[0], valptr))
194
+		}
195
+	}
196
+}
197
+
198
+// Test addSubScription(2) (same [topic, value] dif [operator])
199
+func Test_addSubscription_same_topicNvalue(t *testing.T) {
200
+	rand.Seed(time.Now().UnixNano())
201
+
202
+	// To Init sub_mng
203
+	s := sub_manager{
204
+		ip2sub:     make(map[string][]int),
205
+		sub2node:   make(map[int][]*valueNode),
206
+		israngesub: make(map[int]bool),
207
+	}
208
+
209
+	mos := Moscato{sub_mng: s}
210
+
211
+	// Fix Topic & Value
212
+	topicLen := rand.Int()%10 + 1
213
+	staticTopic := make([]int64, topicLen)
214
+	staticValue := []int64{rand.Int63()}
215
+
216
+
217
+	// Make Data
218
+	var msgList []MsgUnit
219
+	dataLen := 100
220
+	msgList = makeMsgList(dataLen, false)
221
+
222
+	for i := 0; i < topicLen; i++ {
223
+		staticTopic[i] = rand.Int63()
224
+	}
225
+
226
+	// Fix Same Topic & Value
227
+	for i := 0; i < dataLen; i++ {
228
+		operator := msgList[i].(*SubscriptionMsg).operator
229
+		msgList[i].(*SubscriptionMsg).topic = staticTopic
230
+
231
+		if len(operator) == 1{
232
+			msgList[i].(*SubscriptionMsg).value = staticValue
233
+		}
234
+	}
235
+
236
+	// Watch Data set
237
+	// watchData(msgList, dataLen)
238
+
239
+	for i := 0; i < dataLen; i++ {
240
+		msg := msgList[i]
241
+		err := mos.sub_mng.addSubscription(msg)
242
+		assert.Equal(t, nil, err)
243
+	}
244
+
245
+	topicPtr := mos.sub_mng.list.head
246
+	for topicPtr != nil {
247
+		if Compare(topicPtr.topic, staticTopic) == 0 {
248
+			break
249
+		}
250
+		topicPtr = topicPtr.next
251
+	}
252
+	watchValueNode(topicPtr.list.head)
253
+
254
+}
255
+
256
+func watchValueNode(ptr *valueNode) {
257
+	valPtr := ptr
258
+	for valPtr != nil{
259
+		fmt.Println("Value = ", valPtr.val)
260
+		if len(valPtr.single2sub_s) != 0 {
261
+			fmt.Println("Single2sub (<) List = ", valPtr.single2sub_s)
262
+		}
263
+		if len(valPtr.single2sub_es) != 0 {
264
+			fmt.Println("Single2sub (<=) List = ", valPtr.single2sub_es)
265
+		}
266
+		if len(valPtr.single2sub_b) != 0 {
267
+			fmt.Println("Single2sub (>) List = ", valPtr.single2sub_b)
268
+		}
269
+		if len(valPtr.single2sub_eb) != 0 {
270
+			fmt.Println("Single2sub (>=) List = ", valPtr.single2sub_eb)
271
+		}
272
+		if len(valPtr.single2sub_e) != 0 {
273
+			fmt.Println("Single2sub (==) List = ", valPtr.single2sub_e)
274
+		}
275
+
276
+		if len(valPtr.range2sub_s) != 0 {
277
+			fmt.Println("range2sub (<) List = ", valPtr.range2sub_s)
278
+		}
279
+		if len(valPtr.range2sub_es) != 0 {
280
+			fmt.Println("range2sub (<=) List = ", valPtr.range2sub_es)
281
+		}
282
+		if len(valPtr.range2sub_b) != 0 {
283
+			fmt.Println("range2sub (>) List = ", valPtr.range2sub_b)
284
+		}
285
+		if len(valPtr.range2sub_eb) != 0 {
286
+			fmt.Println("range2sub (>=) List = ", valPtr.range2sub_eb)
287
+		}
288
+		valPtr = valPtr.next
289
+	}
290
+}

Notiek ielāde…
Atcelt
Saglabāt