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@1
|
14 |
|
franta-hg@1
|
15 |
using namespace std;
|
franta-hg@1
|
16 |
|
franta-hg@2
|
17 |
int main() {
|
franta-hg@1
|
18 |
|
franta-hg@1
|
19 |
cout << "Vítejte v programu POSIXovéFronty" << endl;
|
franta-hg@1
|
20 |
|
franta-hg@2
|
21 |
mq_vlastnosti.mq_maxmsg = POCET_ZPRAV_VE_FRONTE;
|
franta-hg@2
|
22 |
mq_vlastnosti.mq_msgsize = DELKA_ZPRAVY;
|
franta-hg@1
|
23 |
|
franta-hg@2
|
24 |
|
franta-hg@2
|
25 |
//cout << "maxmsg: " << ma.mq_maxmsg;
|
franta-hg@2
|
26 |
//cout << "msgsize: " << ma.mq_msgsize;
|
franta-hg@2
|
27 |
|
franta-hg@2
|
28 |
|
franta-hg@2
|
29 |
|
franta-hg@1
|
30 |
|
franta-hg@1
|
31 |
//cout << NAZEV_FRONTY << endl;
|
franta-hg@1
|
32 |
|
franta-hg@1
|
33 |
if (isOdesilaciRezim()) {
|
franta-hg@1
|
34 |
odesilej();
|
franta-hg@1
|
35 |
} else {
|
franta-hg@1
|
36 |
prijimej();
|
franta-hg@1
|
37 |
}
|
franta-hg@1
|
38 |
|
franta-hg@1
|
39 |
|
franta-hg@1
|
40 |
|
franta-hg@1
|
41 |
|
franta-hg@1
|
42 |
|
franta-hg@1
|
43 |
return (EXIT_SUCCESS);
|
franta-hg@1
|
44 |
}
|
franta-hg@1
|
45 |
|
franta-hg@1
|
46 |
bool isOdesilaciRezim() {
|
franta-hg@1
|
47 |
//char *odpoved = readline("Zvolte režim: (p)řijímací / (o)desílací");
|
franta-hg@1
|
48 |
|
franta-hg@1
|
49 |
cout << "Zvolte režim: (p)řijímací / (o)desílací" << endl;
|
franta-hg@1
|
50 |
|
franta-hg@1
|
51 |
char odpoved = cin.get();
|
franta-hg@1
|
52 |
cin.get();
|
franta-hg@1
|
53 |
|
franta-hg@1
|
54 |
if (odpoved == 'o') {
|
franta-hg@1
|
55 |
return true;
|
franta-hg@1
|
56 |
} else if (odpoved == 'p') {
|
franta-hg@1
|
57 |
return false;
|
franta-hg@1
|
58 |
} else {
|
franta-hg@1
|
59 |
return isOdesilaciRezim();
|
franta-hg@1
|
60 |
}
|
franta-hg@1
|
61 |
}
|
franta-hg@1
|
62 |
|
franta-hg@1
|
63 |
void odesilej() {
|
franta-hg@1
|
64 |
cout << "odesíláme" << endl;
|
franta-hg@1
|
65 |
|
franta-hg@1
|
66 |
|
franta-hg@1
|
67 |
|
franta-hg@2
|
68 |
mqd_t fronta = mq_open(NAZEV_FRONTY, O_RDWR | O_CREAT, S_IRWXU | S_IRWXG, &mq_vlastnosti);
|
franta-hg@2
|
69 |
|
franta-hg@2
|
70 |
char zprava[] = "zpráva";
|
franta-hg@1
|
71 |
|
franta-hg@1
|
72 |
mq_send(fronta, zprava, strlen(zprava) + 1, 0);
|
franta-hg@1
|
73 |
|
franta-hg@1
|
74 |
cin.get();
|
franta-hg@1
|
75 |
|
franta-hg@1
|
76 |
mq_close(fronta);
|
franta-hg@1
|
77 |
}
|
franta-hg@1
|
78 |
|
franta-hg@1
|
79 |
void prijimej() {
|
franta-hg@1
|
80 |
cout << "přijímáme" << endl;
|
franta-hg@1
|
81 |
|
franta-hg@1
|
82 |
mqd_t fronta = mq_open(NAZEV_FRONTY, O_RDONLY);
|
franta-hg@2
|
83 |
char s[DELKA_ZPRAVY + 1];
|
franta-hg@2
|
84 |
s[DELKA_ZPRAVY] = '\0';
|
franta-hg@2
|
85 |
if (mq_receive(fronta, s, DELKA_ZPRAVY, NULL) > 0) {
|
franta-hg@1
|
86 |
printf("Přijatá zpráva: %s\n", s);
|
franta-hg@1
|
87 |
} else {
|
franta-hg@1
|
88 |
cout << "Nepodařilo se přijmout zprávu" << endl;
|
franta-hg@1
|
89 |
}
|
franta-hg@1
|
90 |
|
franta-hg@1
|
91 |
//mq_unlink(NAZEV_FRONTY);
|
franta-hg@1
|
92 |
mq_close(fronta);
|
franta-hg@1
|
93 |
|
franta-hg@1
|
94 |
|
franta-hg@1
|
95 |
}
|