-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_interface.cpp
More file actions
160 lines (109 loc) · 4.43 KB
/
cli_interface.cpp
File metadata and controls
160 lines (109 loc) · 4.43 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include <netdb.h>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include "globals.hpp"
#include "client_interface.hpp"
#include "client_utils.hpp"
#include "node_controller.hpp"
#include <iostream>
using std::cerr;
using std::endl;
extern unsigned MY_NODE_ID;
clientUtils::Message construct_client_msg( Action act,
subAction sub_act,
clientUtils::MESSAGE_T msg_type )
{
return clientUtils::Message{ getpid( ), msg_type, act, sub_act };
}
void enter_critical_section( Action act, subAction subact )
{
using namespace clientUtils;
Socket mysock( AF_LOCAL, SOCK_DGRAM );
struct sockaddr_un local_monitor_addr, myaddr;
socklen_t sock_len;
bzero( &myaddr, sizeof (myaddr ) );
bzero( &local_monitor_addr, sizeof (local_monitor_addr ) );
local_monitor_addr.sun_family = AF_LOCAL;
snprintf( local_monitor_addr.sun_path, sizeof (local_monitor_addr.sun_path ) - 1,
nodeController::LocalListensock_template.c_str( ), MY_NODE_ID );
myaddr.sun_family = AF_LOCAL;
snprintf( myaddr.sun_path, sizeof (myaddr.sun_path ) - 1,
ClientSock_template, getpid( ) );
unlink( myaddr.sun_path );
if ( bind( mysock.get_channel( ), (struct sockaddr*) &myaddr,
sizeof (struct sockaddr_un ) ) != 0 ) {
perror( "enter_crit_sect: bind to Unix Domain socket failed: " );
exit( EXIT_FAILURE );
}
char buffer[sizeof (Message )];
host2network_msg( construct_client_msg( act,
subact,
MESSAGE_T::LOCAL_REQUEST ),
buffer );
//here we are asking node controller for permission to enter CS
if ( sendto( mysock.get_channel( ), buffer,
sizeof (buffer ), 0,
(struct sockaddr *) &local_monitor_addr,
sizeof (local_monitor_addr ) ) != sizeof (Message ) ) {
cerr << "enter_crit_section: sendto did not transmit"
" all bytes" << endl;
exit( EXIT_FAILURE );
}
memset( buffer, 0, sizeof (buffer ) );
//here we are waiting for node controller to give us
//permission to enter CS
if ( recvfrom( mysock.get_channel( ), buffer,
sizeof (buffer ), MSG_WAITALL,
nullptr, nullptr ) != sizeof (Message ) ) {
cerr << "enter_crit_section: recvfrom did not receive"
" all bytes" << endl;
exit( EXIT_FAILURE );
}
unlink( myaddr.sun_path );
//now okay to enter critical section
return;
}
void leave_critical_section( Action act, subAction subact )
{
using namespace clientUtils;
struct sockaddr_un local_sender_addr, myaddr;
bzero( &local_sender_addr, sizeof (local_sender_addr ) );
local_sender_addr.sun_family = AF_LOCAL;
snprintf( local_sender_addr.sun_path,
sizeof (local_sender_addr.sun_path ),
nodeController::LocalSendersock_template.c_str( ), MY_NODE_ID,
static_cast<int> ( act ), static_cast<int> ( subact ) );
Socket mysock( AF_LOCAL, SOCK_DGRAM );
bzero( &myaddr, sizeof (myaddr ) );
myaddr.sun_family = AF_LOCAL;
snprintf( myaddr.sun_path, sizeof (myaddr.sun_path ) - 1,
ClientSock_template, getpid( ) );
unlink( myaddr.sun_path );
if ( bind( mysock.get_channel( ), (struct sockaddr*) &myaddr,
sizeof (struct sockaddr_un ) ) != 0 ) {
perror( "enter_crit_sect: bind to Unix Domain socket failed: " );
exit( EXIT_FAILURE );
}
char buffer[sizeof (Message )];
host2network_msg( construct_client_msg( act,
subact,
MESSAGE_T::FINISHED ),
buffer );
//here we are telling NC that we are finished executing C.S.
if ( sendto( mysock.get_channel( ), buffer,
sizeof (buffer ), 0,
(struct sockaddr *) &local_sender_addr,
sizeof (local_sender_addr ) ) != sizeof (Message ) ) {
cerr << "leave_crit_section: sendto did not transmit"
" all bytes" << endl;
exit( EXIT_FAILURE );
}
//now okay for other processes to enter critical section
//associated with act, subact
return;
}