You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@guacamole.apache.org by GitBox <gi...@apache.org> on 2020/07/02 02:20:18 UTC

[GitHub] [guacamole-server] mike-jumper opened a new pull request #297: GUACAMOLE-221: Add convenience API for automatically handling received "argv" streams.

mike-jumper opened a new pull request #297:
URL: https://github.com/apache/guacamole-server/pull/297


   This change adds a new header to libguac, `argv.h`, which provides a series of convenience functions for automatically handling "argv" streams:
   
   * `guac_argv_register()` - Registers a callback which should be invoked when the full, updated value of an argument has been received via an "argv" stream. Option flags can be specified to declare that a particular callback should be invoked only once, or that received values should be echoed back to all users of the connection.
   * `guac_argv_received()` - Hands over control of a received "argv" stream such that registered callbacks will automatically be invoked.
   * `guac_argv_handler` - Convenient implementation of the "argv" instruction handler which automatically handles sending of "ack" instructions and invokes `guac_argv_received()` appropriately.
   
   In general, `guac_argv_register()` and `guac_argv_handler` should be sufficient to replace all current usage of the "argv" stream. For handling of "argv" streams that must block the connection, two additional functions are provided:
   
   * `guac_argv_await()` - Blocks until each of the provided NULL-terminated list of arguments has been received.
   * `guac_argv_stop()` - Safely unblocks any `guac_argv_await()` that are currently blocked, and prevents any future calls from blocking.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [guacamole-server] necouchman merged pull request #297: GUACAMOLE-221: Add convenience API for automatically handling received "argv" streams.

Posted by GitBox <gi...@apache.org>.
necouchman merged pull request #297:
URL: https://github.com/apache/guacamole-server/pull/297


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [guacamole-server] mike-jumper commented on a change in pull request #297: GUACAMOLE-221: Add convenience API for automatically handling received "argv" streams.

Posted by GitBox <gi...@apache.org>.
mike-jumper commented on a change in pull request #297:
URL: https://github.com/apache/guacamole-server/pull/297#discussion_r450558885



##########
File path: src/libguac/argv.c
##########
@@ -0,0 +1,350 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "config.h"
+
+#include "guacamole/argv.h"
+#include "guacamole/client.h"
+#include "guacamole/protocol.h"
+#include "guacamole/socket.h"
+#include "guacamole/stream.h"
+#include "guacamole/string.h"
+#include "guacamole/user.h"
+
+#include <pthread.h>
+#include <stdlib.h>
+#include <string.h>
+
+/**
+ * The state of an argument that will be automatically processed. Note that
+ * this is distinct from the state of an argument value that is currently being
+ * processed. Argument value states are dynamically-allocated and scoped by the
+ * associated guac_stream.
+ */
+typedef struct guac_argv_state {
+
+    /**
+     * The name of the argument.
+     */
+    char name[GUAC_ARGV_MAX_NAME_LENGTH];
+
+    /**
+     * Whether at least one value for this argument has been received since it
+     * was registered.
+     */
+    int received;
+
+    /**
+     * Bitwise OR of all option flags that should affect processing of this
+     * argument.
+     */
+    int options;
+
+    /**
+     * The callback that should be invoked when a new value for the associated
+     * argument has been received. If the GUAC_ARGV_OPTION_ONCE flag is set,
+     * the callback will be invoked at most once.
+     */
+    guac_argv_callback* callback;
+
+    /**
+     * The arbitrary data that should be passed to the callback.
+     */
+    void* data;
+
+} guac_argv_state;
+
+/**
+ * The current state of automatic processing of "argv" streams.
+ */
+typedef struct guac_argv_await_state {
+
+    /**
+     * Whether automatic argument processing has been stopped via a call to
+     * guac_argv_stop().
+     */
+    int stopped;
+
+    /**
+     * The total number of arguments registered.
+     */
+    unsigned int num_registered;
+
+    /**
+     * All registered arguments and their corresponding callbacks.
+     */
+    guac_argv_state registered[GUAC_ARGV_MAX_REGISTERED];
+
+    /**
+     * Lock which protects multi-threaded access to this entire state
+     * structure, including the condition that signals specific modifications
+     * to the structure.
+     */
+    pthread_mutex_t lock;
+
+    /**
+     * Condition which is signalled whenever the overall state of "argv"
+     * processing changes, either through the receipt of a new argument value
+     * or due to a call to guac_argv_stop().
+     */
+    pthread_cond_t changed;
+
+} guac_argv_await_state;
+
+/**
+ * The value or current status of a connection parameter received over an
+ * "argv" stream.
+ */
+typedef struct guac_argv {
+
+    /**
+     * The state of the specific setting being updated.
+     */
+    guac_argv_state* state;
+
+    /**
+     * The mimetype of the data being received.
+     */
+    char mimetype[GUAC_ARGV_MAX_MIMETYPE_LENGTH];
+
+    /**
+     * Buffer space for containing the received argument value.
+     */
+    char buffer[GUAC_ARGV_MAX_LENGTH];
+
+    /**
+     * The number of bytes received so far.
+     */
+    int length;
+
+} guac_argv;
+
+/**
+ * Statically-allocated, shared state of the guac_argv_*() family of functions.
+ */
+static guac_argv_await_state await_state = {
+    .lock = PTHREAD_MUTEX_INITIALIZER,
+    .changed = PTHREAD_COND_INITIALIZER
+};
+
+/**
+ * Returns whether at least one value for each of the provided arguments has
+ * been received.
+ *
+ * @param args
+ *     A NULL-terminated array of the names of all arguments to test.
+ *
+ * @return
+ *     Non-zero if at least one value has been received for each of the
+ *     provided arguments, zero otherwise.
+ */
+static int guac_argv_is_received(const char** args) {
+
+    for (int i = 0; i < await_state.num_registered; i++) {
+
+        /* Ignore all received arguments */
+        guac_argv_state* state = &await_state.registered[i];
+        if (state->received)
+            continue;
+
+        /* Fail immediately for any matching non-received arguments */
+        for (const char** arg = args; *arg != NULL; arg++) {
+            if (strcmp(state->name, *arg) == 0)
+                return 0;
+        }
+
+    }
+
+    /* All arguments were received */
+    return 1;
+
+}
+
+int guac_argv_register(const char* name, guac_argv_callback* callback, void* data, int options) {
+
+    pthread_mutex_lock(&await_state.lock);
+
+    if (await_state.num_registered == GUAC_ARGV_MAX_REGISTERED) {
+        pthread_mutex_unlock(&await_state.lock);
+        return 1;
+    }
+
+    guac_argv_state* state = &await_state.registered[await_state.num_registered++];
+    guac_strlcpy(state->name, name, sizeof(state->name));
+    state->options = options;
+    state->callback = callback;
+    state->data = data;
+
+    pthread_mutex_unlock(&await_state.lock);
+    return 0;
+
+}
+
+int guac_argv_await(const char** args) {
+
+    /* Wait for all requested arguments to be received (or for receipt to be
+     * stopped) */
+    pthread_mutex_lock(&await_state.lock);
+    while (!await_state.stopped && !guac_argv_is_received(args))
+        pthread_cond_wait(&await_state.changed, &await_state.lock);
+
+    /* Arguments were successfully received only if receipt was not stopped */
+    int retval = await_state.stopped;
+    pthread_mutex_unlock(&await_state.lock);
+    return retval;
+
+}
+
+/**
+ * Handler for "blob" instructions which appends the data from received blobs
+ * to the end of the in-progress argument value buffer.
+ *
+ * @see guac_user_blob_handler
+ */
+static int guac_argv_blob_handler(guac_user* user, guac_stream* stream,
+        void* data, int length) {
+
+    guac_argv* argv = (guac_argv*) stream->data;
+
+    /* Calculate buffer size remaining, including space for null terminator,
+     * adjusting received length accordingly */
+    int remaining = sizeof(argv->buffer) - argv->length - 1;
+    if (length > remaining)
+        length = remaining;
+
+    /* Append received data to end of buffer */
+    memcpy(argv->buffer + argv->length, data, length);
+    argv->length += length;
+
+    return 0;
+
+}
+
+/**
+ * Handler for "end" instructions which applies the changes specified by the
+ * argument value buffer associated with the stream.
+ *
+ * @see guac_user_end_handler
+ */
+static int guac_argv_end_handler(guac_user* user, guac_stream* stream) {
+
+    int result = 0;
+
+    /* Append null terminator to value */
+    guac_argv* argv = (guac_argv*) stream->data;
+    argv->buffer[argv->length] = '\0';
+
+    pthread_mutex_lock(&await_state.lock);
+
+    /* Invoke callback, limiting to a single invocation if
+     * GUAC_ARGV_OPTION_ONCE applies */
+    guac_argv_state* state = argv->state;
+    if (!(state->options & GUAC_ARGV_OPTION_ONCE) || !state->received) {
+        if (state->callback != NULL)
+            result = state->callback(user, argv->mimetype, state->name, argv->buffer, state->data);
+    }
+
+    /* Alert connected clients regarding newly-accepted values if echo is
+     * enabled */
+    if (!result && (state->options & GUAC_ARGV_OPTION_ECHO)) {
+        guac_client* client = user->client;
+        guac_client_stream_argv(client, client->socket, argv->mimetype, state->name, argv->buffer);
+    }
+
+    /* Notify that argument has been received */
+    state->received = 1;
+    pthread_cond_broadcast(&await_state.changed);
+
+    pthread_mutex_unlock(&await_state.lock);
+
+    free(argv);
+    return 0;
+
+}
+
+int guac_argv_received(guac_stream* stream, const char* mimetype, const char* name) {
+
+    pthread_mutex_lock(&await_state.lock);
+
+    for (int i = 0; i < await_state.num_registered; i++) {
+
+        /* Ignore any arguments that have already been received if they are
+         * declared as being acceptable only once */
+        guac_argv_state* state = &await_state.registered[i];
+        if ((state->options & GUAC_ARGV_OPTION_ONCE) && state->received)
+            continue;
+
+        /* Argument matched */
+        if (strcmp(state->name, name) == 0) {
+
+            guac_argv* argv = malloc(sizeof(guac_argv));
+            guac_strlcpy(argv->mimetype, mimetype, sizeof(argv->mimetype));
+            argv->state = state;
+            argv->length = 0;
+
+            stream->data = argv;
+            stream->blob_handler = guac_argv_blob_handler;
+            stream->end_handler = guac_argv_end_handler;
+
+            pthread_mutex_unlock(&await_state.lock);
+            return 0;
+
+        }
+
+    }
+
+    /* No such argument awaiting processing */
+    pthread_mutex_unlock(&await_state.lock);
+    return 1;
+
+}
+
+void guac_argv_stop() {
+    pthread_mutex_lock(&await_state.lock);
+
+    /* Signal any waiting threads that no further argument values will be
+     * received */
+    if (!await_state.stopped) {
+        await_state.stopped = 1;
+        pthread_cond_broadcast(&await_state.changed);
+
+    }
+
+    pthread_mutex_unlock(&await_state.lock);
+}
+
+int guac_argv_handler(guac_user* user, guac_stream* stream,
+        char* mimetype, char* name) {
+
+    /* Refuse stream if not argument is not registered */

Review comment:
       You're not not not wrong.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [guacamole-server] mike-jumper commented on a change in pull request #297: GUACAMOLE-221: Add convenience API for automatically handling received "argv" streams.

Posted by GitBox <gi...@apache.org>.
mike-jumper commented on a change in pull request #297:
URL: https://github.com/apache/guacamole-server/pull/297#discussion_r450559283



##########
File path: src/libguac/argv.c
##########
@@ -0,0 +1,350 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "config.h"
+
+#include "guacamole/argv.h"
+#include "guacamole/client.h"
+#include "guacamole/protocol.h"
+#include "guacamole/socket.h"
+#include "guacamole/stream.h"
+#include "guacamole/string.h"
+#include "guacamole/user.h"
+
+#include <pthread.h>
+#include <stdlib.h>
+#include <string.h>
+
+/**
+ * The state of an argument that will be automatically processed. Note that
+ * this is distinct from the state of an argument value that is currently being
+ * processed. Argument value states are dynamically-allocated and scoped by the
+ * associated guac_stream.
+ */
+typedef struct guac_argv_state {
+
+    /**
+     * The name of the argument.
+     */
+    char name[GUAC_ARGV_MAX_NAME_LENGTH];
+
+    /**
+     * Whether at least one value for this argument has been received since it
+     * was registered.
+     */
+    int received;
+
+    /**
+     * Bitwise OR of all option flags that should affect processing of this
+     * argument.
+     */
+    int options;
+
+    /**
+     * The callback that should be invoked when a new value for the associated
+     * argument has been received. If the GUAC_ARGV_OPTION_ONCE flag is set,
+     * the callback will be invoked at most once.
+     */
+    guac_argv_callback* callback;
+
+    /**
+     * The arbitrary data that should be passed to the callback.
+     */
+    void* data;
+
+} guac_argv_state;
+
+/**
+ * The current state of automatic processing of "argv" streams.
+ */
+typedef struct guac_argv_await_state {
+
+    /**
+     * Whether automatic argument processing has been stopped via a call to
+     * guac_argv_stop().
+     */
+    int stopped;
+
+    /**
+     * The total number of arguments registered.
+     */
+    unsigned int num_registered;
+
+    /**
+     * All registered arguments and their corresponding callbacks.
+     */
+    guac_argv_state registered[GUAC_ARGV_MAX_REGISTERED];
+
+    /**
+     * Lock which protects multi-threaded access to this entire state
+     * structure, including the condition that signals specific modifications
+     * to the structure.
+     */
+    pthread_mutex_t lock;
+
+    /**
+     * Condition which is signalled whenever the overall state of "argv"
+     * processing changes, either through the receipt of a new argument value
+     * or due to a call to guac_argv_stop().
+     */
+    pthread_cond_t changed;
+
+} guac_argv_await_state;
+
+/**
+ * The value or current status of a connection parameter received over an
+ * "argv" stream.
+ */
+typedef struct guac_argv {
+
+    /**
+     * The state of the specific setting being updated.
+     */
+    guac_argv_state* state;
+
+    /**
+     * The mimetype of the data being received.
+     */
+    char mimetype[GUAC_ARGV_MAX_MIMETYPE_LENGTH];
+
+    /**
+     * Buffer space for containing the received argument value.
+     */
+    char buffer[GUAC_ARGV_MAX_LENGTH];
+
+    /**
+     * The number of bytes received so far.
+     */
+    int length;
+
+} guac_argv;
+
+/**
+ * Statically-allocated, shared state of the guac_argv_*() family of functions.
+ */
+static guac_argv_await_state await_state = {
+    .lock = PTHREAD_MUTEX_INITIALIZER,
+    .changed = PTHREAD_COND_INITIALIZER
+};
+
+/**
+ * Returns whether at least one value for each of the provided arguments has
+ * been received.
+ *
+ * @param args
+ *     A NULL-terminated array of the names of all arguments to test.
+ *
+ * @return
+ *     Non-zero if at least one value has been received for each of the
+ *     provided arguments, zero otherwise.
+ */
+static int guac_argv_is_received(const char** args) {
+
+    for (int i = 0; i < await_state.num_registered; i++) {
+
+        /* Ignore all received arguments */
+        guac_argv_state* state = &await_state.registered[i];
+        if (state->received)
+            continue;
+
+        /* Fail immediately for any matching non-received arguments */
+        for (const char** arg = args; *arg != NULL; arg++) {
+            if (strcmp(state->name, *arg) == 0)
+                return 0;
+        }
+
+    }
+
+    /* All arguments were received */
+    return 1;
+
+}
+
+int guac_argv_register(const char* name, guac_argv_callback* callback, void* data, int options) {
+
+    pthread_mutex_lock(&await_state.lock);
+
+    if (await_state.num_registered == GUAC_ARGV_MAX_REGISTERED) {
+        pthread_mutex_unlock(&await_state.lock);
+        return 1;
+    }
+
+    guac_argv_state* state = &await_state.registered[await_state.num_registered++];
+    guac_strlcpy(state->name, name, sizeof(state->name));
+    state->options = options;
+    state->callback = callback;
+    state->data = data;
+
+    pthread_mutex_unlock(&await_state.lock);
+    return 0;
+
+}
+
+int guac_argv_await(const char** args) {
+
+    /* Wait for all requested arguments to be received (or for receipt to be
+     * stopped) */
+    pthread_mutex_lock(&await_state.lock);
+    while (!await_state.stopped && !guac_argv_is_received(args))
+        pthread_cond_wait(&await_state.changed, &await_state.lock);
+
+    /* Arguments were successfully received only if receipt was not stopped */
+    int retval = await_state.stopped;
+    pthread_mutex_unlock(&await_state.lock);
+    return retval;
+
+}
+
+/**
+ * Handler for "blob" instructions which appends the data from received blobs
+ * to the end of the in-progress argument value buffer.
+ *
+ * @see guac_user_blob_handler
+ */
+static int guac_argv_blob_handler(guac_user* user, guac_stream* stream,
+        void* data, int length) {
+
+    guac_argv* argv = (guac_argv*) stream->data;
+
+    /* Calculate buffer size remaining, including space for null terminator,
+     * adjusting received length accordingly */
+    int remaining = sizeof(argv->buffer) - argv->length - 1;
+    if (length > remaining)
+        length = remaining;
+
+    /* Append received data to end of buffer */
+    memcpy(argv->buffer + argv->length, data, length);
+    argv->length += length;
+
+    return 0;
+
+}
+
+/**
+ * Handler for "end" instructions which applies the changes specified by the
+ * argument value buffer associated with the stream.
+ *
+ * @see guac_user_end_handler
+ */
+static int guac_argv_end_handler(guac_user* user, guac_stream* stream) {
+
+    int result = 0;
+
+    /* Append null terminator to value */
+    guac_argv* argv = (guac_argv*) stream->data;
+    argv->buffer[argv->length] = '\0';
+
+    pthread_mutex_lock(&await_state.lock);
+
+    /* Invoke callback, limiting to a single invocation if
+     * GUAC_ARGV_OPTION_ONCE applies */
+    guac_argv_state* state = argv->state;
+    if (!(state->options & GUAC_ARGV_OPTION_ONCE) || !state->received) {
+        if (state->callback != NULL)
+            result = state->callback(user, argv->mimetype, state->name, argv->buffer, state->data);
+    }
+
+    /* Alert connected clients regarding newly-accepted values if echo is
+     * enabled */
+    if (!result && (state->options & GUAC_ARGV_OPTION_ECHO)) {
+        guac_client* client = user->client;
+        guac_client_stream_argv(client, client->socket, argv->mimetype, state->name, argv->buffer);
+    }
+
+    /* Notify that argument has been received */
+    state->received = 1;
+    pthread_cond_broadcast(&await_state.changed);
+
+    pthread_mutex_unlock(&await_state.lock);
+
+    free(argv);
+    return 0;
+
+}
+
+int guac_argv_received(guac_stream* stream, const char* mimetype, const char* name) {
+
+    pthread_mutex_lock(&await_state.lock);
+
+    for (int i = 0; i < await_state.num_registered; i++) {
+
+        /* Ignore any arguments that have already been received if they are
+         * declared as being acceptable only once */
+        guac_argv_state* state = &await_state.registered[i];
+        if ((state->options & GUAC_ARGV_OPTION_ONCE) && state->received)
+            continue;
+
+        /* Argument matched */
+        if (strcmp(state->name, name) == 0) {
+
+            guac_argv* argv = malloc(sizeof(guac_argv));
+            guac_strlcpy(argv->mimetype, mimetype, sizeof(argv->mimetype));
+            argv->state = state;
+            argv->length = 0;
+
+            stream->data = argv;
+            stream->blob_handler = guac_argv_blob_handler;
+            stream->end_handler = guac_argv_end_handler;
+
+            pthread_mutex_unlock(&await_state.lock);
+            return 0;
+
+        }
+
+    }
+
+    /* No such argument awaiting processing */
+    pthread_mutex_unlock(&await_state.lock);
+    return 1;
+
+}
+
+void guac_argv_stop() {
+    pthread_mutex_lock(&await_state.lock);
+
+    /* Signal any waiting threads that no further argument values will be
+     * received */
+    if (!await_state.stopped) {
+        await_state.stopped = 1;
+        pthread_cond_broadcast(&await_state.changed);
+
+    }
+
+    pthread_mutex_unlock(&await_state.lock);
+}
+
+int guac_argv_handler(guac_user* user, guac_stream* stream,
+        char* mimetype, char* name) {
+
+    /* Refuse stream if not argument is not registered */

Review comment:
       OK - fixed up the relevant commit and rebased.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [guacamole-server] mike-jumper commented on a change in pull request #297: GUACAMOLE-221: Add convenience API for automatically handling received "argv" streams.

Posted by GitBox <gi...@apache.org>.
mike-jumper commented on a change in pull request #297:
URL: https://github.com/apache/guacamole-server/pull/297#discussion_r451291278



##########
File path: src/libguac/argv.c
##########
@@ -0,0 +1,350 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "config.h"
+
+#include "guacamole/argv.h"
+#include "guacamole/client.h"
+#include "guacamole/protocol.h"
+#include "guacamole/socket.h"
+#include "guacamole/stream.h"
+#include "guacamole/string.h"
+#include "guacamole/user.h"
+
+#include <pthread.h>
+#include <stdlib.h>
+#include <string.h>
+
+/**
+ * The state of an argument that will be automatically processed. Note that
+ * this is distinct from the state of an argument value that is currently being
+ * processed. Argument value states are dynamically-allocated and scoped by the
+ * associated guac_stream.
+ */
+typedef struct guac_argv_state {
+
+    /**
+     * The name of the argument.
+     */
+    char name[GUAC_ARGV_MAX_NAME_LENGTH];
+
+    /**
+     * Whether at least one value for this argument has been received since it
+     * was registered.
+     */
+    int received;
+
+    /**
+     * Bitwise OR of all option flags that should affect processing of this
+     * argument.
+     */
+    int options;
+
+    /**
+     * The callback that should be invoked when a new value for the associated
+     * argument has been received. If the GUAC_ARGV_OPTION_ONCE flag is set,
+     * the callback will be invoked at most once.
+     */
+    guac_argv_callback* callback;
+
+    /**
+     * The arbitrary data that should be passed to the callback.
+     */
+    void* data;
+
+} guac_argv_state;
+
+/**
+ * The current state of automatic processing of "argv" streams.
+ */
+typedef struct guac_argv_await_state {
+
+    /**
+     * Whether automatic argument processing has been stopped via a call to
+     * guac_argv_stop().
+     */
+    int stopped;
+
+    /**
+     * The total number of arguments registered.
+     */
+    unsigned int num_registered;

Review comment:
       > Or does that happen automatically during the static allocation below?
   
   Exactly. The C standard specifies that a statically-initialized array or structure is initialized to the values specified, with all other values being initialized to zero.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [guacamole-server] mike-jumper commented on a change in pull request #297: GUACAMOLE-221: Add convenience API for automatically handling received "argv" streams.

Posted by GitBox <gi...@apache.org>.
mike-jumper commented on a change in pull request #297:
URL: https://github.com/apache/guacamole-server/pull/297#discussion_r449821980



##########
File path: src/libguac/guacamole/argv-constants.h
##########
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef GUAC_ARGV_CONSTANTS_H
+#define GUAC_ARGV_CONSTANTS_H
+
+/**
+ * Constants related to automatic handling of received "argv" instructions.
+ *
+ * @file argv-constants.h
+ */
+
+/**
+ * Option flag which declares to guac_argv_register() that the associated
+ * argument should be processed exactly once. If multiple "argv" streams are
+ * received for the argument, only the first such stream is processed.
+ * Additional streams will be rejected.
+ */
+#define GUAC_ARGV_OPTION_ONCE 1
+
+/**
+ * Option flag which declares to guac_argv_register() that the values received
+ * and accepted for the associated argument should be echoed to all connected
+ * users via outbound "argv" streams.
+ */
+#define GUAC_ARGV_OPTION_ECHO 2

Review comment:
       Yep - the current value of the parameters that users may change needs to be sent to the user when the connection starts, as well as to new users joining a shared connection. This is distinct from the case where an update to a parameter is broadcast to all current users of the connection (`GUAC_ARGV_OPTION_ECHO`).




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [guacamole-server] necouchman commented on a change in pull request #297: GUACAMOLE-221: Add convenience API for automatically handling received "argv" streams.

Posted by GitBox <gi...@apache.org>.
necouchman commented on a change in pull request #297:
URL: https://github.com/apache/guacamole-server/pull/297#discussion_r451067213



##########
File path: src/libguac/argv.c
##########
@@ -0,0 +1,350 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "config.h"
+
+#include "guacamole/argv.h"
+#include "guacamole/client.h"
+#include "guacamole/protocol.h"
+#include "guacamole/socket.h"
+#include "guacamole/stream.h"
+#include "guacamole/string.h"
+#include "guacamole/user.h"
+
+#include <pthread.h>
+#include <stdlib.h>
+#include <string.h>
+
+/**
+ * The state of an argument that will be automatically processed. Note that
+ * this is distinct from the state of an argument value that is currently being
+ * processed. Argument value states are dynamically-allocated and scoped by the
+ * associated guac_stream.
+ */
+typedef struct guac_argv_state {
+
+    /**
+     * The name of the argument.
+     */
+    char name[GUAC_ARGV_MAX_NAME_LENGTH];
+
+    /**
+     * Whether at least one value for this argument has been received since it
+     * was registered.
+     */
+    int received;
+
+    /**
+     * Bitwise OR of all option flags that should affect processing of this
+     * argument.
+     */
+    int options;
+
+    /**
+     * The callback that should be invoked when a new value for the associated
+     * argument has been received. If the GUAC_ARGV_OPTION_ONCE flag is set,
+     * the callback will be invoked at most once.
+     */
+    guac_argv_callback* callback;
+
+    /**
+     * The arbitrary data that should be passed to the callback.
+     */
+    void* data;
+
+} guac_argv_state;
+
+/**
+ * The current state of automatic processing of "argv" streams.
+ */
+typedef struct guac_argv_await_state {
+
+    /**
+     * Whether automatic argument processing has been stopped via a call to
+     * guac_argv_stop().
+     */
+    int stopped;
+
+    /**
+     * The total number of arguments registered.
+     */
+    unsigned int num_registered;
+
+    /**
+     * All registered arguments and their corresponding callbacks.
+     */
+    guac_argv_state registered[GUAC_ARGV_MAX_REGISTERED];
+
+    /**
+     * Lock which protects multi-threaded access to this entire state
+     * structure, including the condition that signals specific modifications
+     * to the structure.
+     */
+    pthread_mutex_t lock;
+
+    /**
+     * Condition which is signalled whenever the overall state of "argv"
+     * processing changes, either through the receipt of a new argument value
+     * or due to a call to guac_argv_stop().
+     */
+    pthread_cond_t changed;
+
+} guac_argv_await_state;
+
+/**
+ * The value or current status of a connection parameter received over an
+ * "argv" stream.
+ */
+typedef struct guac_argv {
+
+    /**
+     * The state of the specific setting being updated.
+     */
+    guac_argv_state* state;
+
+    /**
+     * The mimetype of the data being received.
+     */
+    char mimetype[GUAC_ARGV_MAX_MIMETYPE_LENGTH];
+
+    /**
+     * Buffer space for containing the received argument value.
+     */
+    char buffer[GUAC_ARGV_MAX_LENGTH];
+
+    /**
+     * The number of bytes received so far.
+     */
+    int length;
+
+} guac_argv;
+
+/**
+ * Statically-allocated, shared state of the guac_argv_*() family of functions.
+ */
+static guac_argv_await_state await_state = {
+    .lock = PTHREAD_MUTEX_INITIALIZER,
+    .changed = PTHREAD_COND_INITIALIZER
+};

Review comment:
       I'm guessing statically allocated `mutex`/`cond` don't have to be `free`d?  Or is it just assumed that it'll be freed when the process is shut down?

##########
File path: src/libguac/argv.c
##########
@@ -0,0 +1,350 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "config.h"
+
+#include "guacamole/argv.h"
+#include "guacamole/client.h"
+#include "guacamole/protocol.h"
+#include "guacamole/socket.h"
+#include "guacamole/stream.h"
+#include "guacamole/string.h"
+#include "guacamole/user.h"
+
+#include <pthread.h>
+#include <stdlib.h>
+#include <string.h>
+
+/**
+ * The state of an argument that will be automatically processed. Note that
+ * this is distinct from the state of an argument value that is currently being
+ * processed. Argument value states are dynamically-allocated and scoped by the
+ * associated guac_stream.
+ */
+typedef struct guac_argv_state {
+
+    /**
+     * The name of the argument.
+     */
+    char name[GUAC_ARGV_MAX_NAME_LENGTH];
+
+    /**
+     * Whether at least one value for this argument has been received since it
+     * was registered.
+     */
+    int received;
+
+    /**
+     * Bitwise OR of all option flags that should affect processing of this
+     * argument.
+     */
+    int options;
+
+    /**
+     * The callback that should be invoked when a new value for the associated
+     * argument has been received. If the GUAC_ARGV_OPTION_ONCE flag is set,
+     * the callback will be invoked at most once.
+     */
+    guac_argv_callback* callback;
+
+    /**
+     * The arbitrary data that should be passed to the callback.
+     */
+    void* data;
+
+} guac_argv_state;
+
+/**
+ * The current state of automatic processing of "argv" streams.
+ */
+typedef struct guac_argv_await_state {
+
+    /**
+     * Whether automatic argument processing has been stopped via a call to
+     * guac_argv_stop().
+     */
+    int stopped;
+
+    /**
+     * The total number of arguments registered.
+     */
+    unsigned int num_registered;

Review comment:
       I must just be missing it, but where is this variable initialized?  I see the static allocation of .lock and .changed, but I don't see that this is ever set to zero?  Or does that happen automatically during the static allocation below?

##########
File path: src/protocols/telnet/user.c
##########
@@ -92,7 +92,7 @@ int guac_telnet_user_join_handler(guac_user* user, int argc, char** argv) {
         user->pipe_handler = guac_telnet_pipe_handler;
 
         /* Updates to connection parameters */
-        user->argv_handler = guac_telnet_argv_handler;
+        user->argv_handler = guac_argv_handler;

Review comment:
       Looks like this is consistently changed to the `guac_argv_handler` for all of the protocols - does it make sense to continue to allow this to be configurable, or is it something that should be changed in the `guac_user` code such that `guac_argv_handler` is always what is called?
   
   I'm good with it either way, just curious - I'm all for leaving things flexible/configurable, just wasn't sure if the new `argv` code is intended to be the new flexible/configurable framework that is always referred to, or if there are foreseeable situations where you'd still want to change that?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [guacamole-server] necouchman commented on a change in pull request #297: GUACAMOLE-221: Add convenience API for automatically handling received "argv" streams.

Posted by GitBox <gi...@apache.org>.
necouchman commented on a change in pull request #297:
URL: https://github.com/apache/guacamole-server/pull/297#discussion_r449575135



##########
File path: src/libguac/guacamole/argv-constants.h
##########
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef GUAC_ARGV_CONSTANTS_H
+#define GUAC_ARGV_CONSTANTS_H
+
+/**
+ * Constants related to automatic handling of received "argv" instructions.
+ *
+ * @file argv-constants.h
+ */
+
+/**
+ * Option flag which declares to guac_argv_register() that the associated
+ * argument should be processed exactly once. If multiple "argv" streams are
+ * received for the argument, only the first such stream is processed.
+ * Additional streams will be rejected.
+ */
+#define GUAC_ARGV_OPTION_ONCE 1
+
+/**
+ * Option flag which declares to guac_argv_register() that the values received
+ * and accepted for the associated argument should be echoed to all connected
+ * users via outbound "argv" streams.
+ */
+#define GUAC_ARGV_OPTION_ECHO 2

Review comment:
       With this option, here and implemented in the new `libguac/argv.c` file, is there still a need for the following functions:
   `guac_telnet_send_current_argv()`
   `guac_ssh_send_current_argv()`
   `guac_kubernetes_send_current_argv()`
   
   ?  It seems like the echo here and the `guac_*_send_current_argv()` have the same purpose - sending the argv value back to the client.  Is there a reason they are both required?

##########
File path: src/libguac/argv.c
##########
@@ -0,0 +1,350 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "config.h"
+
+#include "guacamole/argv.h"
+#include "guacamole/client.h"
+#include "guacamole/protocol.h"
+#include "guacamole/socket.h"
+#include "guacamole/stream.h"
+#include "guacamole/string.h"
+#include "guacamole/user.h"
+
+#include <pthread.h>
+#include <stdlib.h>
+#include <string.h>
+
+/**
+ * The state of an argument that will be automatically processed. Note that
+ * this is distinct from the state of an argument value that is currently being
+ * processed. Argument value states are dynamically-allocated and scoped by the
+ * associated guac_stream.
+ */
+typedef struct guac_argv_state {
+
+    /**
+     * The name of the argument.
+     */
+    char name[GUAC_ARGV_MAX_NAME_LENGTH];
+
+    /**
+     * Whether at least one value for this argument has been received since it
+     * was registered.
+     */
+    int received;
+
+    /**
+     * Bitwise OR of all option flags that should affect processing of this
+     * argument.
+     */
+    int options;
+
+    /**
+     * The callback that should be invoked when a new value for the associated
+     * argument has been received. If the GUAC_ARGV_OPTION_ONCE flag is set,
+     * the callback will be invoked at most once.
+     */
+    guac_argv_callback* callback;
+
+    /**
+     * The arbitrary data that should be passed to the callback.
+     */
+    void* data;
+
+} guac_argv_state;
+
+/**
+ * The current state of automatic processing of "argv" streams.
+ */
+typedef struct guac_argv_await_state {
+
+    /**
+     * Whether automatic argument processing has been stopped via a call to
+     * guac_argv_stop().
+     */
+    int stopped;
+
+    /**
+     * The total number of arguments registered.
+     */
+    unsigned int num_registered;
+
+    /**
+     * All registered arguments and their corresponding callbacks.
+     */
+    guac_argv_state registered[GUAC_ARGV_MAX_REGISTERED];
+
+    /**
+     * Lock which protects multi-threaded access to this entire state
+     * structure, including the condition that signals specific modifications
+     * to the structure.
+     */
+    pthread_mutex_t lock;
+
+    /**
+     * Condition which is signalled whenever the overall state of "argv"
+     * processing changes, either through the receipt of a new argument value
+     * or due to a call to guac_argv_stop().
+     */
+    pthread_cond_t changed;
+
+} guac_argv_await_state;
+
+/**
+ * The value or current status of a connection parameter received over an
+ * "argv" stream.
+ */
+typedef struct guac_argv {
+
+    /**
+     * The state of the specific setting being updated.
+     */
+    guac_argv_state* state;
+
+    /**
+     * The mimetype of the data being received.
+     */
+    char mimetype[GUAC_ARGV_MAX_MIMETYPE_LENGTH];
+
+    /**
+     * Buffer space for containing the received argument value.
+     */
+    char buffer[GUAC_ARGV_MAX_LENGTH];
+
+    /**
+     * The number of bytes received so far.
+     */
+    int length;
+
+} guac_argv;
+
+/**
+ * Statically-allocated, shared state of the guac_argv_*() family of functions.
+ */
+static guac_argv_await_state await_state = {
+    .lock = PTHREAD_MUTEX_INITIALIZER,
+    .changed = PTHREAD_COND_INITIALIZER
+};
+
+/**
+ * Returns whether at least one value for each of the provided arguments has
+ * been received.
+ *
+ * @param args
+ *     A NULL-terminated array of the names of all arguments to test.
+ *
+ * @return
+ *     Non-zero if at least one value has been received for each of the
+ *     provided arguments, zero otherwise.
+ */
+static int guac_argv_is_received(const char** args) {
+
+    for (int i = 0; i < await_state.num_registered; i++) {
+
+        /* Ignore all received arguments */
+        guac_argv_state* state = &await_state.registered[i];
+        if (state->received)
+            continue;
+
+        /* Fail immediately for any matching non-received arguments */
+        for (const char** arg = args; *arg != NULL; arg++) {
+            if (strcmp(state->name, *arg) == 0)
+                return 0;
+        }
+
+    }
+
+    /* All arguments were received */
+    return 1;
+
+}
+
+int guac_argv_register(const char* name, guac_argv_callback* callback, void* data, int options) {
+
+    pthread_mutex_lock(&await_state.lock);
+
+    if (await_state.num_registered == GUAC_ARGV_MAX_REGISTERED) {
+        pthread_mutex_unlock(&await_state.lock);
+        return 1;
+    }
+
+    guac_argv_state* state = &await_state.registered[await_state.num_registered++];
+    guac_strlcpy(state->name, name, sizeof(state->name));
+    state->options = options;
+    state->callback = callback;
+    state->data = data;
+
+    pthread_mutex_unlock(&await_state.lock);
+    return 0;
+
+}
+
+int guac_argv_await(const char** args) {
+
+    /* Wait for all requested arguments to be received (or for receipt to be
+     * stopped) */
+    pthread_mutex_lock(&await_state.lock);
+    while (!await_state.stopped && !guac_argv_is_received(args))
+        pthread_cond_wait(&await_state.changed, &await_state.lock);
+
+    /* Arguments were successfully received only if receipt was not stopped */
+    int retval = await_state.stopped;
+    pthread_mutex_unlock(&await_state.lock);
+    return retval;
+
+}
+
+/**
+ * Handler for "blob" instructions which appends the data from received blobs
+ * to the end of the in-progress argument value buffer.
+ *
+ * @see guac_user_blob_handler
+ */
+static int guac_argv_blob_handler(guac_user* user, guac_stream* stream,
+        void* data, int length) {
+
+    guac_argv* argv = (guac_argv*) stream->data;
+
+    /* Calculate buffer size remaining, including space for null terminator,
+     * adjusting received length accordingly */
+    int remaining = sizeof(argv->buffer) - argv->length - 1;
+    if (length > remaining)
+        length = remaining;
+
+    /* Append received data to end of buffer */
+    memcpy(argv->buffer + argv->length, data, length);
+    argv->length += length;
+
+    return 0;
+
+}
+
+/**
+ * Handler for "end" instructions which applies the changes specified by the
+ * argument value buffer associated with the stream.
+ *
+ * @see guac_user_end_handler
+ */
+static int guac_argv_end_handler(guac_user* user, guac_stream* stream) {
+
+    int result = 0;
+
+    /* Append null terminator to value */
+    guac_argv* argv = (guac_argv*) stream->data;
+    argv->buffer[argv->length] = '\0';
+
+    pthread_mutex_lock(&await_state.lock);
+
+    /* Invoke callback, limiting to a single invocation if
+     * GUAC_ARGV_OPTION_ONCE applies */
+    guac_argv_state* state = argv->state;
+    if (!(state->options & GUAC_ARGV_OPTION_ONCE) || !state->received) {
+        if (state->callback != NULL)
+            result = state->callback(user, argv->mimetype, state->name, argv->buffer, state->data);
+    }
+
+    /* Alert connected clients regarding newly-accepted values if echo is
+     * enabled */
+    if (!result && (state->options & GUAC_ARGV_OPTION_ECHO)) {
+        guac_client* client = user->client;
+        guac_client_stream_argv(client, client->socket, argv->mimetype, state->name, argv->buffer);
+    }
+
+    /* Notify that argument has been received */
+    state->received = 1;
+    pthread_cond_broadcast(&await_state.changed);
+
+    pthread_mutex_unlock(&await_state.lock);
+
+    free(argv);
+    return 0;
+
+}
+
+int guac_argv_received(guac_stream* stream, const char* mimetype, const char* name) {
+
+    pthread_mutex_lock(&await_state.lock);
+
+    for (int i = 0; i < await_state.num_registered; i++) {
+
+        /* Ignore any arguments that have already been received if they are
+         * declared as being acceptable only once */
+        guac_argv_state* state = &await_state.registered[i];
+        if ((state->options & GUAC_ARGV_OPTION_ONCE) && state->received)
+            continue;
+
+        /* Argument matched */
+        if (strcmp(state->name, name) == 0) {
+
+            guac_argv* argv = malloc(sizeof(guac_argv));
+            guac_strlcpy(argv->mimetype, mimetype, sizeof(argv->mimetype));
+            argv->state = state;
+            argv->length = 0;
+
+            stream->data = argv;
+            stream->blob_handler = guac_argv_blob_handler;
+            stream->end_handler = guac_argv_end_handler;
+
+            pthread_mutex_unlock(&await_state.lock);
+            return 0;
+
+        }
+
+    }
+
+    /* No such argument awaiting processing */
+    pthread_mutex_unlock(&await_state.lock);
+    return 1;
+
+}
+
+void guac_argv_stop() {
+    pthread_mutex_lock(&await_state.lock);
+
+    /* Signal any waiting threads that no further argument values will be
+     * received */
+    if (!await_state.stopped) {
+        await_state.stopped = 1;
+        pthread_cond_broadcast(&await_state.changed);
+
+    }
+
+    pthread_mutex_unlock(&await_state.lock);
+}
+
+int guac_argv_handler(guac_user* user, guac_stream* stream,
+        char* mimetype, char* name) {
+
+    /* Refuse stream if not argument is not registered */

Review comment:
       An extra "not" in here, I think.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [guacamole-server] mike-jumper commented on a change in pull request #297: GUACAMOLE-221: Add convenience API for automatically handling received "argv" streams.

Posted by GitBox <gi...@apache.org>.
mike-jumper commented on a change in pull request #297:
URL: https://github.com/apache/guacamole-server/pull/297#discussion_r451291486



##########
File path: src/libguac/argv.c
##########
@@ -0,0 +1,350 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "config.h"
+
+#include "guacamole/argv.h"
+#include "guacamole/client.h"
+#include "guacamole/protocol.h"
+#include "guacamole/socket.h"
+#include "guacamole/stream.h"
+#include "guacamole/string.h"
+#include "guacamole/user.h"
+
+#include <pthread.h>
+#include <stdlib.h>
+#include <string.h>
+
+/**
+ * The state of an argument that will be automatically processed. Note that
+ * this is distinct from the state of an argument value that is currently being
+ * processed. Argument value states are dynamically-allocated and scoped by the
+ * associated guac_stream.
+ */
+typedef struct guac_argv_state {
+
+    /**
+     * The name of the argument.
+     */
+    char name[GUAC_ARGV_MAX_NAME_LENGTH];
+
+    /**
+     * Whether at least one value for this argument has been received since it
+     * was registered.
+     */
+    int received;
+
+    /**
+     * Bitwise OR of all option flags that should affect processing of this
+     * argument.
+     */
+    int options;
+
+    /**
+     * The callback that should be invoked when a new value for the associated
+     * argument has been received. If the GUAC_ARGV_OPTION_ONCE flag is set,
+     * the callback will be invoked at most once.
+     */
+    guac_argv_callback* callback;
+
+    /**
+     * The arbitrary data that should be passed to the callback.
+     */
+    void* data;
+
+} guac_argv_state;
+
+/**
+ * The current state of automatic processing of "argv" streams.
+ */
+typedef struct guac_argv_await_state {
+
+    /**
+     * Whether automatic argument processing has been stopped via a call to
+     * guac_argv_stop().
+     */
+    int stopped;
+
+    /**
+     * The total number of arguments registered.
+     */
+    unsigned int num_registered;
+
+    /**
+     * All registered arguments and their corresponding callbacks.
+     */
+    guac_argv_state registered[GUAC_ARGV_MAX_REGISTERED];
+
+    /**
+     * Lock which protects multi-threaded access to this entire state
+     * structure, including the condition that signals specific modifications
+     * to the structure.
+     */
+    pthread_mutex_t lock;
+
+    /**
+     * Condition which is signalled whenever the overall state of "argv"
+     * processing changes, either through the receipt of a new argument value
+     * or due to a call to guac_argv_stop().
+     */
+    pthread_cond_t changed;
+
+} guac_argv_await_state;
+
+/**
+ * The value or current status of a connection parameter received over an
+ * "argv" stream.
+ */
+typedef struct guac_argv {
+
+    /**
+     * The state of the specific setting being updated.
+     */
+    guac_argv_state* state;
+
+    /**
+     * The mimetype of the data being received.
+     */
+    char mimetype[GUAC_ARGV_MAX_MIMETYPE_LENGTH];
+
+    /**
+     * Buffer space for containing the received argument value.
+     */
+    char buffer[GUAC_ARGV_MAX_LENGTH];
+
+    /**
+     * The number of bytes received so far.
+     */
+    int length;
+
+} guac_argv;
+
+/**
+ * Statically-allocated, shared state of the guac_argv_*() family of functions.
+ */
+static guac_argv_await_state await_state = {
+    .lock = PTHREAD_MUTEX_INITIALIZER,
+    .changed = PTHREAD_COND_INITIALIZER
+};

Review comment:
       Correct. There does not need to be a corresponding call to `pthread_potato_destroy()` so long as it's a statically-allocated potato.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [guacamole-server] mike-jumper commented on a change in pull request #297: GUACAMOLE-221: Add convenience API for automatically handling received "argv" streams.

Posted by GitBox <gi...@apache.org>.
mike-jumper commented on a change in pull request #297:
URL: https://github.com/apache/guacamole-server/pull/297#discussion_r451296733



##########
File path: src/protocols/telnet/user.c
##########
@@ -92,7 +92,7 @@ int guac_telnet_user_join_handler(guac_user* user, int argc, char** argv) {
         user->pipe_handler = guac_telnet_pipe_handler;
 
         /* Updates to connection parameters */
-        user->argv_handler = guac_telnet_argv_handler;
+        user->argv_handler = guac_argv_handler;

Review comment:
       I definitely think that `argv_handler` needs to stay there:
   
   The pattern of the API provided by libguac is one of gradually increasing levels of abstraction, and I think it's important that we maintain that pattern consistently. We provide higher-level convenience functions, default implementations, etc. as needed, but aim to always allow implementations to take control if those conveniences aren't well-suited in their case.
   
   Regarding this specific implementation, it provides its convenience at the cost of limited buffer space for received argument values. Should an implementation need greater space, or to process arguments of any size as true streams, they will need to be able to handle the raw stream themselves.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org