/** Create a new out stream for sending over the network * \param transport string representing the protocol used to establish the connection (oml_strndup()'d locally) * \param hostname string representing the host to connect to (oml_strndup()'d locally) * \param service symbolic name or port number of the service to connect to (oml_strndup()'d locally) * \return a new OmlOutStream instance * * \see oml_strndup */ OmlOutStream* net_stream_new(const char *transport, const char *hostname, const char *service) { MString *dest; assert(transport != NULL && hostname != NULL && service != NULL); OmlNetOutStream* self = (OmlNetOutStream *)oml_malloc(sizeof(OmlNetOutStream)); memset(self, 0, sizeof(OmlNetOutStream)); dest = mstring_create(); mstring_sprintf(dest, "%s://%s:%s", transport, hostname, service); self->dest = (char*)oml_strndup (mstring_buf(dest), mstring_len(dest)); mstring_delete(dest); self->protocol = (char*)oml_strndup (transport, strlen (transport)); self->host = (char*)oml_strndup (hostname, strlen (hostname)); self->service = (char*)oml_strndup (service, strlen (service)); logdebug("%s: Created OmlNetOutStream\n", self->dest); socket_set_non_blocking_mode(0); /* // Now see if we can connect to server */ /* if (! open_socket(self)) { */ /* free(self); */ /* return NULL; */ /* } */ self->write = net_stream_write; self->close = net_stream_close; return (OmlOutStream*)self; }
/** Create a new out stream for writing into a local file. * * \param file destination file (oml_strndup()'d locally) * \return a new OmlOutStream instance * * \see oml_strndup */ OmlOutStream* file_stream_new(const char *file) { MString *dest; OmlFileOutStream* self = (OmlFileOutStream *)oml_malloc(sizeof(OmlFileOutStream)); memset(self, 0, sizeof(OmlFileOutStream)); loginfo ("File_stream: opening local storage file '%s'\n", file); if (strcmp(file, "stdout") == 0 || strcmp(file, "-") == 0) { self->f = stdout; } else { if ((self->f = fopen(file, "a+")) == NULL) { logerror ("Can't open local storage file '%s'\n", file); return 0; } } dest = mstring_create(); mstring_sprintf(dest, "file:%s", file); self->dest = (char*)oml_strndup (mstring_buf(dest), mstring_len(dest)); mstring_delete(dest); self->write = file_stream_write; self->close = file_stream_close; return (OmlOutStream*)self; }
void free_mstring(char *cp) { #ifdef DEBUG if (mstring_magic(cp) != MSTRING_MAGIC) fatal("Bad m-magic: %lx %x\n", mstring_magic(cp), MSTRING_MAGIC); #endif if (mstring_count(cp) != 0) { allocd_strings_malloced--; allocd_bytes_malloced -= mstring_header + mstring_len(cp) + 1; if (--mstring_count(cp) == 0) { #ifdef DEBUG mstring_magic(cp) = 0; #endif num_distinct_strings_malloced--; bytes_distinct_strings_malloced -= mstring_len(cp) + 1; overhead_bytes_malloced -= mstring_header; free(cp - mstring_header); } } }
/** * \brief Definition of the meta function of the oml net writer * \param writer the net writer that will send the data to the server * \param str the string to send * \return 1 if successful, 0 otherwise */ static int meta(OmlWriter* writer, char* str) { OmlTextWriter* self = (OmlTextWriter*)writer; if (self->bufferedWriter == NULL) return 0; MString *mstr = mstring_create(); mstring_set (mstr, str); mstring_cat (mstr, "\n"); bw_push_meta(self->bufferedWriter, (uint8_t*)mstring_buf (mstr), mstring_len (mstr)); mstring_delete (mstr); return 1; }
char * reference_mstring(char *cp) { #ifdef DEBUG if (mstring_magic(cp) != MSTRING_MAGIC) fatal("Bad m-magic: %lx %x\n", mstring_magic(cp), MSTRING_MAGIC); #endif if (mstring_count(cp) != 0) mstring_count(cp)++; allocd_strings_malloced++; allocd_bytes_malloced += mstring_header + mstring_len(cp) + 1; return cp; }
char * allocate_mstring(size_t len) { char *cp; if (len < 0 || len > MAX_STRING_SIZE) error("Illegal string size.\n"); cp = (char *)xalloc(mstring_header + len + 1) + mstring_header; #ifdef DEBUG mstring_magic(cp) = MSTRING_MAGIC; #endif mstring_count(cp) = 1; mstring_len(cp) = len; allocd_strings_malloced++; allocd_bytes_malloced += mstring_header + len + 1; num_distinct_strings_malloced++; bytes_distinct_strings_malloced += len + 1; overhead_bytes_malloced += mstring_header; return cp; }
/** Helper function to build and send a database-related event to the server hook. * * \param self Database on which the event happened * \param event event to report * * \return 0 on success (even if the hook is disabled), -1 otherwise * * \see HOOK_CMD_DBCREATED, HOOK_CMD_DBOPENED, HOOK_CMD_DBCLOSED */ static int database_hook_send_event(Database *self, const char* event){ char dburi[PATH_MAX+1]; MString *hook_command = mstring_create(); if(hook_enabled()) { if(!self->get_uri(self, dburi, sizeof(dburi))) { logwarn("%s: Unable to get full URI to database for hook\n", self->name); } else if (mstring_sprintf(hook_command, "%s %s\n", event, dburi) == -1) { logwarn("%s: Failed to construct command string for event hook\n", self->name); mstring_delete(hook_command); return -1; } else if(hook_write(mstring_buf(hook_command), mstring_len(hook_command)) < (int)mstring_len(hook_command)) { logwarn("%s: Failed to send command string to event hook: %s\n", self->name, strerror(errno)); mstring_delete(hook_command); return -1; } } return 0; }