GUAC-236: Handle specific instructions via an opcode/handler mapping.

This commit is contained in:
Michael Jumper 2016-02-26 16:59:55 -08:00
parent 1f54ea10dd
commit a926a6dc3b
4 changed files with 186 additions and 10 deletions

View File

@ -24,13 +24,15 @@ AUTOMAKE_OPTIONS = foreign
bin_PROGRAMS = guacenc bin_PROGRAMS = guacenc
noinst_HEADERS = \ noinst_HEADERS = \
encode.h \ encode.h \
instructions.h \
log.h log.h
guacenc_SOURCES = \ guacenc_SOURCES = \
encode.c \ encode.c \
guacenc.c \ guacenc.c \
instructions.c \
log.c log.c
guacenc_CFLAGS = \ guacenc_CFLAGS = \

View File

@ -21,6 +21,7 @@
*/ */
#include "config.h" #include "config.h"
#include "instructions.h"
#include "log.h" #include "log.h"
#include <guacamole/client.h> #include <guacamole/client.h>
@ -57,12 +58,10 @@ static int guacenc_read_instructions(const char* path, guac_socket* socket) {
if (parser == NULL) if (parser == NULL)
return 1; return 1;
/* Continuously read instructions */ /* Continuously read and handle all instructions */
while (!guac_parser_read(parser, socket, -1)) { while (!guac_parser_read(parser, socket, -1)) {
guacenc_handle_instruction(parser->opcode, parser->argc,
/* STUB: Handle instruction */ (const char**) parser->argv);
guacenc_log(GUAC_LOG_DEBUG, "STUB: \"%s\" ...", parser->opcode);
} }
/* Fail on read/parse error */ /* Fail on read/parse error */

View File

@ -0,0 +1,77 @@
/*
* Copyright (C) 2016 Glyptodon, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "config.h"
#include "instructions.h"
#include "log.h"
#include <guacamole/client.h>
#include <string.h>
guacenc_instruction_handler_mapping guacenc_instruction_handler_map[] = {
{"blob", NULL},
{"img", NULL},
{"end", NULL},
{"cursor", NULL},
{"copy", NULL},
{"transfer", NULL},
{"size", NULL},
{"rect", NULL},
{"cfill", NULL},
{"move", NULL},
{"shade", NULL},
{"dispose", NULL},
{NULL, NULL}
};
int guacenc_handle_instruction(const char* opcode,
int argc, const char** argv) {
/* Search through mapping for instruction handler having given opcode */
guacenc_instruction_handler_mapping* current = guacenc_instruction_handler_map;
while (current->opcode != NULL) {
/* Invoke handler if opcode matches (if defined) */
if (strcmp(current->opcode, opcode) == 0) {
/* Invoke defined handler */
guacenc_instruction_handler* handler = current->handler;
if (handler != NULL)
return handler(argc, argv);
/* Log defined but unimplemented instructions */
guacenc_log(GUAC_LOG_DEBUG, "\"%s\" not implemented", opcode);
return 0;
}
/* Next candidate handler */
current++;
} /* end opcode search */
/* Ignore any unknown instructions */
return 0;
}

View File

@ -0,0 +1,98 @@
/*
* Copyright (C) 2016 Glyptodon, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef GUACENC_INSTRUCTIONS_H
#define GUACENC_INSTRUCTIONS_H
#include "config.h"
/**
* A callback function which, when invoked, handles a particular Guacamole
* instruction. The opcode of the instruction is implied (as it is expected
* that there will be a 1:1 mapping of opcode to callback function), while the
* arguments for that instruction are included in the parameters given to the
* callback.
*
* @param argc
* The number of arguments (excluding opcode) passed to the instruction
* being handled by the callback.
*
* @param argv
* All arguments (excluding opcode) associated with the instruction being
* handled by the callback.
*
* @return
* Zero if the instruction was handled successfully, non-zero if an error
* occurs.
*/
typedef int guacenc_instruction_handler(int argc, const char** argv);
/**
* Mapping of instruction opcode to corresponding handler function.
*/
typedef struct guacenc_instruction_handler_mapping {
/**
* The opcode of the instruction that the associated handler function
* should be invoked for.
*/
const char* opcode;
/**
* The handler function to invoke whenever an instruction having the
* associated opcode is parsed.
*/
guacenc_instruction_handler* handler;
} guacenc_instruction_handler_mapping;
/**
* Array of all opcode/handler mappings for all supported opcodes, terminated
* by an entry with a NULL opcode. All opcodes not listed here can be safely
* ignored.
*/
extern guacenc_instruction_handler_mapping guacenc_instruction_handler_map[];
/**
* Handles the instruction having the given opcode and arguments, encoding the
* result to the in-progress video.
*
* @param opcode
* The opcode of the instruction being handled.
*
* @param argc
* The number of arguments (excluding opcode) passed to the instruction
* being handled by the callback.
*
* @param argv
* All arguments (excluding opcode) associated with the instruction being
* handled by the callback.
*
* @return
* Zero if the instruction was handled successfully, non-zero if an error
* occurs.
*/
int guacenc_handle_instruction(const char* opcode, int argc,
const char** argv);
#endif