char queue for WiFi communication
objectQueue.cpp@2:8f54ba4d961f, 2017-08-23 (annotated)
- Committer:
- williampeers
- Date:
- Wed Aug 23 02:10:23 2017 +0000
- Revision:
- 2:8f54ba4d961f
- Parent:
- 1:f9e6627f1f59
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
williampeers | 0:043b1271ed1b | 1 | #include "objectQueue.h" |
williampeers | 0:043b1271ed1b | 2 | |
williampeers | 0:043b1271ed1b | 3 | StrQueue::StrQueue() { |
williampeers | 0:043b1271ed1b | 4 | chars = (char*)malloc(sizeof(char[1])); |
williampeers | 2:8f54ba4d961f | 5 | lock = new Mutex(); |
williampeers | 0:043b1271ed1b | 6 | size = 1; |
williampeers | 0:043b1271ed1b | 7 | front = 0; |
williampeers | 0:043b1271ed1b | 8 | back = 0; |
williampeers | 0:043b1271ed1b | 9 | empty = true; |
williampeers | 0:043b1271ed1b | 10 | full = false; |
williampeers | 0:043b1271ed1b | 11 | } |
williampeers | 0:043b1271ed1b | 12 | |
williampeers | 0:043b1271ed1b | 13 | StrQueue::StrQueue(int size) { |
williampeers | 0:043b1271ed1b | 14 | chars = (char*)malloc(sizeof(char[size])); |
williampeers | 0:043b1271ed1b | 15 | size = size; |
williampeers | 0:043b1271ed1b | 16 | front = 0; |
williampeers | 0:043b1271ed1b | 17 | back = 0; |
williampeers | 0:043b1271ed1b | 18 | empty = true; |
williampeers | 0:043b1271ed1b | 19 | full = false; |
williampeers | 0:043b1271ed1b | 20 | } |
williampeers | 0:043b1271ed1b | 21 | |
williampeers | 0:043b1271ed1b | 22 | StrQueue::~StrQueue(void) { |
williampeers | 0:043b1271ed1b | 23 | free(chars); |
williampeers | 0:043b1271ed1b | 24 | } |
williampeers | 0:043b1271ed1b | 25 | |
williampeers | 0:043b1271ed1b | 26 | int StrQueue::put(const char* message, int length) { |
williampeers | 2:8f54ba4d961f | 27 | lock->lock(); |
williampeers | 0:043b1271ed1b | 28 | int i; |
williampeers | 0:043b1271ed1b | 29 | if (length == 0) { |
williampeers | 0:043b1271ed1b | 30 | for (i = 0; empty||(front != (back+i)%size); i++) { |
williampeers | 0:043b1271ed1b | 31 | if ((chars[back + i] = message[i]) == '\0') { |
williampeers | 0:043b1271ed1b | 32 | back = (back + i + 1)%size; |
williampeers | 0:043b1271ed1b | 33 | empty = false; |
williampeers | 0:043b1271ed1b | 34 | if (front == back) { |
williampeers | 0:043b1271ed1b | 35 | full = true; |
williampeers | 0:043b1271ed1b | 36 | } |
williampeers | 2:8f54ba4d961f | 37 | lock->unlock(); |
williampeers | 0:043b1271ed1b | 38 | return(i); |
williampeers | 0:043b1271ed1b | 39 | } |
williampeers | 0:043b1271ed1b | 40 | } |
williampeers | 0:043b1271ed1b | 41 | if (front == back) { |
williampeers | 0:043b1271ed1b | 42 | full = true; |
williampeers | 0:043b1271ed1b | 43 | } |
williampeers | 2:8f54ba4d961f | 44 | lock->unlock(); |
williampeers | 0:043b1271ed1b | 45 | return(0); |
williampeers | 0:043b1271ed1b | 46 | } |
williampeers | 0:043b1271ed1b | 47 | else { |
williampeers | 0:043b1271ed1b | 48 | if ((back - front)%size <= length) { |
williampeers | 2:8f54ba4d961f | 49 | lock->unlock(); |
williampeers | 0:043b1271ed1b | 50 | return(0); |
williampeers | 0:043b1271ed1b | 51 | } |
williampeers | 0:043b1271ed1b | 52 | for (i = 0; empty||(i <= length); i++) { |
williampeers | 0:043b1271ed1b | 53 | chars[back + i] = message[i]; |
williampeers | 0:043b1271ed1b | 54 | } |
williampeers | 0:043b1271ed1b | 55 | back = (back + i + 1)%size; |
williampeers | 0:043b1271ed1b | 56 | empty = false; |
williampeers | 0:043b1271ed1b | 57 | if (front == back) { |
williampeers | 0:043b1271ed1b | 58 | full = true; |
williampeers | 0:043b1271ed1b | 59 | } |
williampeers | 2:8f54ba4d961f | 60 | lock->unlock(); |
williampeers | 0:043b1271ed1b | 61 | return(i); |
williampeers | 0:043b1271ed1b | 62 | } |
williampeers | 0:043b1271ed1b | 63 | } |
williampeers | 0:043b1271ed1b | 64 | |
williampeers | 0:043b1271ed1b | 65 | int StrQueue::get(char* out, int length) { |
williampeers | 2:8f54ba4d961f | 66 | lock->lock(); |
williampeers | 0:043b1271ed1b | 67 | int i = 0; |
williampeers | 0:043b1271ed1b | 68 | if (empty) { |
williampeers | 2:8f54ba4d961f | 69 | lock->unlock(); |
williampeers | 0:043b1271ed1b | 70 | return(0); |
williampeers | 0:043b1271ed1b | 71 | } |
williampeers | 0:043b1271ed1b | 72 | for (i = 0;; i++) { |
williampeers | 0:043b1271ed1b | 73 | if (i > length) { |
williampeers | 0:043b1271ed1b | 74 | strncpy(out, "", length); |
williampeers | 0:043b1271ed1b | 75 | return(0); |
williampeers | 0:043b1271ed1b | 76 | } |
williampeers | 0:043b1271ed1b | 77 | if ((out[i] = chars[(front + i)%size]) == '\0') { |
williampeers | 0:043b1271ed1b | 78 | front = (front + i + 1)%size; |
williampeers | 0:043b1271ed1b | 79 | break; |
williampeers | 0:043b1271ed1b | 80 | } |
williampeers | 0:043b1271ed1b | 81 | } |
williampeers | 0:043b1271ed1b | 82 | if (front == back) { |
williampeers | 0:043b1271ed1b | 83 | empty = true; |
williampeers | 0:043b1271ed1b | 84 | } |
williampeers | 2:8f54ba4d961f | 85 | lock->unlock(); |
williampeers | 0:043b1271ed1b | 86 | |
williampeers | 0:043b1271ed1b | 87 | return(i); |
williampeers | 0:043b1271ed1b | 88 | } |
williampeers | 0:043b1271ed1b | 89 | |
williampeers | 0:043b1271ed1b | 90 | int StrQueue::getChars(char* buffer, int size) { |
williampeers | 0:043b1271ed1b | 91 | int ret = 0, total = 0; |
williampeers | 2:8f54ba4d961f | 92 | lock->lock(); |
williampeers | 0:043b1271ed1b | 93 | if (empty) {return(0);} |
williampeers | 0:043b1271ed1b | 94 | while ((ret = get(buffer + ret, size - total)) > 0) { |
williampeers | 0:043b1271ed1b | 95 | buffer += ret; |
williampeers | 1:f9e6627f1f59 | 96 | total++; |
williampeers | 0:043b1271ed1b | 97 | ret = 0; |
williampeers | 0:043b1271ed1b | 98 | } |
williampeers | 2:8f54ba4d961f | 99 | lock->unlock(); |
williampeers | 0:043b1271ed1b | 100 | return(total); |
williampeers | 0:043b1271ed1b | 101 | } |
williampeers | 0:043b1271ed1b | 102 | |
williampeers | 0:043b1271ed1b | 103 | bool StrQueue::isfull() { |
williampeers | 0:043b1271ed1b | 104 | return(full); |
williampeers | 0:043b1271ed1b | 105 | } |
williampeers | 0:043b1271ed1b | 106 | |
williampeers | 0:043b1271ed1b | 107 | bool StrQueue::isEmpty() { |
williampeers | 0:043b1271ed1b | 108 | return(empty); |
williampeers | 0:043b1271ed1b | 109 | } |