1
|
|
|
#!/usr/bin/php |
2
|
|
|
|
3
|
|
|
<?php |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This is an example of a client application listening for SQS messages |
7
|
|
|
* The CPE stack sends output message back to the client using the "output" SQS queue |
8
|
|
|
* The application needs to listen to this output queue to get updates |
9
|
|
|
* Using the CpeClientSdk you can easily listen to the SQS |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
require __DIR__ . "/vendor/autoload.php"; |
13
|
|
|
|
14
|
|
|
function poll_SQS_queues($CpeClientSdk, $decodedClient) |
15
|
|
|
{ |
16
|
|
|
try { |
17
|
|
|
// Will poll for 2 seconds |
18
|
|
|
if ($msg = $CpeClientSdk->receive_message($decodedClient, 10)) |
19
|
|
|
{ |
20
|
|
|
if (!($decoded = json_decode($msg['Body']))) { |
21
|
|
|
throw new Exception("JSON output data is invalid!"); |
22
|
|
|
} else { |
23
|
|
|
handle_output($decoded); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
} catch (Exception $e) { |
27
|
|
|
print("[ERROR] " . $e->getMessage() . "\n"); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
// Message polled. We delete it from SQS |
31
|
|
|
if (isset($msg) && $msg != "") { |
32
|
|
|
$CpeClientSdk->delete_message($decodedClient, $msg); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
function handle_output($output) |
37
|
|
|
{ |
38
|
|
|
global $debug; |
39
|
|
|
|
40
|
|
|
if ($debug) { |
41
|
|
|
print_r($output); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* CLIENT POLLER START |
47
|
|
|
*/ |
48
|
|
|
|
49
|
|
|
$debug = false; |
50
|
|
|
$region = getenv("AWS_DEFAULT_REGION"); |
51
|
|
|
$key = getenv("AWS_ACCESS_KEY_ID"); |
52
|
|
|
$secret = getenv("AWS_SECRET_KEY"); |
53
|
|
|
|
54
|
|
View Code Duplication |
function usage() |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
echo("Usage: php " . basename(__FILE__) . " -c configFile [-h] [-k <key>] [-s <secret>] [-r <region>]\n"); |
57
|
|
|
echo("-h: Print this help\n"); |
58
|
|
|
echo("-d: Debug mode\n"); |
59
|
|
|
echo("-c: configFile\n"); |
60
|
|
|
echo("-k <AWS key>: Optional. Will use env variables by default\n"); |
61
|
|
|
echo("-s <AWS secret>: Optional. Will use env variables by default\n"); |
62
|
|
|
echo("-r <AWS region>: Optional. Will use env variables by default\n"); |
63
|
|
|
exit(0); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
function check_input_parameters() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
global $region; |
69
|
|
|
global $secret; |
70
|
|
|
global $key; |
71
|
|
|
global $debug; |
72
|
|
|
global $clientInfo; |
73
|
|
|
global $argv; |
74
|
|
|
|
75
|
|
|
// Handle input parameters |
76
|
|
|
if (!($options = getopt("c:k::s::r::hd"))) { |
77
|
|
|
usage(); |
78
|
|
|
} |
79
|
|
|
if (isset($options['h'])) { |
80
|
|
|
usage(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (isset($options['d'])) { |
84
|
|
|
$debug = true; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (isset($options['c'])) |
88
|
|
|
{ |
89
|
|
|
$clientConfFile = $options['c']; |
90
|
|
|
if (!file_exists($clientConfFile)) { |
91
|
|
|
throw new Exception("The client config file is not valid!"); |
92
|
|
|
} |
93
|
|
|
if (!($clientInfo = file_get_contents($clientConfFile))) { |
94
|
|
|
throw new Exception("Unable to read the file"); |
95
|
|
|
} |
96
|
|
|
} else { |
97
|
|
|
throw new Exception("Please provide the client config file!\n"); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (isset($options['k'])) { |
101
|
|
|
$key = $options['k']; |
102
|
|
|
} else { |
103
|
|
|
$key = getenv("AWS_ACCESS_KEY_ID"); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (isset($options['s'])) { |
107
|
|
|
$secret = $options['s']; |
108
|
|
|
} else { |
109
|
|
|
$secret = getenv("AWS_SECRET_KEY"); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (isset($options['r'])) { |
113
|
|
|
$region = $options['r']; |
114
|
|
|
} else { |
115
|
|
|
$region = getenv("AWS_DEFAULT_REGION"); |
116
|
|
|
} |
117
|
|
|
if (!$region) { |
118
|
|
|
throw new Exception("Please provide your AWS region as parameter or using AWS_DEFAULT_REGION env var !"); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// Instanciate ComSDK to communicate with the stack |
123
|
|
|
try { |
124
|
|
|
check_input_parameters(); |
125
|
|
|
$CpeClientSdk = new SA\CpeClientSdk($key, $secret, $region, $debug); |
126
|
|
|
} catch (Exception $e) { |
127
|
|
|
exit($e->getMessage()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// You must JSON decode it |
131
|
|
|
$decodedClient = json_decode($clientInfo); |
132
|
|
|
|
133
|
|
|
// Keep polling for output messages! |
134
|
|
|
while (42) { |
135
|
|
|
poll_SQS_queues($CpeClientSdk, $decodedClient); |
136
|
|
|
} |
137
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.