/
hb.c
159 lines (135 loc) · 3.41 KB
/
hb.c
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
/*
* hb.c
* Hit & Blow server core.
*/
#ifdef WIN32
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include "postgres.h"
#include "libpq/pqformat.h"
#include "libpq-fe.h"
#include "utils/elog.h"
#include "util.h"
#include "hb.h"
static void
exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
}
int
hb_main(const char* conninfo)
{
PGconn *conn;
PGresult *res;
PGnotify *notify;
int nnotifies;
int checked;
int result;
char number[5];
char notify_buf[1024];
srand((unsigned)time(NULL));
/* Make a connection to the database */
conn = PQconnectdb(conninfo);
/* Check to see that the backend connection was successfully made */
if (PQstatus(conn) != CONNECTION_OK)
{
elog(WARNING, "Connection to database failed: %s",
PQerrorMessage(conn));
exit_nicely(conn);
}
/*
* Issue LISTEN command to enable notifications from the rule's NOTIFY.
*/
res = PQexec(conn, "LISTEN HB_SV");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
elog(WARNING, "LISTEN command failed: %s", PQerrorMessage(conn));
PQclear(res);
exit_nicely(conn);
}
/*
* should PQclear PGresult whenever it is no longer needed to avoid memory
* leaks
*/
PQclear(res);
/* Set Secret Number */
memset(number, 0x00, 5);
create_random_number(number);
elog(LOG , "hb_worker: set secret number=%s\n", number);
/* Quit after four notifies are received. */
nnotifies = 0;
while (1)
{
/*
* Sleep until something happens on the connection. We use select(2)
* to wait for input, but you could also use poll() or similar
* facilities.
*/
int sock;
fd_set input_mask;
sock = PQsocket(conn);
if (sock < 0)
break; /* shouldn't happen */
FD_ZERO(&input_mask);
FD_SET(sock, &input_mask);
if (select(sock + 1, &input_mask, NULL, NULL, NULL) < 0)
{
elog(WARNING, "select() failed: %s\n", strerror(errno));
exit_nicely(conn);
}
/* Now check for input */
PQconsumeInput(conn);
while ((notify = PQnotifies(conn)) != NULL)
{
checked = check_number(notify->extra);
switch (checked) {
case NUMBER_COMMAND:
result = compare_numbers(number, notify->extra);
if (GET_HITS(result) == 4) {
// Notify Game Clear, and Set new number.
elog(LOG, "hb_worker: NOTIFY HB_CL,'4 Hit! Conguratulatoins!, next new game.'\n");
strcpy(notify_buf, "NOTIFY HB_CL,'4 Hit! Conguratulatoins!, next new game.'");
PQexec(conn, notify_buf);
create_random_number(number);
elog(LOG, "hb_worker: set secret number=%s\n", number);
} else {
// Notify Hit&blow
elog(LOG, "NOTIFY HB_CL,'%d Hit / %d Blow.'",
GET_HITS(result), GET_BLOWS(result));
sprintf(notify_buf, "NOTIFY HB_CL,'%d Hit / %d Blow.'",
GET_HITS(result), GET_BLOWS(result));
PQexec(conn, notify_buf);
}
break;
case START_COMMAND:
// Set New number.
elog(LOG, "hb_worker: Set New number.");
create_random_number(number);
break;
case QUIT_COMMAND:
// nop
break;
case INVALID_COMMAND:
default:
// NOTIFY error status
sprintf(notify_buf, "NOTIFY HB_CL,'Invalid data.(%s)'", notify->extra);
PQexec(conn, notify_buf);
break;
}
PQfreemem(notify);
nnotifies++;
}
}
elog(LOG, "Done.\n");
/* close the connection to the database and cleanup */
PQfinish(conn);
return 0;
}