Browse Source

update simulator with watchdog, ping, reset, poweroff and relaod commands

pehladik 4 years ago
parent
commit
89a2bae5c0
1 changed files with 124 additions and 28 deletions
  1. 124
    28
      software/simulateur/main.cpp

+ 124
- 28
software/simulateur/main.cpp View File

@@ -27,8 +27,14 @@ const char LABEL_ROBOT_SEPARATOR_CHAR = '=';
27 27
 const char LABEL_ROBOT_ENDING_CHAR = 0x0D;
28 28
 
29 29
 int server_fd, new_socket, valread;
30
+struct sockaddr_in address;
31
+int addrlen;
30 32
 #define PORT 6699
31 33
 
34
+int status = 0;
35
+int noerr = 0;
36
+int isWD = 0;
37
+
32 38
 long int ellapse(struct timespec ref, struct timespec cur) {
33 39
     long int e;
34 40
     e = cur.tv_sec - ref.tv_sec;
@@ -47,13 +53,13 @@ void print_time(struct timespec start_time) {
47 53
 int simulate_error() {
48 54
     int r = rand() % 1000;
49 55
     if (r > 950) {
50
-        printf(">>> I don't understand what you said (-1)\n");
56
+        printf("[I don't understand what you said (-1)]\n");
51 57
         return -1;
52 58
     } else if (r > 900) {
53
-        printf(">>> I don't want to respond (-2)\n");
59
+        printf("[I'm mute, because I never got your message (-2)]\n");
54 60
         return -2;
55 61
     }
56
-    printf(">>> WILCO (0)\n");
62
+    printf("[WILCO (0)] ");
57 63
     return 0;
58 64
 }
59 65
 
@@ -62,12 +68,8 @@ void simulate_transmission_time() {
62 68
 }
63 69
 
64 70
 void open_server() {
65
-    struct sockaddr_in address;
66
-    int opt = 1;
67
-    int addrlen = sizeof (address);
68
-
69
-    srand(time(NULL));
70 71
 
72
+    int opt = 1;
71 73
     // Creating socket file descriptor 
72 74
     if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
73 75
         perror("socket failed");
@@ -80,11 +82,11 @@ void open_server() {
80 82
         perror("setsockopt");
81 83
         exit(EXIT_FAILURE);
82 84
     }
83
-    address.sin_family = AF_INET;
84
-    address.sin_addr.s_addr = INADDR_ANY;
85
-    address.sin_port = htons(PORT);
86 85
 
87
-    cout << "<<< simulator >>>" << endl;
86
+    cout << "<<< simulator >>>" << endl;    
87
+    cout << ">>> Hello, I'm Mr " ;
88
+    if (noerr) cout << "perfect ";
89
+    cout << "Robot" << endl;
88 90
 
89 91
     // Forcefully attaching socket to the port 8080 
90 92
     if (bind(server_fd, (struct sockaddr *) &address,
@@ -94,13 +96,16 @@ void open_server() {
94 96
     }
95 97
     cout << ">>> I create a server" << endl;
96 98
     cout << ">>> ..." << endl;
97
-    cout << ">>> I'm waiting a client" << endl;
99
+
100
+}
101
+
102
+void wait_connection() {
98 103
     if (listen(server_fd, 3) < 0) {
99 104
         perror("listen");
100 105
         exit(EXIT_FAILURE);
101 106
     }
102 107
 
103
-    cout << ">>> Hello, I'm Robot" << endl;
108
+    cout << ">>> I'm waiting a client" << endl;
104 109
     if ((new_socket = accept(server_fd, (struct sockaddr *) &address,
105 110
             (socklen_t*) & addrlen)) < 0) {
106 111
         perror("accept");
@@ -108,32 +113,96 @@ void open_server() {
108 113
     }
109 114
 }
110 115
 
116
+void reset(){
117
+    isWD = 0;
118
+    status = 0;   
119
+    cout << ">>> XX I stop XX" << endl;
120
+}
121
+
111 122
 int main(int argc, char const *argv[]) {
123
+    if (argc != 1){
124
+        if (argv[1] == std::string("noerror")){
125
+            noerr = 1;
126
+        }
127
+    }
112 128
 
113 129
     char buffer[1024] = {0};
130
+    addrlen = sizeof (address);
131
+    address.sin_family = AF_INET;
132
+    address.sin_addr.s_addr = INADDR_ANY;
133
+    address.sin_port = htons(PORT);
134
+
135
+    srand(time(NULL));
136
+
114 137
     open_server();
138
+    wait_connection();
115 139
     cout << ">>> I'm ready to receive something" << endl;
116 140
     struct timespec start_time;
117 141
     clock_gettime(CLOCK_REALTIME, &start_time);
142
+    struct timespec start_wd;
143
+
144
+
145
+    struct timespec t;
146
+
147
+    long int e;
148
+    struct timespec last_call;
149
+    clock_gettime(CLOCK_REALTIME, &last_call);
150
+    isWD = 0;
151
+
152
+    while (status < 3) {
153
+
154
+        if (isWD) {
155
+            clock_gettime(CLOCK_REALTIME, &t);
156
+            e = ellapse(last_call, t);
157
+            if ((e / 1000000000) > 3) {
158
+                cout << ">>> You break my heart, you never talk at the right time." << endl;
159
+                break;
160
+            }
161
+        }
162
+
118 163
 
119
-    while (1) {
120 164
         valread = read(new_socket, buffer, 1024);
121
-        if (valread <= 0)
122
-            break;
123
-        print_time(start_time);
124
-        printf(" >>> I received a message : %s\n", buffer);
165
+        if (valread <= 0) {
166
+            if (errno == EAGAIN) {
167
+                status = 3;
168
+                cout << ">>> You break my heart, I've been waiting too long for you." << endl;
169
+                break;
170
+            } else {
171
+                cout << ">>> Why did you hang up? Please, contact me again." << endl;
172
+                reset();
173
+                wait_connection();
174
+                clock_gettime(CLOCK_REALTIME, &last_call);
175
+            }
176
+        }
125 177
         string s = "";
126
-        int error = simulate_error();
178
+        int error = 0;
179
+        
180
+        if (!noerr) error = simulate_error();
181
+        
127 182
         if (error == 0) {
128
-            struct timespec t;
129
-            long int e;
183
+
184
+            print_time(start_time);
185
+            printf(": I received a message %s\n", buffer);
130 186
             switch (buffer[0]) {
131 187
                 case LABEL_ROBOT_START_WITHOUT_WD:
132 188
                     cout << ">>> I start without watchdog" << endl;
133 189
                     s += LABEL_ROBOT_OK;
134 190
                     break;
191
+                case LABEL_ROBOT_PING:
192
+                    cout << ">>> ...Pong" << endl;
193
+                    s += LABEL_ROBOT_OK;
194
+                    break;
135 195
                 case LABEL_ROBOT_START_WITH_WD:
196
+                    clock_gettime(CLOCK_REALTIME, &start_wd);
197
+                    clock_gettime(CLOCK_REALTIME, &last_call);
198
+                    struct timeval tv;
199
+                    tv.tv_sec = 3;
200
+                    tv.tv_usec = 0;
201
+                    setsockopt(new_socket, SOL_SOCKET, SO_RCVTIMEO, (const char*) &tv, sizeof tv);
202
+
203
+                    cout << ">>> I start with watchdog" << endl;
136 204
                     s += LABEL_ROBOT_OK;
205
+                    isWD = 1;
137 206
                     break;
138 207
                 case LABEL_ROBOT_MOVE:
139 208
                     switch (buffer[2]) {
@@ -175,11 +244,37 @@ int main(int argc, char const *argv[]) {
175 244
                     }
176 245
                     break;
177 246
                 case LABEL_ROBOT_RELOAD_WD:
178
-                    cout << ">>> I start with watchdog" << endl;
179
-                    s += LABEL_ROBOT_OK;
247
+                    clock_gettime(CLOCK_REALTIME, &t);
248
+                    e = ellapse(start_wd, t);
249
+                    e = (e / 1000000) % 1000;
250
+                    if (isWD) {
251
+                        if ((e < 50) || (e > 950)) {
252
+                            cout << ">>> Just in time for a reload " << e << "ms" << endl;
253
+                            last_call = t;
254
+                            status = 0;
255
+                            s += LABEL_ROBOT_OK;
256
+                        } else {
257
+                            status++;
258
+                            cout << ">>> You missed the date, -1 point " << e << "ms (" << status << ")" << endl;
259
+
260
+                            s += LABEL_ROBOT_UNKNOWN_COMMAND;
261
+                        }
262
+                    } else {
263
+                        cout << "Why you said that, I do nothing" << endl;
264
+                    }
180 265
                     break;
181 266
                 case LABEL_ROBOT_POWEROFF:
182
-                    cout << ">>> Bye bye" << endl;
267
+                    cout << ">>> Bye bye, see you soon" << endl;
268
+                    s += LABEL_ROBOT_OK;
269
+                    status = 10;
270
+                    break;
271
+                case LABEL_ROBOT_RESET:
272
+                    cout << ">>> I reset" << endl;
273
+                    s += LABEL_ROBOT_OK;
274
+                    reset();
275
+                    break;
276
+                case LABEL_ROBOT_GET_STATE:
277
+                    cout << ">>> I'm fine, thank you" << endl;
183 278
                     s += LABEL_ROBOT_OK;
184 279
                     break;
185 280
                 default:
@@ -188,15 +283,16 @@ int main(int argc, char const *argv[]) {
188 283
             }
189 284
             simulate_transmission_time();
190 285
             send(new_socket, s.c_str(), s.length(), 0);
191
-        } else if (error == -1){
286
+        } else if (error == -1) {
192 287
             s += LABEL_ROBOT_UNKNOWN_COMMAND;
193 288
             simulate_transmission_time();
194 289
             send(new_socket, s.c_str(), s.length(), 0);
195
-        } else if (error == -2){
290
+        } else if (error == -2) {
196 291
             /* Do nothing */
197 292
         }
198 293
     }
199
-    cout << "The robot is dead. End of story. " << endl;
294
+
295
+    cout << "The robot is out. End of story. " << endl;
200 296
     cout << " /\\_/\\" << endl << "( o.o )" << endl << " > ^ <" << endl;
201 297
     return 0;
202 298
 } 

Loading…
Cancel
Save