From 637b5145c12ac150a00026e4929cace1ec3e593d Mon Sep 17 00:00:00 2001
From: chenshijun <csj_sky@126.com>
Date: 星期四, 12 三月 2020 19:44:49 +0800
Subject: [PATCH] 闭包初步测试通过

---
 shmqueue.go |   58 ++++++++++++++++++++++++++++++++++++----------------------
 1 files changed, 36 insertions(+), 22 deletions(-)

diff --git a/shmqueue.go b/shmqueue.go
index c819298..f926652 100644
--- a/shmqueue.go
+++ b/shmqueue.go
@@ -17,7 +17,8 @@
 
 //Element info
 type ElemInfo struct {
-	ShmId int
+	PicId  int
+	InfoId int
 }
 
 //queue info
@@ -38,10 +39,10 @@
 
 type EsQueueInfo struct {
 	EsCaches []esCache
-	ShmKey int
-	ShmID int
-	ShmData []byte
-	Queue *esQueue
+	ShmKey   int
+	ShmID    int
+	ShmData  []byte
+	Queue    *esQueue
 }
 
 // ptr2esCache convert unsafe.Pointer to []esCache
@@ -87,23 +88,23 @@
 	q.putPos = 0
 	q.getPos = 0
 
-	fmt.Println("int(q.capacity)",int(q.capacity))
+	fmt.Println("int(q.capacity)", int(q.capacity))
 	eqi.EsCaches = ptr2esCache(unsafe.Pointer(&q.cache), int(q.capacity))
 	fmt.Printf("EsCaches:%p\n", &eqi.EsCaches)
 	fmt.Printf("EsCaches[0]:%p\n", &(eqi.EsCaches[0]))
 	fmt.Printf("EsCaches[1]:%p\n", &(eqi.EsCaches[1]))
 
-	fmt.Println("int(q.capacity)",int(q.capacity))
+	fmt.Println("int(q.capacity)", int(q.capacity))
 	for i := 0; i < int(q.capacity); i++ {
 		fmt.Println("i", i)
-		fmt.Printf("EsCaches[%d]:%p\n", i,  &(eqi.EsCaches[i]))
-		fmt.Printf("EsCaches[%d].getNo:%p\n", i,  &(eqi.EsCaches[i].getNo))
-		fmt.Printf("EsCaches[%d].putNo:%p\n", i,  &(eqi.EsCaches[i].putNo))
+		fmt.Printf("EsCaches[%d]:%p\n", i, &(eqi.EsCaches[i]))
+		fmt.Printf("EsCaches[%d].getNo:%p\n", i, &(eqi.EsCaches[i].getNo))
+		fmt.Printf("EsCaches[%d].putNo:%p\n", i, &(eqi.EsCaches[i].putNo))
 
 		eqi.EsCaches[i].getNo = uint32(i)
 		eqi.EsCaches[i].putNo = uint32(i)
-		fmt.Printf("cache.putNo:%p\n",  &(eqi.EsCaches[i].putNo))
-		fmt.Printf("cache.putNo:%p\n",  &(eqi.EsCaches[i].putNo))
+		fmt.Printf("cache.putNo:%p\n", &(eqi.EsCaches[i].putNo))
+		fmt.Printf("cache.putNo:%p\n", &(eqi.EsCaches[i].putNo))
 		fmt.Println("cache:", eqi.EsCaches[i])
 	}
 	cache := &eqi.EsCaches[0]
@@ -112,13 +113,13 @@
 	fmt.Println("cache:", cache)
 
 	cache0 := eqi.EsCaches[0]
-	fmt.Printf("cache0:%p\n",   &(cache0))
+	fmt.Printf("cache0:%p\n", &(cache0))
 	fmt.Printf("cache0.getNo:%p\n", &(cache0.getNo))
 	fmt.Printf("cache0.putNo:%p\n", &(cache0.putNo))
 	fmt.Println("cache0:", cache0)
 
 	cache1 := eqi.EsCaches[1]
-	fmt.Printf("cache1:%p\n",   &(cache1))
+	fmt.Printf("cache1:%p\n", &(cache1))
 	fmt.Printf("cache1.getNo:%p\n", &(cache1.getNo))
 	fmt.Printf("cache1.putNo:%p\n", &(cache1.putNo))
 	fmt.Println("cache1:", cache1)
@@ -129,7 +130,7 @@
 	eqi.ShmID = shmid
 	eqi.ShmKey = key
 	eqi.Queue = q
-	time.Sleep(10*time.Second)
+	time.Sleep(10 * time.Second)
 
 	return &eqi
 }
@@ -143,7 +144,7 @@
 	shmdata := bytes2shmEsQueue(data)
 	fmt.Println("shmdata:", shmdata)
 	eqi.EsCaches = ptr2esCache(unsafe.Pointer(&shmdata.cache), int(shmdata.capacity))
-	//fmt.Println("EsCaches:", eqi.EsCaches)
+	fmt.Println("EsCaches:", eqi.EsCaches)
 
 	eqi.Queue = shmdata
 	eqi.ShmKey = key
@@ -155,12 +156,13 @@
 
 // ReleaseQueue detach an exist shm queue
 func (eqi *EsQueueInfo) ReleaseQueue() {
-	fmt.Println("ReleaseQueue: key", eqi.ShmKey)
+	fmt.Printf("ReleaseQueue: key=%x\n", eqi.ShmKey)
 	DestroyShm(eqi.ShmData)
 }
 
 // RemoveShmId remove an exist shm queue (ipcrm -m shmid)
 func (eqi *EsQueueInfo) RemoveShmId() error {
+	fmt.Printf("RemoveShmId: key=%x\n", eqi.ShmKey)
 	return shm.Rm(eqi.ShmID)
 }
 
@@ -193,6 +195,19 @@
 	}
 
 	return int(quantity)
+}
+
+// Status status of shm queue
+func (eqi *EsQueueInfo) QueueElements() []ElemInfo {
+	var elems []ElemInfo
+
+	for i := 0; i < len(eqi.EsCaches); i++ {
+		if eqi.EsCaches[i].value.PicId != 0 && eqi.EsCaches[i].value.InfoId != 0 {
+			elems = append(elems, eqi.EsCaches[i].value)
+		}
+	}
+
+	return elems
 }
 
 // Put 鍗曟鍐欏叆锛屽彲鑳藉け璐�
@@ -258,7 +273,7 @@
 		return ok, qua
 	}
 
-	to := time.NewTimer(time.Duration(timeout)*time.Millisecond)
+	to := time.NewTimer(time.Duration(timeout) * time.Millisecond)
 	defer to.Stop()
 
 	for {
@@ -309,7 +324,7 @@
 		putNo := atomic.LoadUint32(&cache.putNo)
 		if getPosNew == getNo && getNo == putNo-eqi.Queue.capacity {
 			val := cache.value
-			cache.value = ElemInfo{ShmId:0}
+			cache.value = ElemInfo{PicId: 0}
 			atomic.AddUint32(&cache.getNo, eqi.Queue.capacity)
 			return val, true, int(posCnt - 1)
 		} else {
@@ -336,13 +351,13 @@
 		return val, ok, qua
 	}
 
-	to := time.NewTimer(time.Duration(timeout)*time.Millisecond)
+	to := time.NewTimer(time.Duration(timeout) * time.Millisecond)
 	defer to.Stop()
 
 	for {
 		select {
 		case <-to.C:
-			return ElemInfo{},false, 0
+			return ElemInfo{}, false, 0
 		default:
 			val, ok, qua = eqi.Get()
 			if ok {
@@ -365,4 +380,3 @@
 	v++
 	return v
 }
-

--
Gitblit v1.8.0