/** * \brief before sending datastore information about the time and the stream * \param writer the netwriter to send data * \param ms the stream to store the measruement from * \param now a timestamp that represensent the current time * \return 1 if succesfull, 0 otherwise */ int row_start(OmlWriter* writer, OmlMStream* ms, double now) { OmlTextWriter* self = (OmlTextWriter*)writer; assert(self->bufferedWriter != NULL); MBuffer* mbuf; if ((mbuf = self->mbuf = bw_get_write_buf(self->bufferedWriter, 1)) == NULL) return 0; mbuf_begin_write(mbuf); if (mbuf_print(mbuf, "%f\t%d\t%ld", now, ms->index, ms->seq_no)) { mbuf_reset_write(mbuf); self->mbuf = NULL; return 0; } return 1; }
/** * \brief write the data after finalysing the data structure * \param writer the net writer that send the measurements * \param ms the stream of measurmenent * \return 1 if successful, 0 otherwise */ int row_end(OmlWriter* writer, OmlMStream* ms) { (void)ms; OmlTextWriter* self = (OmlTextWriter*)writer; MBuffer* mbuf; if ((mbuf = self->mbuf) == NULL) return 0; /* previous use of mbuf failed */ int res; if ((res = mbuf_write(mbuf, (uint8_t*)"\n", 1)) != 0) { mbuf_reset_write(mbuf); } else { // success, lock in message mbuf_begin_write (mbuf); } self->mbuf = NULL; bw_unlock_buf(self->bufferedWriter); return res == 0; }
/** Function called after all items in a tuple have been sent * \see oml_writer_row_end * * This releases the lock on the BufferedWriter. * * \see BufferedWriter, bw_unlock_buf, marshal_finalize */ static int owb_row_end(OmlWriter* writer, OmlMStream* ms) { (void)ms; OmlBinWriter* self = (OmlBinWriter*)writer; MBuffer* mbuf; if ((mbuf = self->mbuf) == NULL) { return 0; /* previous use of mbuf failed */ } marshal_finalize(self->mbuf); if (marshal_get_msgtype (self->mbuf) == OMB_LDATA_P) { self->msgtype = OMB_LDATA_P; // Generate long packets from now on. } if (0 == ms->index) { /* This is schema0, also push the data into the meta_buf * to be replayed after a disconnection. * * At the moment, the oml_outs_write_f takes header information as a * whole, but does not push more once it has sent the initial block. Its * two last parameters are only used to resend the entirety of the headers * when a disconnection does occur, nothing before. * * We therefore send the extra piece of data the normal way, but also * record it, separately, in the meta_buf * * XXX: This logic should be in higher layer levels, but given the current * implementation, with some of it already spread down into the * OmlOutStream (oml_outs_write_f), this require a much bigger refactoring. * It is also duplicated with the OmlTextWriter (see #1101). */ _bw_push_meta(self->bufferedWriter, mbuf_message(self->mbuf), mbuf_message_length(self->mbuf)); } mbuf_begin_write(mbuf); self->mbuf = NULL; bw_unlock_buf(self->bufferedWriter); return 1; }
/** Initialise the MBuffer to serialise a new measurement packet, starting at * the current write pointer. * * Two basic types (OmlBinMsgType) of packets are available, short and long. * Short packets (OMB_DATA_P) can contain up to UINT16_MAX, whislt long packets * (OMB_LDATA_P) extend this to UINT32_MAX. * * Packets headers start with two SYNC_BYTEs (0xAA), then the packet type * (OMB_DATA_P or OMB_LDATA_P). * - OMB_DATA_P headers are 5 bytes long, the last two bytes containing the * size of the message (including headers) as a 16-bit integer. * - OMB_LDATA_P headers are 7 bytes long, the last four bytes containing the * size of the message (including headers) as a 32-bit integer. * * Metadata about the stream can then be packed in with marshal_measurements(). * * This function can fail if there is a memory allocation failure or if the * buffer is misconfigured. * * \param mbuf MBuffer to serialize into * \param msgtype OmlBinMsgType of packet to build * \return 0 on success, -1 on failure * \see marshal_header_short, marshal_header_long, marshal_measurements */ int marshal_init(MBuffer *mbuf, OmlBinMsgType msgtype) { int result; if (mbuf == NULL) return -1; result = mbuf_begin_write (mbuf); if (result == -1) { logerror("Couldn't start marshalling packet (mbuf_begin_write())\n"); return -1; } switch (msgtype) { case OMB_DATA_P: result = marshal_header_short (mbuf); break; case OMB_LDATA_P: result = marshal_header_long (mbuf); break; } if (result == -1) { logerror("Error when trying to marshal packet header\n"); return -1; } return 0; }