Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix some usability issues

1) Prohibit re-granting an already granted ticket.
2) Revoke command will be waiting until the operation was executed.
3) Improve log output for connect_nonb function.

Signed-off-by: Dongmao Zhang <dmzhang@suse.com>
  • Loading branch information...
commit a2bbdbd368120d8a518ad8dd54955be7fa283296 1 parent 84b6583
@thesues thesues authored committed
View
1  src/booth.h
@@ -67,6 +67,7 @@ typedef enum {
BOOTHC_RLT_SYNC_FAIL,
BOOTHC_RLT_INVALID_ARG,
BOOTHC_RLT_REMOTE_OP,
+ BOOTHC_RLT_OVERGRANT,
} cmd_result_t;
struct client {
View
124 src/main.c
@@ -34,6 +34,9 @@
#include <fcntl.h>
#include <string.h>
#include <assert.h>
+#include <error.h>
+#include <sys/ioctl.h>
+#include <termios.h>
#include "log.h"
#include "booth.h"
#include "config.h"
@@ -41,7 +44,6 @@
#include "timer.h"
#include "pacemaker.h"
#include "ticket.h"
-#include <error.h>
#define RELEASE_VERSION "1.0"
@@ -268,6 +270,7 @@ void process_connection(int ci)
struct boothc_header h;
char *data = NULL;
char *site, *ticket;
+ int ticket_owner;
int local, rv;
void (*deadfn) (int ci);
@@ -275,15 +278,11 @@ void process_connection(int ci)
if (rv < 0) {
if (errno == ECONNRESET)
- log_debug("client %d connection reset for fd %d", ci, client[ci].fd);
- else
- log_debug("client %d read failed for fd %d: %s (%d)",
- ci, client[ci].fd,
- strerror(errno), errno);
+ log_debug("client %d connection reset for fd %d",
+ ci, client[ci].fd);
deadfn = client[ci].deadfn;
if(deadfn) {
- log_debug("run deadfn");
deadfn(ci);
}
return;
@@ -322,10 +321,26 @@ void process_connection(int ci)
h.len = 0;
site = data;
ticket = data + BOOTH_NAME_LEN;
+
if (!check_ticket(ticket)) {
h.result = BOOTHC_RLT_INVALID_ARG;
goto reply;
}
+
+ if (get_ticket_info(ticket, &ticket_owner, NULL) == 0) {
+ if (ticket_owner > -1) {
+ log_error("client want to get an granted "
+ "ticket %s", ticket);
+ h.result = BOOTHC_RLT_OVERGRANT;
+ goto reply;
+ }
+
+ } else {
+ log_error("can not get ticket %s's info", ticket);
+ h.result = BOOTHC_RLT_INVALID_ARG;
+ goto reply;
+ }
+
if (!check_site(site, &local)) {
h.result = BOOTHC_RLT_INVALID_ARG;
goto reply;
@@ -558,6 +573,56 @@ static int do_list(void)
return rv;
}
+static inline void load_bar(int x, int n, int r, int w)
+{
+ int i;
+ float ratio;
+ int c;
+
+ /* Only update r times.*/
+ if ( x % (n / r) != 0 ) return;
+
+ /* Calculuate the ratio of complete-to-incomplete.*/
+ ratio = x / (float)n;
+ c = ratio * w;
+
+ /* Show the percentage complete.*/
+ printf("%3d%% [", (int)(ratio * 100) );
+
+ /* Show the load bar.*/
+ for (i = 0; i < c; i++)
+ printf("=");
+ for (i = c; i < w; i++)
+ printf(" ");
+
+ printf("]");
+ printf("\r");
+ fflush(stdout);
+}
+
+static void counting_down(int total_time)
+{
+ struct winsize size;
+ int screen_width;
+ int i;
+
+ ioctl(STDIN_FILENO, TIOCGWINSZ, (char*)&size);
+ screen_width = size.ws_col/(float)2;
+
+ /* ignore signals */
+ signal(SIGTERM, SIG_IGN);
+ signal(SIGINT, SIG_IGN);
+ signal(SIGHUP, SIG_IGN);
+
+ i = 0;
+ while (i <= total_time) {
+ load_bar(i, total_time, total_time, screen_width);
+ sleep(1);
+ i++;
+ }
+
+ log_info("\nCounting Down Over...\n");
+}
static int do_command(cmd_request_t cmd)
{
char *buf;
@@ -566,6 +631,10 @@ static int do_command(cmd_request_t cmd)
uint32_t force = 0;
int fd, rv;
+ int expire_time;
+ int i;
+
+
buflen = sizeof(struct boothc_header) +
sizeof(cl.site) + sizeof(cl.ticket);
buf = malloc(buflen);
@@ -601,6 +670,14 @@ static int do_command(cmd_request_t cmd)
goto out_close;
}
+ if (reply.result == BOOTHC_RLT_OVERGRANT) {
+ log_info("You're granting a granted ticket"
+ "If you wanted to migrate a ticket,"
+ "use revoke first, then use grant");
+ rv = -1;
+ goto out_close;
+ }
+
if (reply.result == BOOTHC_RLT_REMOTE_OP) {
struct booth_node to;
int s;
@@ -632,12 +709,41 @@ static int do_command(cmd_request_t cmd)
log_info("grant command sent, result will be returned "
"asynchronously, you can get the result from "
"the log files");
- else if (cmd == BOOTHC_CMD_REVOKE)
+ else if (cmd == BOOTHC_CMD_REVOKE) {
log_info("revoke command sent, result will be returned "
"asynchronously, you can get the result from "
"the log files after the ticket expiry time.");
+ i = 0;
+ /* FIXME: if we access the server then get the actual
+ * remaining time the waiting will be shorter, for now,
+ * client is just waiting the expiry time.
+ */
+ read_config(cl.configfile);
+ while (i < booth_conf->ticket_count) {
+ if (!strncmp(booth_conf->ticket[i].name, cl.ticket,
+ BOOTH_NAME_LEN)) {
+ expire_time = booth_conf->ticket[i].expiry;
+ log_info("You have to wait %d seconds to "
+ "ensure all timer has expired!",
+ expire_time);
+ counting_down(expire_time);
+ rv = 0;
+ break;
+ }
+ i++;
+ /* no ticket found in conf file */
+ if( i == booth_conf->ticket_count ) {
+ log_error("check your config file, "
+ "ticket %s not found", cl.ticket);
+ log_error("your booth's config file may "
+ "not be the same!");
+ break;
+ rv = -1;
+ }
+ }
+ }
else
- log_error("internal error reading reply result!");
+ log_error("internal error when reading reply result!");
rv = 0;
} else if (reply.result == BOOTHC_RLT_SYNC_SUCC) {
if (cmd == BOOTHC_CMD_GRANT)
View
17 src/ticket.c
@@ -441,6 +441,23 @@ int revoke_ticket(char *ticket, int force)
}
}
+int get_ticket_info(char *name, int *owner, int *expires)
+{
+ struct ticket *tk;
+
+ list_for_each_entry(tk, &ticket_list, list) {
+ if (!strncmp(tk->id, name, BOOTH_NAME_LEN + 1)) {
+ if(owner)
+ *owner = tk->owner;
+ if(expires)
+ *expires = tk->expires;
+ return 0;
+ }
+ }
+
+ return -1;
+}
+
int list_ticket(char **pdata, unsigned int *len)
{
struct ticket *tk;
View
1  src/ticket.h
@@ -29,5 +29,6 @@ int list_ticket(char **pdata, unsigned int *len);
int catchup_ticket(char **pdata, unsigned int len);
int ticket_recv(void *msg, int msglen);
int setup_ticket(void);
+int get_ticket_info(char *name, int *owner, int *expires);
#endif /* _TICKET_H */
View
6 src/transport.c
@@ -404,7 +404,11 @@ static int booth_tcp_open(struct booth_node *to)
rv = connect_nonb(s, (struct sockaddr *)&sockaddr, addrlen, 10);
if (rv == -1) {
- log_error("connection to %s timeout", to->addr);
+ if( errno == ETIMEDOUT)
+ log_error("connection to %s timeout", to->addr);
+ else
+ log_error("connection to %s error %s", to->addr,
+ strerror(errno));
close(s);
return rv;
}
Please sign in to comment.
Something went wrong with that request. Please try again.