Changeset 5576358 in mainline


Ignore:
Timestamp:
2012-01-09T17:52:42Z (12 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
6f7cd5d
Parents:
21a9869
Message:

remcons: add wrappers for client_t

File:
1 edited

Legend:

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

    r21a9869 r5576358  
    6767        int socket;
    6868        service_id_t service_id;
     69        char *service_name;
    6970        /** Producer-consumer of kbd_event_t. */
    7071        prodcons_t in_events;
     
    7576} client_t;
    7677
     78static FIBRIL_MUTEX_INITIALIZE(clients_guard);
    7779static LIST_INITIALIZE(clients);
    78 static int client_counter = 0;
     80
     81static client_t *client_create(int socket)
     82{
     83        static int client_counter = 0;
     84
     85        client_t *client = malloc(sizeof(client_t));
     86        if (client == NULL) {
     87                return NULL;
     88        }
     89
     90        client->id = ++client_counter;
     91
     92        int rc = asprintf(&client->service_name, "%s/telnet%d", NAMESPACE, client->id);
     93        if (rc < 0) {
     94                free(client);
     95                return NULL;
     96        }
     97
     98        client->socket = socket;
     99        client->service_id = (service_id_t) -1;
     100        prodcons_initialize(&client->in_events);
     101        link_initialize(&client->link);
     102        client->socket_buffer_len = 0;
     103        client->socket_buffer_pos = 0;
     104
     105
     106        fibril_mutex_lock(&clients_guard);
     107        list_append(&client->link, &clients);
     108        fibril_mutex_unlock(&clients_guard);
     109
     110        return client;
     111}
     112
     113static void client_destroy(client_t *client)
     114{
     115        assert(client);
     116
     117        fibril_mutex_lock(&clients_guard);
     118        list_remove(&client->link);
     119        fibril_mutex_unlock(&clients_guard);
     120
     121        free(client);
     122}
     123
     124static client_t *client_find(service_id_t id)
     125{
     126        client_t *client = NULL;
     127
     128        fibril_mutex_lock(&clients_guard);
     129        list_foreach(clients, link) {
     130                client_t *tmp = list_get_instance(link, client_t, link);
     131                if (tmp->service_id == id) {
     132                        client = tmp;
     133                        break;
     134                }
     135        }
     136        fibril_mutex_unlock(&clients_guard);
     137
     138        return client;
     139}
     140
    79141
    80142
     
    94156static void client_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
    95157{
    96         service_id_t id = IPC_GET_ARG1(*icall);
    97 
    98158        /* Find the client. */
    99         client_t *client = NULL;
    100         list_foreach(clients, link) {
    101                 client_t *tmp = list_get_instance(link, client_t, link);
    102                 if (tmp->service_id == IPC_GET_ARG1(*icall)) {
    103                         client = tmp;
    104                         break;
    105                 }
    106         }
    107 
     159        client_t *client = client_find(IPC_GET_ARG1(*icall));
    108160        if (client == NULL) {
    109161                async_answer_0(iid, ENOENT);
     
    111163        }
    112164
    113         printf("New client for service %" PRIun ".\n", id);
     165        printf("New client for service %s.\n", client->service_name);
    114166
    115167        /* Accept the connection */
     
    256308        client_t *client = arg;
    257309
    258         // FIXME: locking
    259         list_append(&client->link, &clients);
    260 
    261         char vc[LOC_NAME_MAXLEN + 1];
    262         snprintf(vc, LOC_NAME_MAXLEN, "%s/rem%d", NAMESPACE, client->id);
    263         if (loc_service_register(vc, &client->service_id) != EOK) {
    264                 fprintf(stderr, "%s: Unable to register device %s\n", NAME, vc);
     310        rc = loc_service_register(client->service_name, &client->service_id);
     311        if (rc != EOK) {
     312                fprintf(stderr, "%s: Unable to register device %s\n", NAME,
     313                    client->service_name);
    265314                return EOK;
    266315        }
    267         printf("Service %s registered as %" PRIun "\n", vc, client->service_id);
    268 
    269 
    270         prodcons_initialize(&client->in_events);
    271         client->socket_buffer_len = 0;
    272         client->socket_buffer_pos = 0;
     316        printf("Service %s registered as %" PRIun "\n", client->service_name,
     317            client->service_id);
    273318
    274319        char term[LOC_NAME_MAXLEN];
    275         snprintf(term, LOC_NAME_MAXLEN, "%s/%s", "/loc", vc);
     320        snprintf(term, LOC_NAME_MAXLEN, "%s/%s", "/loc", client->service_name);
    276321
    277322        task_id_t task;
     
    289334
    290335        closesocket(client->socket);
     336
     337        client_destroy(client);
    291338
    292339        return EOK;
     
    350397                }
    351398
    352                 client_t *client = malloc(sizeof(client_t));
     399                client_t *client = client_create(conn_sd);
    353400                assert(client);
    354                 client->id = ++client_counter;
    355                 client->socket = conn_sd;
    356401
    357402                fid_t fid = fibril_create(network_client_fibril, client);
Note: See TracChangeset for help on using the changeset viewer.