forked from NavroopKaur/Notification-Junction
-
Notifications
You must be signed in to change notification settings - Fork 20
/
nj_nonblocklib.c
179 lines (149 loc) · 4.22 KB
/
nj_nonblocklib.c
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* This file is for the library that provides the option for non_block,
* and also provides for the app to call nj internally, i.e. through the application's code
*
*/
#include "lib.h"
/* Register the application by writing the key-value string to the NJ using socket */
int appRegister(char *key_val_string)
{
int sock;
struct sockaddr_un server;
char data[1024];
if (key_val_string == NULL) {
printf("Provide arg string\n");
return -1;
}
strcpy(data, key_val_string);
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock < 0) {
perror("APP_REG : Opening Stream Socket");
exit(1);
}
server.sun_family = AF_UNIX;
strcpy(server.sun_path, AppReg);
if (connect
(sock, (struct sockaddr *)&server,
sizeof(struct sockaddr_un)) < 0) {
close(sock);
perror("APP_REG : Connecting Stream Socket");
exit(1);
}
if (write(sock, data, sizeof(data)) < 0)
perror("APP_REG : Writing On Stream Socket");
close(sock);
return 1;
}
/* UnRegister the application by writing the key-value string to the NJ using socket */
int appUnregister(char *key_val_string)
{
int sock;
struct sockaddr_un server;
char data[1024];
if (key_val_string == NULL) {
printf("Provide arg string\n");
return -1;
}
strcpy(data, key_val_string);
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock < 0) {
perror("APP_UNREG : Opening Stream Socket");
exit(1);
}
server.sun_family = AF_UNIX;
strcpy(server.sun_path, AppUnReg);
if (connect
(sock, (struct sockaddr *)&server,
sizeof(struct sockaddr_un)) < 0) {
close(sock);
perror("APP_UNREG : Connecting Stream Socket");
exit(1);
}
if (write(sock, data, sizeof(data)) < 0)
perror("APP_UNREG : Writing On Stream Socket");
close(sock);
return 1;//proper return without error
}
/* Request for notifications from an np, the choice field is B for blocking and N for non-blocking */
int appGetnotify(int pid, char *key_val_string, char choice)
{
int sock, x = 0;
struct sockaddr_un server;
int sock_block, rval_block, msgsock_block;
struct sockaddr_un server_block;
char buf[1024];
char data[1024];
char sock_name[512];
sprintf(sock_name, "%d", pid);
printf("pid as a string - %s\n", sock_name);
if(choice == 'B') {
// make a socket with pid_sock as the name
strcat(sock_name, "_sock");
printf("sock_name is %s\n", sock_name);
}
if (key_val_string == NULL) {
printf("Provide arg string\n");
return -1;
}
printf("Received :\npid = %d, keyvalstr = %s, choice = %c\n", pid,
key_val_string, choice);
char str[16];
sprintf(str, "%d", pid);
printf("STRING OF PID IS %s\n", str);
//strcpy(data, "pid::");
strcpy(data, str);
strcat(data, "##");
strcat(data, key_val_string);
if (choice == 'B') {
sock = socket(AF_UNIX, SOCK_STREAM, 0);
strcat(data, "##TYPE::B");
} else if (choice == 'N') {
sock = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
strcat(data, "##TYPE::N");
}
if (sock < 0) {
perror("APP_GETNOTIFY : ERROR OPENING STREAM SOCKET :");
exit(1);
}
server.sun_family = AF_UNIX;
strcpy(server.sun_path, AppGetnotify);
if (connect
(sock, (struct sockaddr *)&server,
sizeof(struct sockaddr_un)) < 0) {
close(sock);
perror("APP_GETNOTIFY : ERROR CONNECTING STREAM SOCKET :");
exit(1);
}
printf(" DATA SEND by nonblock library %s \n", data);
if (write(sock, data, sizeof(data)) < 0)
perror
("APP_GETNOTIFY : ERROR WRITING COMMAND ON STREAM SOCKET :");
if (choice == 'B') {
sock_block = socket(AF_UNIX, SOCK_STREAM, 0);
server_block.sun_family = AF_UNIX;
strcpy(server_block.sun_path, sock_name);
if (bind(sock_block, (struct sockaddr *)&server_block, sizeof(struct sockaddr_un))) {
perror("binding stream socket");
exit(EXIT_FAILURE);
}
printf("Socket bound\n");
if ((listen(sock_block, QLEN)) != 0) {
perror("Error in listening on pid_sock socket:");
}
msgsock_block = accept(sock_block, 0, 0);
do {
if ((rval_block = read(msgsock_block, buf, 1024)) < 0)
perror
("APP_GETNOTIFY : ERROR READING STREAM SOCKET IN CLIENT \n");
else {
x++;
if(x == 1) {
printf("APP_GETNOTIFY : NOTIFICATION RECEIVED BY GETNOTIFY.\n");
}
}
}while(rval_block >= 0);
}
unlink(sock_name);
close(sock);
return 1;
}