-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder2.cc
123 lines (107 loc) · 3.63 KB
/
encoder2.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <string.h>
#include <semaphore.h>
#include <errno.h>
#include "Encoder.h"
int main(int argc, char *argv[]) {
// Checking input validity
if (argc != 1) {
std::cout << "Correct usage: $./encoder2.ex //No arguments" << std::endl;
return -1;
}
Encoder enc2;
message * m;
// Set up semaphores - They already exist
sem_t *sem_channel;
sem_t *sem_encoder2;
sem_t *sem_consumer;
// Channel semaphore
if ((sem_channel = sem_open(SEM_CHANNEL, 0)) == SEM_FAILED) {
perror("sem_open/channel");
exit(EXIT_FAILURE);
}
// Encoder 2 semaphore
if ((sem_encoder2 = sem_open(SEM_ENCODER2, 0)) == SEM_FAILED) {
perror("sem_open/encoder2");
exit(EXIT_FAILURE);
}
// P2 semaphore
if ((sem_consumer = sem_open(SEM_CONSUMER, 0)) == SEM_FAILED) {
perror("sem_open/consumer");
exit(EXIT_FAILURE);
}
// Left encoder 2 block is shared with channel
message *blockLeft = attachBlock(FILENAME, BLOCK_SIZE, CHAN_ENC2_BLOCK_ID);
if (blockLeft == NULL) {
std::cout << "ERROR: Couldn't get block." << std::endl;
return -1;
}
// Right encoder 2 block is shared with p2
message *blockRight = attachBlock(FILENAME, BLOCK_SIZE, ENC2_P2_BLOCK_ID);
if (blockRight == NULL) {
std::cout << "ERROR: Couldn't get block." << std::endl;
return -1;
}
// This shared memory block contains the flag that indicates the flow of the message
bool *directionFlag = attachFlagBlock(FILENAME, sizeof(bool *), DIRECTION_BLOCK_ID);
if (directionFlag == NULL) {
std::cout << "ERROR: Couldn't get flag block." << std::endl;
return -1;
}
// Critical Section
do {
sem_wait(sem_encoder2);
if (*directionFlag) {
// Flow left: -> right
std::cout << "Encoder 2 from Channel: [" << blockLeft->text << "]" << std::endl;
if (blockLeft->status == -1) {
// Channel is requesting restransmition of a message
blockRight->status = -1;
// Signal p2 to resend
sem_post(sem_consumer);
} else {
// This is a message from p1
if (enc2.decodeMessage(*blockLeft)) {
// Message decoded successfully
memcpy(blockRight, blockLeft, sizeof(*blockLeft));
std::cout << "Encoder 2 to P2: [" << blockRight->text << "]" << std::endl;
// Signal p2
sem_post(sem_consumer);
} else {
std::cout << "Message corrupted - Requesting retransmition..." << std::endl;
// Update the message status
blockLeft->status = -1;
// Revert the flow of transmition
*directionFlag = false;
// Signal channel
sem_post(sem_channel);
}
}
} else {
// Normal operation p2 -> p1
std::cout << "Encoder 2 from P2: [" << blockRight->text << "]" << std::endl;
enc2.encodeMessage(*blockRight);
m = enc2.getMessage();
// Just pass the message to channel
memcpy(blockLeft, m, sizeof(*blockLeft));
std::cout << "Encoder 2 to Channel: [" << blockLeft->text << "]" << std::endl;
// Signal channel
sem_post(sem_channel);
}
} while (strcmp(blockRight->text, "TERM"));
// Release semaphores and memory
sem_close(sem_channel);
sem_close(sem_encoder2);
sem_close(sem_consumer);
detachBlock(blockLeft);
detachBlock(blockRight);
detachBlock(directionFlag);
// Delete the shared memory after it's no longer used
if (destroyBlock(FILENAME, sizeof(bool *), CHAN_ENC2_BLOCK_ID)) {
std::cout << "Destroyed block [" << CHAN_ENC2_BLOCK_ID << "]" << std::endl;
} else {
std::cout << "Couldn't destroy block [" << CHAN_ENC2_BLOCK_ID << "]" << std::endl;
return -1;
}
return 0;
}