aboutsummaryrefslogtreecommitdiff
path: root/src/sensordashboard
diff options
context:
space:
mode:
authorOmar Tarabai <tarabai@devegypt.com>2014-07-25 18:02:46 +0000
committerOmar Tarabai <tarabai@devegypt.com>2014-07-25 18:02:46 +0000
commit29f73bb496a579a5a7600b3e3b2ab88b859bee65 (patch)
tree8752fcdd559c54c31b84cd0ba7dddd09cad37a5e /src/sensordashboard
parent30752e6cb73b6c71ad5aab392c40ac482257e410 (diff)
downloadgnunet-29f73bb496a579a5a7600b3e3b2ab88b859bee65.tar.gz
gnunet-29f73bb496a579a5a7600b3e3b2ab88b859bee65.zip
sensor: towards update functionality
Diffstat (limited to 'src/sensordashboard')
-rw-r--r--src/sensordashboard/gnunet-service-sensordashboard.c141
1 files changed, 140 insertions, 1 deletions
diff --git a/src/sensordashboard/gnunet-service-sensordashboard.c b/src/sensordashboard/gnunet-service-sensordashboard.c
index b1fc67fa4..a1f4318b8 100644
--- a/src/sensordashboard/gnunet-service-sensordashboard.c
+++ b/src/sensordashboard/gnunet-service-sensordashboard.c
@@ -558,7 +558,7 @@ handle_sensor_reading (void *cls,
558 if (NULL == reading) 558 if (NULL == reading)
559 { 559 {
560 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 560 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
561 "Received an invalid sensor reading from peer `%s'\n", 561 "Received an invalid sensor reading from peer `%s'.\n",
562 GNUNET_i2s (&cp->peerid)); 562 GNUNET_i2s (&cp->peerid));
563 return GNUNET_SYSERR; 563 return GNUNET_SYSERR;
564 } 564 }
@@ -583,6 +583,142 @@ handle_sensor_reading (void *cls,
583 583
584 584
585/** 585/**
586 * Create a message with full information about sensor
587 *
588 * @param sensorname Name of sensor requested
589 * @return Message ready to be sent to client or NULL on error
590 */
591static struct GNUNET_SENSOR_SensorFullMessage *
592create_full_sensor_msg (char *sensorname)
593{
594 struct GNUNET_HashCode key;
595 struct GNUNET_SENSOR_SensorInfo *sensor;
596 struct GNUNET_SENSOR_SensorFullMessage *msg;
597 char *sensor_dir;
598 char *sensor_path;
599 char *sensorscript_path;
600 uint64_t sensorfile_size;
601 uint64_t sensorscriptname_size;
602 uint64_t sensorscript_size;
603 uint64_t total_size;
604 void *dummy;
605
606 GNUNET_CRYPTO_hash (sensorname, strlen (sensorname) + 1, &key);
607 sensor = GNUNET_CONTAINER_multihashmap_get (sensors, &key);
608 if (NULL == sensor)
609 return NULL;
610 sensor_dir = GNUNET_SENSOR_get_sensor_dir ();
611 GNUNET_asprintf (&sensor_path, "%s%s",
612 sensor_dir, sensorname);
613 if (GNUNET_OK != GNUNET_DISK_file_size (sensor_path,
614 &sensorfile_size,
615 GNUNET_NO,
616 GNUNET_YES))
617 {
618 GNUNET_free (sensor_dir);
619 GNUNET_free (sensor_path);
620 return NULL;
621 }
622 sensorscript_size = 0;
623 sensorscriptname_size = 0;
624 /* Test if there is an associated script */
625 if (NULL != sensor->ext_process)
626 {
627 GNUNET_asprintf (&sensorscript_path, "%s%s-files%s%s",
628 sensor_dir,
629 sensor->name,
630 DIR_SEPARATOR_STR,
631 sensor->ext_process);
632 if (GNUNET_OK == GNUNET_DISK_file_size (sensorscript_path,
633 &sensorscript_size,
634 GNUNET_NO,
635 GNUNET_YES))
636 sensorscriptname_size = strlen (sensor->ext_process) + 1;
637 }
638 /* Construct the msg */
639 total_size = sizeof (struct GNUNET_SENSOR_SensorFullMessage) +
640 sensorfile_size + sensorscriptname_size + sensorscript_size;
641 msg = GNUNET_malloc (total_size);
642 msg->header.size = htons (total_size);
643 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SENSOR_FULL);
644 msg->cfg_size = htons (sensorfile_size);
645 msg->scriptname_size = htons (sensorscriptname_size);
646 msg->script_size = htons (sensorscript_size);
647 dummy = &msg[1];
648 GNUNET_DISK_fn_read (sensor_path, dummy, sensorfile_size);
649 dummy += sensorfile_size;
650 if (sensorscriptname_size > 0)
651 {
652 memcpy (dummy, sensor->ext_process, sensorscriptname_size);
653 dummy += sensorscriptname_size;
654 GNUNET_DISK_fn_read (sensorscript_path, dummy, sensorscript_size);
655 }
656 GNUNET_free (sensor_dir);
657 GNUNET_free (sensor_path);
658 GNUNET_free (sensorscript_path);
659 return msg;
660}
661
662
663/**
664 * Called with any request for full sensor information.
665 *
666 * Each time the function must call #GNUNET_CADET_receive_done on the channel
667 * in order to receive the next message. This doesn't need to be immediate:
668 * can be delayed if some processing is done on the message.
669 *
670 * @param cls Closure (set from #GNUNET_CADET_connect).
671 * @param channel Connection to the other end.
672 * @param channel_ctx Place to store local state associated with the channel.
673 * @param message The actual message.
674 * @return #GNUNET_OK to keep the channel open,
675 * #GNUNET_SYSERR to close it (signal serious error).
676 */
677static int
678handle_sensor_full_req (void *cls,
679 struct GNUNET_CADET_Channel *channel,
680 void **channel_ctx,
681 const struct GNUNET_MessageHeader *message)
682{
683 struct ClientPeerContext *cp = *channel_ctx;
684 struct GNUNET_SENSOR_SensorBriefMessage *sbm = NULL;
685 struct GNUNET_SENSOR_SensorFullMessage *sfm;
686 uint16_t msg_size;
687 uint16_t sensorname_size;
688
689 msg_size = ntohs (message->size);
690 /* parse & error check */
691 if (msg_size > sizeof (struct GNUNET_SENSOR_SensorBriefMessage))
692 {
693 sbm = (struct GNUNET_SENSOR_SensorBriefMessage *)message;
694 sensorname_size = ntohs (sbm->name_size);
695 if (msg_size != sizeof (struct GNUNET_SENSOR_SensorBriefMessage) +
696 sensorname_size)
697 sbm = NULL;
698 }
699 if (NULL == sbm)
700 {
701 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
702 "Received an invalid full sensor request from peer `%s'.\n",
703 GNUNET_i2s (&cp->peerid));
704 return GNUNET_SYSERR;
705 }
706 /* Create and send msg with full sensor info */
707 sfm = create_full_sensor_msg ((char *)&sbm[1]);
708 if (NULL == sfm)
709 {
710 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
711 "Error creating full sensor info msg for sensor `%s'.\n",
712 (char *)&sbm[1]);
713 return GNUNET_SYSERR;
714 }
715 queue_msg ((struct GNUNET_MessageHeader *)sfm, cp);
716 GNUNET_CADET_receive_done (channel);
717 return GNUNET_OK;
718}
719
720
721/**
586 * Process sensordashboard requests. 722 * Process sensordashboard requests.
587 * 723 *
588 * @param cls closure 724 * @param cls closure
@@ -602,6 +738,9 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
602 {&handle_sensor_list_req, 738 {&handle_sensor_list_req,
603 GNUNET_MESSAGE_TYPE_SENSOR_LIST_REQ, 739 GNUNET_MESSAGE_TYPE_SENSOR_LIST_REQ,
604 sizeof (struct GNUNET_MessageHeader)}, 740 sizeof (struct GNUNET_MessageHeader)},
741 {&handle_sensor_full_req,
742 GNUNET_MESSAGE_TYPE_SENSOR_FULL_REQ,
743 sizeof (struct GNUNET_MessageHeader)},
605 {NULL, 0, 0} 744 {NULL, 0, 0}
606 }; 745 };
607 static uint32_t cadet_ports[] = { 746 static uint32_t cadet_ports[] = {