Adding missing instructions to load missing Apache2 modules for Fidelius to work!
[iotcloud.git] / version1 / src / js / iotjs / orig / slotbuffer.js
1 class SlotBuffer{
2         constructor(){
3                 this.DEFAULT_SIZE = 128;
4                 this.array = [];
5                 this.head=0;
6                 this.tail=0;
7                 this.oldestseqn = 0;
8         }
9         size(){
10                 if(this.head>=this.tail){
11                         return this.head-this.tail;
12                 }
13                 return (this.array.length + this.head - this.tail);
14         }
15         capacity(){
16                 return this.array.length-1;
17         }
18         resize(newsize){
19                 if(newsize === (this.array.length-1)){
20                         return;
21                 }
22                 var newarray = [];
23                 var currsize = this.size();
24                 var index = this.tail;
25                 for(let i = 0 ; i<currsize ; i++){
26                         newarray[i]=this.array[index];
27                         if((++index) === this.array.length){
28                                 index = 0;
29                         }
30                         this.array = newarray;
31                         this.tail=0;
32                         this.head=currsize; 
33                 }
34         }
35         incrementHead(){
36                 this.head++;
37                 if(this.head >= this.array.length){
38                         this.head=0;
39                 }
40         }
41         incrementTail(){
42                 this.tail++;
43                 if(this.tail >= this.array.length){
44                         this.tail=0;
45                 }
46         }
47         putSlot(s){
48                 if(!(s instanceof Slot)){
49                         throw new Error("Error with arguments. Argument should be a slot object");
50                 }
51                 this.array[this.head]=s;
52                 this.incrementHead();
53
54                 if(this.oldestseqn ===0){
55                         this.oldestseqn = s.getSequenceNumber();
56                 }
57
58                 if(this.head === this.tail){
59                         this.incrementTail();
60                         this.oldestseqn++;
61                 }
62         }
63         getSlot(seqnum){
64                 var diff = (seqnum - this.oldestseqn);
65                 var index = diff + this.tail;
66                 if(index >= this.array.length){
67                         if(this.head >= this.tail){
68                                 return null;
69                         }
70                         index = index - this.array.length; 
71                 }
72                 if(index >= this.array.length){
73                         return null;
74                 }
75
76                 if(this.head >= this.tail && index >= this.head){
77                         return null;
78                 }
79
80                 return this.array[index];
81         }
82
83         getOldestSeqNum(){
84                 return this.oldestseqn;
85         }
86         getNewestSeqNum(){
87                 return this.oldestseqn + this.size() -1
88         }
89 }