-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimaryserver.c
More file actions
159 lines (136 loc) · 4.19 KB
/
primaryserver.c
File metadata and controls
159 lines (136 loc) · 4.19 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 <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/mman.h>
#define pServerMtype 3
#define clientMtype 1
struct Message {
long mType;
int sqno;
int opno;
char filename[100]; // Adjust the size based on your needs
};
struct SharedMemory {
int numberOfNodes;
int adjacencyMatrix[30][30]; // Assuming a maximum of 30 nodes
};
struct Result{
long mtype;
char message[100];
char output[100];
};
struct argument{
struct Message message;
int msqid;
};
void* handleWrite(void* arg);
int main() {
key_t msgQueueKey = ftok("/tmp", 'A');
int msqid = msgget(1234, 0666);
printf("%d\n",msqid);
while(1){
struct Message msg;
msgrcv(msqid, &msg, sizeof(msg),2,0);
printf("%ld\n",msg.mType);
struct argument arg;
arg.message=msg;
arg.msqid=msqid;
pthread_t thread;
pthread_create(&thread,NULL, handleWrite, (void*)&arg);
pthread_join(thread,NULL);
}
return 0;
}
void* handleWrite(void* arg){
struct argument Arg = *((struct argument *)arg);
struct Message msg = Arg.message;
key_t shmKey = ftok("/tmp", 'B' + msg.sqno);
int shmid = shmget(shmKey, sizeof(struct SharedMemory),0666|IPC_CREAT);
// Attach shared memory segment to the process
struct SharedMemory *sharedMemory = (struct SharedMemory *)shmat(shmid, NULL, 0);
if (sharedMemory == (void *)-1) {
perror("Error attaching shared memory segment");
exit(EXIT_FAILURE);
}
// Map the shared memory segment
// struct SharedMemory SharedMemoryptr = mmap(NULL, sizeof(struct SharedMemory), PROT_READ | PROT_WRITE, MAP_SHARED, shmid, 0);
// if (SharedMemoryptr == MAP_FAILED) {
// perror("mmap");
// close(shmid);
// pthread_exit(NULL);
// }
struct Result result;
result.mtype= 8*(msg.sqno)+msg.opno;
int option=msg.opno;
FILE* file;
if(option==1){
file=fopen(msg.filename,"w");
if (file == NULL) {
perror("Error opening file");
return 0;
}
fprintf(file,"%d\n", sharedMemory->numberOfNodes);
for (int i = 0; i < sharedMemory->numberOfNodes; ++i) {
for (int j = 0; j < sharedMemory->numberOfNodes; ++j) {
fprintf(file, "%d ", sharedMemory->adjacencyMatrix[i][j]);
}
fprintf(file, "\n");
}
strcpy(result.message,"File successfully added");
fclose(file);
}
else if (option==2){
file = fopen(msg.filename, "r");
if (file == NULL) {
perror("Error opening file");
return 0;
}
// Read the existing number of nodes
if (fscanf(file, "%d", &(sharedMemory->numberOfNodes)) != 1) {
perror("Error reading number of nodes");
fclose(file);
return 0;
}
// Read the existing adjacency matrix
for (int i = 0; i < sharedMemory->numberOfNodes; ++i) {
for (int j = 0; j < sharedMemory->numberOfNodes; ++j) {
if (fscanf(file, "%d", &(sharedMemory->adjacencyMatrix[i][j])) != 1) {
perror("Error reading adjacency matrix");
fclose(file);
return 0;
}
}
}
fclose(file);
file = fopen(msg.filename, "w");
if (file == NULL) {
perror("Error opening file");
return 0;
}
fprintf(file, "%d\n", sharedMemory->numberOfNodes);
for (int i = 0; i < sharedMemory->numberOfNodes; ++i) {
for (int j = 0; j < sharedMemory->numberOfNodes; ++j) {
fprintf(file, "%d ", sharedMemory->adjacencyMatrix[i][j]);
}
fprintf(file, "\n");
}
strcpy(result.message, "File successfully modified");
fclose(file);
}
else{
printf("Invalid Request received\n");
}
// munmap(SharedMemoryptr, sizeof(struct SharedMemory));
if (msgsnd(Arg.msqid, &result, sizeof(struct Result), 0) == -1) {
perror("Error sending message to the Client");
exit(EXIT_FAILURE);
}
close(shmid);
pthread_exit(NULL);
}