예제 #1
0
/* We assume that a float is 32 bits long */
int GraphHost_graphData(float x, float y, const char* dataset,
                        struct graphhost_t* graphhost) {
    struct list_elem_t* elem;
    struct list_elem_t* datasetp;
    struct socketconn_t* conn;
    struct graph_payload_t payload;
    uint32_t tmp;

    char* dataset_str;

    if (!graphhost->running) return -1;

    /* Zero the payload structure */
    memset((void*)&payload, 0x00, sizeof(struct graph_payload_t));

    /* Change to network byte order */
    payload.type = 'd';

    /* Swap bytes in x, and copy into the payload struct */
    memcpy(&tmp, &x, sizeof(uint32_t));
    tmp = htonl(tmp);
    memcpy(&payload.x, &tmp, sizeof(uint32_t));

    /* Swap bytes in y, and copy into the payload struct */
    memcpy(&tmp, &y, sizeof(uint32_t));
    tmp = htonl(tmp);
    memcpy(&payload.y, &tmp, sizeof(uint32_t));

    strncpy(payload.dataset, dataset, 15);

    /* Giant lock approach */
    pthread_mutex_lock(&graphhost->mutex);

    /* If the dataset name isn't in the list already, add it. */
    socket_recordgraph(graphhost->graphlist, dataset);

    /* Send the point to connected clients */
    for (elem = graphhost->connlist->start; elem != NULL; elem = elem->next) {
        conn = elem->data;
        for (datasetp = conn->datasets->start; datasetp != NULL;
             datasetp = datasetp->next) {
            dataset_str = datasetp->data;
            if (dataset_str != NULL && strcmp(dataset_str, dataset) == 0) {
                /* Send the value off */
                /* qpayload = malloc(sizeof(struct graph_payload_t));
                memcpy(qpayload, &payload, sizeof(struct graph_payload_t)); */
                sockets_queuewrite(graphhost, conn, (void*)&payload,
                                   sizeof(struct graph_payload_t));
            }
        }
    }

    pthread_mutex_unlock(&graphhost->mutex);

    return 0;
}
예제 #2
0
/* Send to the client a list of available graphs */
int
sockets_sendlist(struct graphhost_t *inst, struct list_t *list, struct list_elem_t *elem) {
  struct list_t *graphlist = inst->graphlist;
  struct socketconn_t *conn = elem->data;
        struct graph_list_t replydg;
  struct list_elem_t *clelem;
  char *tmpstr;

  for(clelem = graphlist->start; clelem != NULL;
    clelem = clelem->next) {

    tmpstr = clelem->data;

    /* Set up the response body, and queue it for sending. */
    memset((void *)&replydg, 0x00, sizeof(struct graph_list_t));

    /* Set the type of the datagram. */
    replydg.type = 'l';

    /* Is this the last element in the list? */
    if(clelem->next == NULL) {
      replydg.end = 1;
    }else{
                  replydg.end = 0;
    }

    /* Copy in the string */
    strcpy(replydg.dataset, tmpstr);

    /* Queue the datagram for writing */
    if(sockets_queuewrite(inst, conn, (void *)&replydg, sizeof(struct graph_list_t)) == -1) {
      return -1;
    }
  }

  return 0;
}