franta-hg@1
|
1 |
/*
|
franta-hg@1
|
2 |
* File: program.cpp
|
franta-hg@1
|
3 |
* Author: fiki
|
franta-hg@1
|
4 |
*
|
franta-hg@1
|
5 |
* Created on 1. duben 2009, 23:54
|
franta-hg@1
|
6 |
*/
|
franta-hg@1
|
7 |
|
franta-hg@1
|
8 |
#include <program.h>
|
franta-hg@1
|
9 |
#include <stdlib.h>
|
franta-hg@1
|
10 |
//#include <iostream>
|
franta-hg@1
|
11 |
#include <c++/4.3.2/iostream>
|
franta-hg@1
|
12 |
#include <mqueue.h>
|
franta-hg@1
|
13 |
#include <readline/readline.h>
|
franta-hg@4
|
14 |
#include <signal.h>
|
franta-hg@1
|
15 |
|
franta-hg@1
|
16 |
using namespace std;
|
franta-hg@1
|
17 |
|
franta-hg@4
|
18 |
/**
|
franta-hg@4
|
19 |
* Spuštění programu „POSIXovéFronty“
|
franta-hg@4
|
20 |
*/
|
franta-hg@2
|
21 |
int main() {
|
franta-hg@1
|
22 |
|
franta-hg@1
|
23 |
cout << "Vítejte v programu POSIXovéFronty" << endl;
|
franta-hg@1
|
24 |
|
franta-hg@4
|
25 |
signal(SIGTERM, zpracujSignal);
|
franta-hg@4
|
26 |
|
franta-hg@2
|
27 |
mq_vlastnosti.mq_maxmsg = POCET_ZPRAV_VE_FRONTE;
|
franta-hg@2
|
28 |
mq_vlastnosti.mq_msgsize = DELKA_ZPRAVY;
|
franta-hg@1
|
29 |
|
franta-hg@2
|
30 |
|
franta-hg@2
|
31 |
//cout << "maxmsg: " << ma.mq_maxmsg;
|
franta-hg@2
|
32 |
//cout << "msgsize: " << ma.mq_msgsize;
|
franta-hg@2
|
33 |
|
franta-hg@2
|
34 |
|
franta-hg@2
|
35 |
|
franta-hg@1
|
36 |
|
franta-hg@1
|
37 |
//cout << NAZEV_FRONTY << endl;
|
franta-hg@1
|
38 |
|
franta-hg@1
|
39 |
if (isOdesilaciRezim()) {
|
franta-hg@1
|
40 |
odesilej();
|
franta-hg@1
|
41 |
} else {
|
franta-hg@1
|
42 |
prijimej();
|
franta-hg@1
|
43 |
}
|
franta-hg@1
|
44 |
|
franta-hg@1
|
45 |
|
franta-hg@1
|
46 |
|
franta-hg@1
|
47 |
|
franta-hg@1
|
48 |
|
franta-hg@1
|
49 |
return (EXIT_SUCCESS);
|
franta-hg@1
|
50 |
}
|
franta-hg@1
|
51 |
|
franta-hg@1
|
52 |
bool isOdesilaciRezim() {
|
franta-hg@1
|
53 |
//char *odpoved = readline("Zvolte režim: (p)řijímací / (o)desílací");
|
franta-hg@1
|
54 |
|
franta-hg@1
|
55 |
cout << "Zvolte režim: (p)řijímací / (o)desílací" << endl;
|
franta-hg@1
|
56 |
|
franta-hg@1
|
57 |
char odpoved = cin.get();
|
franta-hg@1
|
58 |
cin.get();
|
franta-hg@1
|
59 |
|
franta-hg@1
|
60 |
if (odpoved == 'o') {
|
franta-hg@1
|
61 |
return true;
|
franta-hg@1
|
62 |
} else if (odpoved == 'p') {
|
franta-hg@1
|
63 |
return false;
|
franta-hg@1
|
64 |
} else {
|
franta-hg@1
|
65 |
return isOdesilaciRezim();
|
franta-hg@1
|
66 |
}
|
franta-hg@1
|
67 |
}
|
franta-hg@1
|
68 |
|
franta-hg@1
|
69 |
void odesilej() {
|
franta-hg@1
|
70 |
cout << "odesíláme" << endl;
|
franta-hg@1
|
71 |
|
franta-hg@1
|
72 |
|
franta-hg@1
|
73 |
|
franta-hg@2
|
74 |
mqd_t fronta = mq_open(NAZEV_FRONTY, O_RDWR | O_CREAT, S_IRWXU | S_IRWXG, &mq_vlastnosti);
|
franta-hg@2
|
75 |
|
franta-hg@2
|
76 |
char zprava[] = "zpráva";
|
franta-hg@1
|
77 |
|
franta-hg@4
|
78 |
int vysledek = mq_send(fronta, zprava, strlen(zprava) + 1, 0);
|
franta-hg@4
|
79 |
if (vysledek == 0) {
|
franta-hg@4
|
80 |
cout << "Zpráva odeslána" << endl;
|
franta-hg@4
|
81 |
} else {
|
franta-hg@4
|
82 |
cout << "Při odesílání zprávy došlo k chybě" << endl;
|
franta-hg@4
|
83 |
}
|
franta-hg@1
|
84 |
|
franta-hg@4
|
85 |
//cin.get();
|
franta-hg@1
|
86 |
|
franta-hg@1
|
87 |
mq_close(fronta);
|
franta-hg@1
|
88 |
}
|
franta-hg@1
|
89 |
|
franta-hg@1
|
90 |
void prijimej() {
|
franta-hg@1
|
91 |
cout << "přijímáme" << endl;
|
franta-hg@1
|
92 |
|
franta-hg@1
|
93 |
mqd_t fronta = mq_open(NAZEV_FRONTY, O_RDONLY);
|
franta-hg@2
|
94 |
char s[DELKA_ZPRAVY + 1];
|
franta-hg@2
|
95 |
s[DELKA_ZPRAVY] = '\0';
|
franta-hg@2
|
96 |
if (mq_receive(fronta, s, DELKA_ZPRAVY, NULL) > 0) {
|
franta-hg@1
|
97 |
printf("Přijatá zpráva: %s\n", s);
|
franta-hg@1
|
98 |
} else {
|
franta-hg@1
|
99 |
cout << "Nepodařilo se přijmout zprávu" << endl;
|
franta-hg@1
|
100 |
}
|
franta-hg@1
|
101 |
|
franta-hg@1
|
102 |
//mq_unlink(NAZEV_FRONTY);
|
franta-hg@1
|
103 |
mq_close(fronta);
|
franta-hg@4
|
104 |
}
|
franta-hg@1
|
105 |
|
franta-hg@4
|
106 |
void zpracujSignal(int signal) {
|
franta-hg@4
|
107 |
if (signal == SIGTERM) {
|
franta-hg@4
|
108 |
cout << "Končím program na základě signálu " << signal << endl;
|
franta-hg@4
|
109 |
exit(1);
|
franta-hg@4
|
110 |
}
|
franta-hg@4
|
111 |
}
|
franta-hg@1
|
112 |
|