-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.cpp
More file actions
123 lines (120 loc) · 2.98 KB
/
Server.cpp
File metadata and controls
123 lines (120 loc) · 2.98 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
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <fcntl.h>
struct shared_buffer
{
int size_buff[8];
char block_data[8][124];
};
int main(int argc, char** argv)
{
// Get directory and help
char* dir;
if(argv[1] == NULL)
{
char* buff = new char[256];
getcwd(buff, 256);
int size = strlen(buff);
dir = new char[size + 16 + 1];
dir[0]='\0';
strcat(dir,buff);
dir[size]= '/';
dir[size + 1]='\0';
strcat(dir,"transmission.txt\0");
delete[] buff;
printf("No arguments. Creating default file: %s\n", dir);
}
else
{
dir = new char[strlen(argv[1])];
strcpy(dir,argv[1]);
// help
if( strlen(dir) == 2 && strncmp(dir, "-h", 2) == 0)
{
printf("Server take file name as argument and save all what client send.\n");
exit(EXIT_SUCCESS);
}
printf("Creating file wirh name %s\n", dir);
}
// Get shared memory id
void *shared_memory = (void*)0;
struct shared_buffer* shared_data = new struct shared_buffer;
int shmid;
srand((unsigned int)getpid());
shmid = shmget((key_t)7501, 1024, 0666 | IPC_CREAT);
if (shmid == -1)
{
fprintf(stderr, "shmget failed\n");
exit(EXIT_FAILURE);
}
// Attach shared memory
shared_memory = shmat(shmid, (void *)0, 0);
if (shared_memory == (void *)-1)
{
fprintf(stderr, "shmat failed\n");
exit(EXIT_FAILURE);
}
// Associate our struct and shared memory
shared_data = (struct shared_buffer*)shared_memory;
printf("Getting shared memory: %p\n", shared_memory);
// Create semaphores
int sem_full, sem_empty;
sem_full = semget((key_t)1057, 1, 0666 | IPC_CREAT);
sem_empty = semget((key_t)2057, 1, 0666 | IPC_CREAT);
if(sem_full == -1 || sem_empty == -1)
{
printf("semget failed\n");
exit(EXIT_FAILURE);
}
// Create struct for control semaphores
struct sembuf control_sem;
control_sem.sem_flg = 0;
control_sem.sem_num = 0;
// Waiting if it need
while(semctl(sem_full, 0, GETVAL) != 0 && semctl(sem_empty, 0, GETVAL) !=0)
{
printf("Waiting for client\n");
sleep(1);
};
// Create file
int file_id;
file_id = creat(dir, 0644);
delete[] dir;
int write_size, mod;
int cycle_counter = 0;
while(true)
{
mod = cycle_counter%8;
control_sem.sem_op = -1;
semop(sem_full,&control_sem, 1);
write_size = write(file_id, shared_data->block_data[mod], shared_data->size_buff[mod]);
control_sem.sem_op = 1;
semop(sem_empty,&control_sem, 1);
if(write_size <= 0)
{
printf("File saved.\n");
break;
}
else
printf("Package %i is accepted. Size: %i.\n", cycle_counter, write_size);
cycle_counter++;
}
close(file_id);
// Detaches the shared memory segment
if (shmdt(shared_memory) == -1)
{
fprintf(stderr, "shmdt failed\n");
exit(EXIT_FAILURE);
}
// Mark the segment to be destroyed.
if (shmctl(shmid, IPC_RMID, 0) == -1)
{
fprintf(stderr, "shmctl(IPC_RMID) failed\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}