-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProtocol.cpp
91 lines (82 loc) · 2.22 KB
/
Protocol.cpp
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
#include "Protocol.h"
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/stat.h>
#include <limits.h>
#include <fcntl.h>
#include <errno.h>
#include <iostream>
#include <sstream>
Protocol::Protocol( const std::string & fifo ) : server_fifo_( fifo ) {
umask( 0 );
if ( mkfifo( server_fifo_.c_str(), S_IRUSR | S_IWUSR | S_IWGRP ) == -1
&& errno != EEXIST)
{
perror("mkfifo");
exit( EXIT_FAILURE );
}
server_fd_ = open( server_fifo_.c_str(), O_RDONLY );
if ( server_fd_ == -1 ) {
perror("open");
exit( EXIT_FAILURE );
}
dummy_fd_ = open( server_fifo_.c_str(), O_WRONLY );
if ( dummy_fd_ == -1 ) {
perror("open");
exit( EXIT_FAILURE );
}
if ( signal( SIGPIPE, SIG_IGN ) == SIG_ERR ) {
perror("signal");
exit( EXIT_FAILURE );
}
}
void Protocol::do_processing( ICmdDispatcher & cmd_disp ) {
std::string in_str, cmd_string, client_fifo, c_pid, s_res;
int count, n_bytes, client_fd;
char symb;
for( ; ; ) {
in_str.clear();
for ( count = 0; count < PIPE_BUF; ) {
n_bytes = read( server_fd_, &symb, sizeof( char ) );
if ( n_bytes == 0 || symb == '\n' )
break;
count += n_bytes;
in_str += symb;
}
if ( n_bytes == 0 || count >= PIPE_BUF )
continue;
std::size_t lpos = 0, rpos = 0;
lpos = in_str.find_first_not_of( ' ', rpos );
rpos = in_str.find_first_of( ' ', lpos );
if ( lpos != std::string::npos ) {
c_pid = in_str.substr( lpos, rpos - lpos );
if ( rpos != std::string::npos ) {
cmd_string = in_str.substr( rpos );
} else {
continue;
}
} else {
continue;
}
client_fifo = "/tmp/led_client_fifo." + c_pid;
/* Open client FIFO (previously created by client) */
client_fd = open( client_fifo.c_str(), O_WRONLY );
if ( client_fd == -1) {
std::cout << "Can`t open clinet fifo " << client_fifo << "\n";
continue;
}
/* Send response and close FIFO */
s_res = cmd_disp.dispatch( cmd_string ) ;
if ( write( client_fd, s_res.data(), s_res.size() ) == -1 ) {
perror("write");
}
if ( close( client_fd ) == -1 )
perror("close");
}
}
Protocol::~Protocol() {
if ( close( server_fd_ ) == -1 || close( dummy_fd_ ) == -1 ) {
perror("close");
}
}