Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added the 'SCORE' command to the daemon, client and php extension. Mi…

…sc code cleanup.
  • Loading branch information...
commit eb12bd6c3a42a9b9614f0ab9c788c433477bd741 1 parent efffb40
@ngerakines authored
View
16 README
@@ -87,6 +87,18 @@ When there are no more items to return a '-1' is returned.
C: NEXT\r\n
S: +-1\r\n
+'SCORE <item id>'
+
+Return the score of a given item.
+
+ C: SCORE 61231\r\n
+ S: +5\r\n
+
+If the item does not exist or have a score then a '-1' is returned.
+
+ C: SCORE 61231\r\n
+ S: +-1\r\n
+
'INFO'
Return some server stats. This command deviates from the standard response
@@ -104,7 +116,3 @@ format by returning a list of key-value pairs separated by a ':'.
S: updates:9742851\r\n
S: items:2132931\r\n
S: pools:47831\r\n
-
-# TODO
-
- * Add support for the 'GET' command which returns the priority of an item.
View
28 clients/php/barbershop.c
@@ -44,6 +44,7 @@ static zend_function_entry barbershop_functions[] = {
PHP_ME(Barbershop, next, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Barbershop, peak, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Barbershop, update, NULL, ZEND_ACC_PUBLIC)
+ PHP_ME(Barbershop, score, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Barbershop, info, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(Barbershop, open, connect, NULL, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
@@ -543,6 +544,33 @@ PHP_METHOD(Barbershop, update) {
RETURN_STRINGL(response, response_len, 0);
}
+PHP_METHOD(Barbershop, score) {
+ zval *object;
+ BarbershopSock *barbershop_sock;
+ char *key = NULL, *cmd, *response;
+ int key_len, cmd_len, response_len;
+
+ if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &object, barbershop_ce, &key, &key_len) == FAILURE) {
+ RETURN_FALSE;
+ }
+
+ if (barbershop_sock_get(object, &barbershop_sock TSRMLS_CC) < 0) {
+ RETURN_FALSE;
+ }
+
+ cmd_len = barbershop_cmd_format(&cmd, "SCORE %s\r\n", key, key_len);
+ if (barbershop_sock_write(barbershop_sock, cmd, cmd_len) < 0) {
+ efree(cmd);
+ RETURN_FALSE;
+ }
+ efree(cmd);
+
+ if ((response = barbershop_sock_read(barbershop_sock, &response_len TSRMLS_CC)) == NULL) {
+ RETURN_FALSE;
+ }
+ RETURN_STRINGL(response, response_len, 0);
+}
+
PHP_METHOD(Barbershop, next) {
zval *object;
BarbershopSock *barbershop_sock;
View
3  clients/php/php_barbershop.h
@@ -26,10 +26,9 @@ THE SOFTWARE.
PHP_METHOD(Barbershop, __construct);
PHP_METHOD(Barbershop, connect);
PHP_METHOD(Barbershop, close);
-PHP_METHOD(Barbershop, get);
-PHP_METHOD(Barbershop, set);
PHP_METHOD(Barbershop, info);
PHP_METHOD(Barbershop, update);
+PHP_METHOD(Barbershop, score);
PHP_METHOD(Barbershop, next);
PHP_METHOD(Barbershop, peak);
View
2  clients/php/tests/basic.phpt
@@ -7,6 +7,7 @@ var_dump($bs);
var_dump($bs->connect('127.0.0.1', 8002));
var_dump($bs->update('5000', 1));
var_dump($bs->peak());
+var_dump($bs->score('5000'));
var_dump($bs->next());
var_dump($bs->next());
?>
@@ -16,5 +17,6 @@ object(Barbershop)#1 (0) {
bool(true)
string(3) "+OK"
string(5) "+5000"
+string(3) "+1"
string(5) "+5000"
string(3) "+-1"
View
23 src/barbershop.c
@@ -73,7 +73,7 @@ static size_t tokenize_command(char *command, token_t *tokens, const size_t max_
void on_read(int fd, short ev, void *arg) {
struct client *client = (struct client *)arg;
- char buf[64]; // largest possible command is 'update 2147483647 2147483647'
+ char buf[64];
int len = read(fd, buf, sizeof(buf));
if (len == 0) {
close(fd);
@@ -101,8 +101,11 @@ void on_read(int fd, short ev, void *arg) {
int item_id = atoi(tokens[KEY_TOKEN].value);
int score = atoi(tokens[VALUE_TOKEN].value);
- // Score should probably be type-checked to assert that it really is
- // an unsigned 32bit integer.
+ if (item_id == 0) {
+ reply(fd, "-ERROR INVALID ITEM ID\r\n");
+ return;
+ }
+
if (score < 1) {
reply(fd, "-ERROR INVALID SCORE\r\n");
return;
@@ -152,6 +155,20 @@ void on_read(int fd, short ev, void *arg) {
char msg[32];
sprintf(msg, "+%d\r\n", next);
reply(fd, msg);
+ } else if (ntokens == 3 && strcmp(tokens[COMMAND_TOKEN].value, "SCORE") == 0) {
+ int item_id = atoi(tokens[KEY_TOKEN].value);
+ if (item_id == 0) {
+ reply(fd, "-ERROR INVALID ITEM ID\r\n");
+ return;
+ }
+ Position lookup = Find(item_id, items);
+ if (lookup == NULL) {
+ reply(fd, "+-1\r\n");
+ } else {
+ char msg[32];
+ sprintf(msg, "+%d\r\n", lookup->score);
+ reply(fd, msg);
+ }
} else if (ntokens == 2 && strcmp(tokens[COMMAND_TOKEN].value, "INFO") == 0) {
char out[128];
time_t current_time;
View
20 src/client.c
@@ -30,12 +30,6 @@ Copyright (c) 2010 Nick Gerakines <nick at gerakines dot net>
#include <signal.h>
#include <unistd.h>
-const int UNKNOWN = 0;
-const int UPDATE = 1;
-const int NEXT = 2;
-const int PEAK = 3;
-const int INFO = 4;
-
void send_command(int sd, char *command);
int main(int argc, char **argv) {
@@ -117,6 +111,15 @@ int main(int argc, char **argv) {
action = 4;
}
+ if (strcmp(argv[optind], "score") == 0) {
+ if (argc - optind != 2) {
+ printf("The 'score' command requires 1 command parameter.\n");
+ printf("usage: client [--ip=] [--port=] score <item id>\n");
+ exit(1);
+ }
+ action = 5;
+ }
+
if (action == 0) {
printf("Invalid command given, should be either update, next, peak or info.\n");
printf("usage: client [--ip=] [--port=] <command> [... command arguments]\n");
@@ -167,6 +170,11 @@ int main(int argc, char **argv) {
case 4:
send_command(sd, "INFO\r\n");
break;
+ case 5:
+ item_id = atoi(argv[optind + 1]);
+ sprintf(msg, "SCORE %d\r\n", item_id);
+ send_command(sd, msg);
+ break;
default:
break;
}
Please sign in to comment.
Something went wrong with that request. Please try again.