Changeset 787b65b in mainline for uspace/srv/hid/remcons/remcons.c


Ignore:
Timestamp:
2012-01-12T07:47:51Z (12 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
30d4706
Parents:
1870d81
Message:

remcons: variable/type renaming

The most fundamental change is that client_t was renamed to telnet_user_t
to emphasise the difference between a human user on the other side of the
network and a local client that requires some service from remcons.

This commit shall not bring any change in functionality.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/hid/remcons/remcons.c

    r1870d81 r787b65b  
    8484        int locsrv_connection_count;
    8585        bool socket_closed;
    86 } client_t;
    87 
    88 static FIBRIL_MUTEX_INITIALIZE(clients_guard);
    89 static LIST_INITIALIZE(clients);
     86} telnet_user_t;
     87
     88static FIBRIL_MUTEX_INITIALIZE(users_guard);
     89static LIST_INITIALIZE(users);
    9090
    9191/** Telnet commands to force character mode
    9292 * (redundant to be on the safe side).
    9393 * See
    94  * http://stackoverflow.com/questions/273261/force-telnet-client-into-character-mode
     94 * http://stackoverflow.com/questions/273261/force-telnet-user-into-character-mode
    9595 * for discussion.
    9696 */
     
    103103    sizeof(telnet_force_character_mode_command) / sizeof(telnet_cmd_t);
    104104
    105 static client_t *client_create(int socket)
    106 {
    107         static int client_counter = 0;
    108 
    109         client_t *client = malloc(sizeof(client_t));
    110         if (client == NULL) {
     105static telnet_user_t *telnet_user_create(int socket)
     106{
     107        static int telnet_user_id_counter = 0;
     108
     109        telnet_user_t *user = malloc(sizeof(telnet_user_t));
     110        if (user == NULL) {
    111111                return NULL;
    112112        }
    113113
    114         client->id = ++client_counter;
    115 
    116         int rc = asprintf(&client->service_name, "%s/telnet%d", NAMESPACE, client->id);
     114        user->id = ++telnet_user_id_counter;
     115
     116        int rc = asprintf(&user->service_name, "%s/telnet%d", NAMESPACE, user->id);
    117117        if (rc < 0) {
    118                 free(client);
     118                free(user);
    119119                return NULL;
    120120        }
    121121
    122         client->socket = socket;
    123         client->service_id = (service_id_t) -1;
    124         prodcons_initialize(&client->in_events);
    125         link_initialize(&client->link);
    126         client->socket_buffer_len = 0;
    127         client->socket_buffer_pos = 0;
    128 
    129         fibril_condvar_initialize(&client->refcount_cv);
    130         fibril_mutex_initialize(&client->refcount_mutex);
    131         client->task_finished = false;
    132         client->socket_closed = false;
    133         client->locsrv_connection_count = 0;
    134 
    135 
    136         fibril_mutex_lock(&clients_guard);
    137         list_append(&client->link, &clients);
    138         fibril_mutex_unlock(&clients_guard);
    139 
    140         return client;
    141 }
    142 
    143 static void client_destroy(client_t *client)
    144 {
    145         assert(client);
    146 
    147         fibril_mutex_lock(&clients_guard);
    148         list_remove(&client->link);
    149         fibril_mutex_unlock(&clients_guard);
    150 
    151         free(client);
    152 }
    153 
    154 static client_t *client_get_for_client_connection(service_id_t id)
    155 {
    156         client_t *client = NULL;
    157 
    158         fibril_mutex_lock(&clients_guard);
    159         list_foreach(clients, link) {
    160                 client_t *tmp = list_get_instance(link, client_t, link);
     122        user->socket = socket;
     123        user->service_id = (service_id_t) -1;
     124        prodcons_initialize(&user->in_events);
     125        link_initialize(&user->link);
     126        user->socket_buffer_len = 0;
     127        user->socket_buffer_pos = 0;
     128
     129        fibril_condvar_initialize(&user->refcount_cv);
     130        fibril_mutex_initialize(&user->refcount_mutex);
     131        user->task_finished = false;
     132        user->socket_closed = false;
     133        user->locsrv_connection_count = 0;
     134
     135
     136        fibril_mutex_lock(&users_guard);
     137        list_append(&user->link, &users);
     138        fibril_mutex_unlock(&users_guard);
     139
     140        return user;
     141}
     142
     143static void telnet_user_destroy(telnet_user_t *user)
     144{
     145        assert(user);
     146
     147        fibril_mutex_lock(&users_guard);
     148        list_remove(&user->link);
     149        fibril_mutex_unlock(&users_guard);
     150
     151        free(user);
     152}
     153
     154static telnet_user_t *telnet_user_get_for_client_connection(service_id_t id)
     155{
     156        telnet_user_t *user = NULL;
     157
     158        fibril_mutex_lock(&users_guard);
     159        list_foreach(users, link) {
     160                telnet_user_t *tmp = list_get_instance(link, telnet_user_t, link);
    161161                if (tmp->service_id == id) {
    162                         client = tmp;
    163                         break;
    164                 }
    165         }
    166         if (client == NULL) {
    167                 fibril_mutex_unlock(&clients_guard);
     162                        user = tmp;
     163                        break;
     164                }
     165        }
     166        if (user == NULL) {
     167                fibril_mutex_unlock(&users_guard);
    168168                return NULL;
    169169        }
    170170
    171         client_t *tmp = client;
     171        telnet_user_t *tmp = user;
    172172        fibril_mutex_lock(&tmp->refcount_mutex);
    173         client->locsrv_connection_count++;
     173        user->locsrv_connection_count++;
    174174
    175175        /*
    176          * Refuse to return client whose task already finished or when
     176         * Refuse to return user whose task already finished or when
    177177         * the socket is already closed().
    178178         */
    179         if (client->task_finished || client->socket_closed) {
    180                 client = NULL;
    181                 client->locsrv_connection_count--;
     179        if (user->task_finished || user->socket_closed) {
     180                user = NULL;
     181                user->locsrv_connection_count--;
    182182        }
    183183
     
    185185
    186186
    187         fibril_mutex_unlock(&clients_guard);
    188 
    189         return client;
     187        fibril_mutex_unlock(&users_guard);
     188
     189        return user;
    190190}
    191191
     
    205205}
    206206
    207 static void client_connection_message_loop(client_t *client)
     207static void client_connection_message_loop(telnet_user_t *user)
    208208{
    209209        while (true) {
     
    213213                        callid = async_get_call_timeout(&call, 1000);
    214214
    215                         fibril_mutex_lock(&client->refcount_mutex);
    216                         bool bail_out = client->socket_closed || client->task_finished;
    217                         fibril_mutex_unlock(&client->refcount_mutex);
     215                        fibril_mutex_lock(&user->refcount_mutex);
     216                        bool bail_out = user->socket_closed || user->task_finished;
     217                        fibril_mutex_unlock(&user->refcount_mutex);
    218218
    219219                        if (bail_out) {
     
    238238                        break;
    239239                case CONSOLE_GET_EVENT: {
    240                         if (list_empty(&client->in_events.list)) {
     240                        if (list_empty(&user->in_events.list)) {
    241241                                retry:
    242                                 if (client->socket_buffer_len <= client->socket_buffer_pos) {
    243                                         int recv_length = recv(client->socket, client->socket_buffer, BUFFER_SIZE, 0);
     242                                if (user->socket_buffer_len <= user->socket_buffer_pos) {
     243                                        int recv_length = recv(user->socket, user->socket_buffer, BUFFER_SIZE, 0);
    244244                                        if ((recv_length == 0) || (recv_length == ENOTCONN)) {
    245                                                 fibril_mutex_lock(&client->refcount_mutex);
    246                                                 client->socket_closed = true;
    247                                                 fibril_mutex_unlock(&client->refcount_mutex);
     245                                                fibril_mutex_lock(&user->refcount_mutex);
     246                                                user->socket_closed = true;
     247                                                fibril_mutex_unlock(&user->refcount_mutex);
    248248                                                async_answer_0(callid, ENOENT);
    249249                                                return;
     
    253253                                                return;
    254254                                        }
    255                                         client->socket_buffer_len = recv_length;
    256                                         client->socket_buffer_pos = 0;
     255                                        user->socket_buffer_len = recv_length;
     256                                        user->socket_buffer_pos = 0;
    257257                                }
    258                                 char data = client->socket_buffer[client->socket_buffer_pos++];
     258                                char data = user->socket_buffer[user->socket_buffer_pos++];
    259259                                if (data == 13) {
    260260                                        data = 10;
     
    267267                                assert(down);
    268268                                assert(up);
    269                                 prodcons_produce(&client->in_events, &down->link);
    270                                 prodcons_produce(&client->in_events, &up->link);
     269                                prodcons_produce(&user->in_events, &down->link);
     270                                prodcons_produce(&user->in_events, &up->link);
    271271                        }
    272272
    273273
    274                         link_t *link = prodcons_consume(&client->in_events);
     274                        link_t *link = prodcons_consume(&user->in_events);
    275275                        kbd_event_t *event = list_get_instance(link, kbd_event_t, link);
    276276                        async_answer_4(callid, EOK, event->type, event->key, event->mods, event->c);
     
    306306                                }
    307307                        }
    308                         rc = send(client->socket, buf_converted, buf_converted_size, 0);
     308                        rc = send(user->socket, buf_converted, buf_converted_size, 0);
    309309                        free(buf);
    310310
     
    352352static void client_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
    353353{
    354         /* Find the client. */
    355         client_t *client = client_get_for_client_connection(IPC_GET_ARG1(*icall));
    356         if (client == NULL) {
     354        /* Find the user. */
     355        telnet_user_t *user = telnet_user_get_for_client_connection(IPC_GET_ARG1(*icall));
     356        if (user == NULL) {
    357357                async_answer_0(iid, ENOENT);
    358358                return;
    359359        }
    360360
    361         printf("New client for service %s.\n", client->service_name);
     361        printf("New user for service %s.\n", user->service_name);
    362362
    363363        /* Accept the connection, increment reference. */
     
    365365
    366366        /* Force character mode. */
    367         send(client->socket, (void *)telnet_force_character_mode_command,
     367        send(user->socket, (void *)telnet_force_character_mode_command,
    368368            telnet_force_character_mode_command_count, 0);
    369369
    370         client_connection_message_loop(client);
    371 
    372         /* Announce client disconnection. */
    373         fibril_mutex_lock(&client->refcount_mutex);
    374         client->locsrv_connection_count--;
    375         fibril_condvar_signal(&client->refcount_cv);
    376         fibril_mutex_unlock(&client->refcount_mutex);
     370        client_connection_message_loop(user);
     371
     372        /* Announce user disconnection. */
     373        fibril_mutex_lock(&user->refcount_mutex);
     374        user->locsrv_connection_count--;
     375        fibril_condvar_signal(&user->refcount_cv);
     376        fibril_mutex_unlock(&user->refcount_mutex);
    377377}
    378378
    379379static int spawn_task_fibril(void *arg)
    380380{
    381         client_t *client = arg;
     381        telnet_user_t *user = arg;
    382382        int rc;
    383383
    384384        char term[LOC_NAME_MAXLEN];
    385         snprintf(term, LOC_NAME_MAXLEN, "%s/%s", "/loc", client->service_name);
     385        snprintf(term, LOC_NAME_MAXLEN, "%s/%s", "/loc", user->service_name);
    386386
    387387        task_id_t task;
     
    390390                printf("%s: Error spawning %s -w %s %s (%s)\n", NAME,
    391391                    APP_GETTERM, term, "/app/bdsh", str_error(rc));
    392                 fibril_mutex_lock(&client->refcount_mutex);
    393                 client->task_finished = true;
    394                 fibril_condvar_signal(&client->refcount_cv);
    395                 fibril_mutex_unlock(&client->refcount_mutex);
     392                fibril_mutex_lock(&user->refcount_mutex);
     393                user->task_finished = true;
     394                fibril_condvar_signal(&user->refcount_cv);
     395                fibril_mutex_unlock(&user->refcount_mutex);
    396396                return EOK;
    397397        }
    398398
    399         fibril_mutex_lock(&client->refcount_mutex);
    400         client->task_id = task;
    401         fibril_mutex_unlock(&client->refcount_mutex);
     399        fibril_mutex_lock(&user->refcount_mutex);
     400        user->task_id = task;
     401        fibril_mutex_unlock(&user->refcount_mutex);
    402402
    403403        task_exit_t task_exit;
     
    407407
    408408        /* Announce destruction. */
    409         fibril_mutex_lock(&client->refcount_mutex);
    410         client->task_finished = true;
    411         fibril_condvar_signal(&client->refcount_cv);
    412         fibril_mutex_unlock(&client->refcount_mutex);
     409        fibril_mutex_lock(&user->refcount_mutex);
     410        user->task_finished = true;
     411        fibril_condvar_signal(&user->refcount_cv);
     412        fibril_mutex_unlock(&user->refcount_mutex);
    413413
    414414        return EOK;
    415415}
    416416
    417 static bool client_can_be_destroyed_no_lock(client_t *client)
    418 {
    419         return client->task_finished && client->socket_closed &&
    420             (client->locsrv_connection_count == 0);
    421 }
    422 
    423 static int network_client_fibril(void *arg)
     417static bool user_can_be_destroyed_no_lock(telnet_user_t *user)
     418{
     419        return user->task_finished && user->socket_closed &&
     420            (user->locsrv_connection_count == 0);
     421}
     422
     423static int network_user_fibril(void *arg)
    424424{
    425425        int rc;
    426         client_t *client = arg;
    427 
    428         rc = loc_service_register(client->service_name, &client->service_id);
     426        telnet_user_t *user = arg;
     427
     428        rc = loc_service_register(user->service_name, &user->service_id);
    429429        if (rc != EOK) {
    430430                fprintf(stderr, "%s: Unable to register device %s\n", NAME,
    431                     client->service_name);
     431                    user->service_name);
    432432                return EOK;
    433433        }
    434         printf("Service %s registered as %" PRIun "\n", client->service_name,
    435             client->service_id);
    436 
    437         fid_t spawn_fibril = fibril_create(spawn_task_fibril, client);
     434        printf("Service %s registered as %" PRIun "\n", user->service_name,
     435            user->service_id);
     436
     437        fid_t spawn_fibril = fibril_create(spawn_task_fibril, user);
    438438        assert(spawn_fibril);
    439439        fibril_add_ready(spawn_fibril);
    440440
    441441        /* Wait for all clients to exit. */
    442         fibril_mutex_lock(&client->refcount_mutex);
    443         while (!client_can_be_destroyed_no_lock(client)) {
    444                 if (client->task_finished) {
    445                         closesocket(client->socket);
    446                         client->socket_closed = true;
     442        fibril_mutex_lock(&user->refcount_mutex);
     443        while (!user_can_be_destroyed_no_lock(user)) {
     444                if (user->task_finished) {
     445                        closesocket(user->socket);
     446                        user->socket_closed = true;
    447447                        continue;
    448                 } else if (client->socket_closed) {
    449                         if (client->task_id != 0) {
    450                                 task_kill(client->task_id);
     448                } else if (user->socket_closed) {
     449                        if (user->task_id != 0) {
     450                                task_kill(user->task_id);
    451451                        }
    452452                }
    453                 fibril_condvar_wait_timeout(&client->refcount_cv, &client->refcount_mutex, 1000);
    454         }
    455         fibril_mutex_unlock(&client->refcount_mutex);
    456 
    457         rc = loc_service_unregister(client->service_id);
     453                fibril_condvar_wait_timeout(&user->refcount_cv, &user->refcount_mutex, 1000);
     454        }
     455        fibril_mutex_unlock(&user->refcount_mutex);
     456
     457        rc = loc_service_unregister(user->service_id);
    458458        if (rc != EOK) {
    459                 fprintf(stderr, "Warning: failed to unregister %s: %s\n", client->service_name, str_error(rc));
    460         }
    461 
    462         printf("Destroying service %s.\n", client->service_name);
    463         client_destroy(client);
     459                fprintf(stderr, "Warning: failed to unregister %s: %s\n", user->service_name, str_error(rc));
     460        }
     461
     462        printf("Destroying service %s.\n", user->service_name);
     463        telnet_user_destroy(user);
    464464
    465465        return EOK;
     
    523523                }
    524524
    525                 client_t *client = client_create(conn_sd);
    526                 assert(client);
    527 
    528                 fid_t fid = fibril_create(network_client_fibril, client);
     525                telnet_user_t *user = telnet_user_create(conn_sd);
     526                assert(user);
     527
     528                fid_t fid = fibril_create(network_user_fibril, user);
    529529                assert(fid);
    530530                fibril_add_ready(fid);
Note: See TracChangeset for help on using the changeset viewer.