forked from engineerOfLies/rabbitmqphp_example
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfriends_data_server.php
More file actions
executable file
·67 lines (55 loc) · 2.08 KB
/
friends_data_server.php
File metadata and controls
executable file
·67 lines (55 loc) · 2.08 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
#!/usr/bin/php
<?php
require_once('path.inc');
require_once('get_host_info.inc');
require_once('rabbitMQLib.inc');
require_once('mysqlconnect.php');
function requestProcessor($request) {
if (!isset($request['type'])) {
return ["status" => "error", "message" => "Invalid request"];
}
// process the 'get_friends_data' request
if ($request['type'] === 'get_friends_data') {
return getFriendsData($request['username']);
}
return ["status" => "error", "message" => "Unknown request type"];
}
function getFriendsData($username) {
$db = getDB();
// query for friends list
$friendsQuery = $db->prepare("SELECT friend_username FROM Friends WHERE username = ?");
$friendsQuery->bind_param('s', $username);
$friendsQuery->execute();
$friendsResult = $friendsQuery->get_result();
$friends = [];
while ($row = $friendsResult->fetch_assoc()) {
$friends[] = $row['friend_username'];
}
// query for pending friend requests
$pendingQuery = $db->prepare("SELECT requested_username FROM FriendRequests WHERE username = ? AND status = 'pending'");
$pendingQuery->bind_param('s', $username);
$pendingQuery->execute();
$pendingResult = $pendingQuery->get_result();
$pendingRequests = [];
while ($row = $pendingResult->fetch_assoc()) {
$pendingRequests[] = $row['requested_username'];
}
// query for incoming friend requests
$incomingQuery = $db->prepare("SELECT username FROM FriendRequests WHERE requested_username = ? AND status = 'pending'");
$incomingQuery->bind_param('s', $username);
$incomingQuery->execute();
$incomingResult = $incomingQuery->get_result();
$incomingRequests = [];
while ($row = $incomingResult->fetch_assoc()) {
$incomingRequests[] = $row['username'];
}
return [
"status" => "success",
"friends" => $friends,
"pendingRequests" => $pendingRequests,
"incomingRequests" => $incomingRequests
];
}
$server = new rabbitMQServer("testRabbitMQ.ini", "friendsMQ");
$server->process_requests('requestProcessor');
?>