Exemplo n.º 1
0
cmyth_chain_t
cmyth_chain_create(cmyth_recorder_t rec, char *chain_id)
{
	cmyth_chain_t chain;
	struct event_loop_args *args;

	args = ref_alloc(sizeof(*args));

	chain = ref_alloc(sizeof(*chain));

	chain->chain_id = ref_strdup(chain_id);
	chain->chain_count = 0;
	chain->chain_current = -1;
	chain->chain_list = NULL;
	chain->chain_callback = NULL;
	chain->chain_event = NULL;
	chain->chain_thread = 0;
	chain->chain_conn = ref_hold(rec->rec_conn);

	pthread_mutex_init(&chain->chain_mutex, NULL);
	pthread_cond_init(&chain->chain_cond, NULL);

	ref_set_destroy(chain, (ref_destroy_t)cmyth_chain_destroy);

	args->rec = ref_hold(rec);
	args->chain = ref_hold(chain);

	chain->chain_thread_args = (void*)args;

	pthread_create(&chain->chain_thread, NULL,
		       cmyth_chain_event_loop, (void*)args);

	return chain;
}
Exemplo n.º 2
0
/**
 * Allocate a dynamic query string to have parameters added to it
 * \param db database connection object
 * \param query_string Query string with ? placemarks for all dynamic
 * 			parameters, this is NOT copied and must therefore
 * 			remain valid for the life of the query.
 */
cmyth_mysql_query_t * 
cmyth_mysql_query_create(cmyth_database_t db, const char * query_string)
{
    cmyth_mysql_query_t * out;
    out = ref_alloc(sizeof(*out));
    if(out != NULL)
    {
	ref_set_destroy(out,query_destroy);
	out->source = out->source_pos = query_string;
	out->source_len = strlen(out->source);
	out->buf_size = out->source_len *2;
	out->buf_used = 0;
	out->db = ref_hold(db);
	out->buf = ref_alloc(out->buf_size);
	if(out->buf == NULL)
	{
	    ref_release(out);
	    out = NULL;
	}
	else
	{
	    out->buf[0] = '\0';
	}

    }
    return out;
}
Exemplo n.º 3
0
/*
 * cmyth_livetv_chain_add_url(cmyth_recorder_t rec, char * url)
 * 
 * Scope: PRIVATE
 *
 * Description
 *
 * Called to add a url to a livetv chain structure. The url is added
 * only if it is not already there.
 *
 * Return Value:
 *
 * Success: 0
 *
 * Faiure: -1
 */
static int
cmyth_livetv_chain_add_url(cmyth_recorder_t rec, char * url)
{
	char ** tmp;
	cmyth_file_t * fp;
	cmyth_proginfo_t * pi;
	int ret = 0;

	if(cmyth_livetv_chain_has_url(rec,url) == -1) {
		if(rec->rec_livetv_chain->chain_current == -1) {
			rec->rec_livetv_chain->chain_ct = 1;
			rec->rec_livetv_chain->chain_current = 0;
			/* Nothing in the chain yet, allocate the space */
			tmp = (char**)ref_alloc(sizeof(char *));
			fp = (cmyth_file_t *)ref_alloc(sizeof(cmyth_file_t));
			pi = (cmyth_proginfo_t *)ref_alloc(sizeof(cmyth_proginfo_t));
		}
		else {
			rec->rec_livetv_chain->chain_ct++;
			tmp = (char**)ref_realloc(rec->rec_livetv_chain->chain_urls,
								sizeof(char *)*rec->rec_livetv_chain->chain_ct);
			fp = (cmyth_file_t *)
					ref_realloc(rec->rec_livetv_chain->chain_files,
								sizeof(cmyth_file_t)*rec->rec_livetv_chain->chain_ct);
			pi = (cmyth_proginfo_t *)
					ref_realloc(rec->rec_livetv_chain->progs,
								sizeof(cmyth_proginfo_t)*rec->rec_livetv_chain->chain_ct);
		}
		if(tmp != NULL && fp != NULL) {
			rec->rec_livetv_chain->chain_urls = ref_hold(tmp);
			rec->rec_livetv_chain->chain_files = ref_hold(fp);
			rec->rec_livetv_chain->progs = ref_hold(pi);
			ref_release(tmp);
			ref_release(fp);
			ref_release(pi);
			rec->rec_livetv_chain->chain_urls[rec->rec_livetv_chain->chain_ct-1]
							= ref_strdup(url);
			rec->rec_livetv_chain->chain_files[rec->rec_livetv_chain->chain_ct-1]
							= ref_hold(NULL);
			rec->rec_livetv_chain->progs[rec->rec_livetv_chain->chain_ct-1]
							= ref_hold(NULL);
		}
		else {
			ret = -1;
			cmyth_dbg(CMYTH_DBG_ERROR,
			 		"%s: memory allocation request failed\n",
			 		__FUNCTION__);
		}

	}
	return ret;
}
Exemplo n.º 4
0
int
cmyth_mysql_insert_into_record(cmyth_database_t db, char * query, char * query1, char * query2, char *title, char * subtitle, char * description, char * callsign)
{
	int rows=0;
	char *N_title;
	char *N_subtitle;
	char *N_description;
	char *N_callsign;
	char N_query[2570];

	if(cmyth_db_check_connection(db) != 0)
	{
               cmyth_dbg(CMYTH_DBG_ERROR, "%s: cmyth_db_check_connection failed\n",
                           __FUNCTION__);
               fprintf(stderr,"%s: cmyth_db_check_connection failed\n", __FUNCTION__);
	       return -1;
	}

	N_title = ref_alloc(strlen(title)*2+1);
	mysql_real_escape_string(db->mysql,N_title,title,strlen(title)); 
	N_subtitle = ref_alloc(strlen(subtitle)*2+1);
	mysql_real_escape_string(db->mysql,N_subtitle,subtitle,strlen(subtitle)); 
	N_description = ref_alloc(strlen(description)*2+1);
	mysql_real_escape_string(db->mysql,N_description,description,strlen(description)); 
	N_callsign = ref_alloc(strlen(callsign)*2+1);
	mysql_real_escape_string(db->mysql,N_callsign,callsign,strlen(callsign)); 

	snprintf(N_query,2500,"%s '%s','%s','%s' %s '%s' %s",query,N_title,N_subtitle,N_description,query1,N_callsign,query2); 
	ref_release(N_title);
	ref_release(N_subtitle);
	ref_release(N_callsign);
	cmyth_dbg(CMYTH_DBG_ERROR, "mysql query :%s\n",N_query);

        if(mysql_real_query(db->mysql,N_query,(unsigned int) strlen(N_query))) {
                cmyth_dbg(CMYTH_DBG_ERROR, "%s: mysql_query() Failed: %s\n", 
                           __FUNCTION__, mysql_error(db->mysql));
		return -1;
	}
	rows=mysql_insert_id(db->mysql);

	if (rows <=0) {
        	cmyth_dbg(CMYTH_DBG_ERROR, "%s: mysql_query() Failed: %s\n", 
                	__FUNCTION__, mysql_error(db->mysql));
	}


	return rows;
}
Exemplo n.º 5
0
static char *
cmyth_conn_get_setting_unlocked(cmyth_conn_t conn, const char* hostname, const char* setting)
{
	char msg[256];
	int count, err;
	char* result = NULL;

	if(conn->conn_version < 17) {
		cmyth_dbg(CMYTH_DBG_ERROR, "%s: protocol version doesn't support QUERY_SETTING\n",
			  __FUNCTION__);
		return NULL;
	}

	if (!conn) {
		cmyth_dbg(CMYTH_DBG_ERROR, "%s: no connection\n",
			  __FUNCTION__);
		return NULL;
	}

	snprintf(msg, sizeof(msg), "QUERY_SETTING %s %s", hostname, setting);
	if ((err = cmyth_send_message(conn, msg)) < 0) {
		cmyth_dbg(CMYTH_DBG_ERROR,
			  "%s: cmyth_send_message() failed (%d)\n",
			  __FUNCTION__, err);
		goto err;
	}

	if ((count=cmyth_rcv_length(conn)) < 0) {
		cmyth_dbg(CMYTH_DBG_ERROR,
			  "%s: cmyth_rcv_length() failed (%d)\n",
			  __FUNCTION__, count);
		goto err;
	}

	result = ref_alloc(count+1);
	count -= cmyth_rcv_string(conn, &err,
				    result, count, count);
	if (err < 0) {
		cmyth_dbg(CMYTH_DBG_ERROR,
			  "%s: cmyth_rcv_string() failed (%d)\n",
			  __FUNCTION__, err);
		goto err;
	}

	while(count > 0 && !err) {
		char buffer[100];
		count -= cmyth_rcv_string(conn, &err, buffer, sizeof(buffer)-1, count);
		buffer[sizeof(buffer)-1] = 0;
		cmyth_dbg(CMYTH_DBG_ERROR, "%s: odd left over data %s\n", __FUNCTION__, buffer);
	}

	return result;
err:
	if(result)
		ref_release(result);

	return NULL;
}
Exemplo n.º 6
0
/*
 * cmyth_freespace_create()
 *
 * Scope: PUBLIC
 *
 * Description
 *
 * Create a key frame structure.
 *
 * Return Value:
 *
 * Success: A non-NULL cmyth_freespace_t (this type is a pointer)
 *
 * Failure: A NULL cmyth_freespace_t
 */
cmyth_freespace_t
cmyth_freespace_create(void)
{
	cmyth_freespace_t ret = ref_alloc(sizeof(*ret));
	cmyth_dbg(CMYTH_DBG_DEBUG, "%s\n", __FUNCTION__);
 	if (!ret) {
	       return NULL;
	}

	ret->freespace_total = 0;
	ret->freespace_used = 0;
	return ret;
}
Exemplo n.º 7
0
cmyth_database_t
cmyth_database_init(char *host, char *db_name, char *user, char *pass)
{
	cmyth_database_t rtrn = ref_alloc(sizeof(*rtrn));
	cmyth_dbg(CMYTH_DBG_DEBUG, "%s\n", __FUNCTION__);

	if (rtrn != NULL) {
	    rtrn->db_host = ref_strdup(host);
	    rtrn->db_user = ref_strdup(user);
	    rtrn->db_pass = ref_strdup(pass);
	    rtrn->db_name = ref_strdup(db_name);
	}

	return rtrn;
}
Exemplo n.º 8
0
cmyth_storagegroup_file_t
cmyth_storagegroup_file_create(void)
{
	cmyth_storagegroup_file_t ret = ref_alloc(sizeof(*ret));
	memset(ret, 0, sizeof(*ret));

	cmyth_dbg(CMYTH_DBG_DEBUG, "%s {\n", __FUNCTION__);
	if (!ret) {
		cmyth_dbg(CMYTH_DBG_DEBUG, "%s }!\n", __FUNCTION__);
		return NULL;
	}
	ref_set_destroy(ret, (ref_destroy_t)cmyth_storagegroup_file_destroy);

	return ret;
}
Exemplo n.º 9
0
/*
 * cmyth_recordingrule_create()
 *
 * Scope: PRIVATE
 *
 * Description
 *
 * Create a recording schedule structure to be used to hold channel and return
 * a pointer to the structure.
 * Before forgetting the reference to this recording schedule structure the
 * caller must call ref_release().
 *
 * Return Value:
 *
 * Success: A non-NULL cmyth_channel_t (this type is a pointer)
 *
 * Failure: NULL
 */
cmyth_recordingrule_t
cmyth_recordingrule_create(void)
{
	cmyth_recordingrule_t ret = ref_alloc(sizeof(*ret));
	memset(ret, 0, sizeof(*ret));

	cmyth_dbg(CMYTH_DBG_DEBUG, "%s\n", __FUNCTION__);
	if (!ret) {
		cmyth_dbg(CMYTH_DBG_DEBUG, "%s: ref_alloc() failed\n", __FUNCTION__);
		return NULL;
	}
	ref_set_destroy(ret, (ref_destroy_t)cmyth_recordingrule_destroy);

	return ret;
}
Exemplo n.º 10
0
/*
 * cmyth_keyframe_create(void)
 * 
 * Scope: PUBLIC
 *
 * Description
 *
 * Create a key frame structure.
 *
 * Return Value:
 *
 * Success: A non-NULL cmyth_keyframe_t (this type is a pointer)
 *
 * Failure: A NULL cmyth_keyframe_t
 */
cmyth_keyframe_t
cmyth_keyframe_create(void)
{
	cmyth_keyframe_t ret = ref_alloc(sizeof(*ret));

	cmyth_dbg(CMYTH_DBG_DEBUG, "%s {\n", __FUNCTION__);
	if (!ret) {
		cmyth_dbg(CMYTH_DBG_DEBUG, "%s } !\n", __FUNCTION__);
		return NULL;
	}
	ret->keyframe_number = 0;
	ret->keyframe_pos = 0;
	cmyth_dbg(CMYTH_DBG_DEBUG, "%s }\n", __FUNCTION__);
	return ret;
}
Exemplo n.º 11
0
/*
 * cmyth_posmap_create(void)
 * 
 * Scope: PUBLIC
 *
 * Description
 *
 * Allocate and initialize a position map structure.
 *
 * Return Value:
 *
 * Success: A non-NULL cmyth_posmap_t (this type is a pointer)
 *
 * Failure: A NULL cmyth_posmap_t
 */
cmyth_posmap_t
cmyth_posmap_create(void)
{
	cmyth_posmap_t ret = ref_alloc(sizeof(*ret));

	cmyth_dbg(CMYTH_DBG_DEBUG, "%s\n", __FUNCTION__);
	if (!ret) {
		return NULL;
	}
	ref_set_destroy(ret, (ref_destroy_t)cmyth_posmap_destroy);

	ret->posmap_count = 0;
	ret->posmap_list = NULL;
	return ret;
}
Exemplo n.º 12
0
cmyth_commbreaklist_t
cmyth_commbreaklist_create(void)
{
	cmyth_commbreaklist_t ret;

	cmyth_dbg(CMYTH_DBG_DEBUG, "%s\n", __FUNCTION__);
	ret = ref_alloc(sizeof(*ret));
	if (!ret) {
		return(NULL);
	}
	ref_set_destroy(ret, (ref_destroy_t)cmyth_commbreaklist_destroy);

	ret->commbreak_list = NULL;
	ret->commbreak_count = 0;
	return ret;
}
Exemplo n.º 13
0
/*
 * cmyth_rec_num_create(void)
 * 
 * Scope: PUBLIC
 *
 * Description
 *
 * Create a recorder number structure.
 *
 * Return Value:
 *
 * Success: A non-NULL cmyth_rec_num_t (this type is a pointer)
 *
 * Failure: A NULL cmyth_rec_num_t
 */
cmyth_rec_num_t
cmyth_rec_num_create(void)
{
	cmyth_rec_num_t ret = ref_alloc(sizeof(*ret));

	cmyth_dbg(CMYTH_DBG_DEBUG, "%s\n", __FUNCTION__);
	if (!ret) {
		return NULL;
	}
	ref_set_destroy(ret, (ref_destroy_t)cmyth_rec_num_destroy);

	ret->recnum_host = NULL;
	ret->recnum_port = 0;
	ret->recnum_id = 0;
	return ret;
}
Exemplo n.º 14
0
/*
 * cmyth_proglist_create()
 *
 * Scope: PUBLIC
 *
 * Description
 *
 * Create a timestamp structure and return a pointer to the structure.
 *
 * Return Value:
 *
 * Success: A non-NULL cmyth_proglist_t (this type is a pointer)
 *
 * Failure: A NULL cmyth_proglist_t
 */
cmyth_proglist_t
cmyth_proglist_create(void)
{
    cmyth_proglist_t ret;

    cmyth_dbg(CMYTH_DBG_DEBUG, "%s\n", __FUNCTION__);
    ret = ref_alloc(sizeof(*ret));
    if (!ret) {
        return(NULL);
    }
    ref_set_destroy(ret, (ref_destroy_t)cmyth_proglist_destroy);

    ret->proglist_list = NULL;
    ret->proglist_count = 0;
    pthread_mutex_init(&ret->proglist_mutex, NULL);
    return ret;
}
Exemplo n.º 15
0
/*
 * cmyth_timestamp_create(void)
 *
 * Scope: PUBLIC
 *
 * Description
 *
 * Create a timestamp structure and return a pointer to the structure.
 *
 * Return Value:
 *
 * Success: A non-NULL cmyth_timestamp_t (this type is a pointer)
 *
 * Failure: A NULL cmyth_timestamp_t
 */
cmyth_timestamp_t
cmyth_timestamp_create(void)
{
	cmyth_timestamp_t ret = ref_alloc(sizeof(*ret));

	cmyth_dbg(CMYTH_DBG_DEBUG, "%s\n", __FUNCTION__);
	if (!ret) {
		return(NULL);
	}
	ret->timestamp_year = 0;
	ret->timestamp_month = 0;
	ret->timestamp_day = 0;
	ret->timestamp_hour = 0;
	ret->timestamp_minute = 0;
	ret->timestamp_second = 0;
	ret->timestamp_isdst = 0;
	return ret;
}
Exemplo n.º 16
0
cmyth_commbreak_t
cmyth_commbreak_create(void)
{
	cmyth_commbreak_t ret = ref_alloc(sizeof(*ret));

	cmyth_dbg(CMYTH_DBG_DEBUG, "%s {\n", __FUNCTION__);
	if (!ret) {
		cmyth_dbg(CMYTH_DBG_DEBUG, "%s }!\n", __FUNCTION__);
		return NULL;
	}
	ref_set_destroy(ret, (ref_destroy_t)cmyth_commbreak_destroy);

	ret->start_mark = 0;
	ret->start_offset = 0;
	ret->end_mark = 0;
	ret->end_offset = 0;

	cmyth_dbg(CMYTH_DBG_DEBUG, "%s }\n", __FUNCTION__);
	return ret;
}
Exemplo n.º 17
0
char *
cmyth_mysql_escape_chars(cmyth_database_t db, char * string) 
{
	char *N_string;
	size_t len;

	if(cmyth_db_check_connection(db) != 0)
	{
               cmyth_dbg(CMYTH_DBG_ERROR, "%s: cmyth_db_check_connection failed\n",
                           __FUNCTION__);
               fprintf(stderr,"%s: cmyth_db_check_connection failed\n", __FUNCTION__);
	       return NULL;
	}

	len = strlen(string);
	N_string=ref_alloc(len*2+1);
	mysql_real_escape_string(db->mysql,N_string,string,len); 

	return (N_string);
}
Exemplo n.º 18
0
/*
 * cmyth_livetv_chain_create(void)
 * 
 * Scope: PUBLIC
 *
 * Description
 *
 * Allocate and initialize a ring buffer structure.
 *
 * Return Value:
 *
 * Success: A non-NULL cmyth_livetv_chain_t (this type is a pointer)
 *
 * Failure: A NULL cmyth_livetv_chain_t
 */
cmyth_livetv_chain_t
cmyth_livetv_chain_create(char * chainid)
{
	cmyth_livetv_chain_t ret = ref_alloc(sizeof(*ret));

	cmyth_dbg(CMYTH_DBG_DEBUG, "%s\n", __FUNCTION__);
	if (!ret) {
		return NULL;
	}

	ret->chainid = ref_strdup(chainid);
	ret->chain_ct = 0;
	ret->chain_switch_on_create = 0;
	ret->chain_current = -1;
	ret->chain_urls = NULL;
	ret->chain_files = NULL;
	ret->progs = NULL;
	ref_set_destroy(ret, (ref_destroy_t)cmyth_livetv_chain_destroy);
	return ret;
}
Exemplo n.º 19
0
/*
 * cmyth_ringbuf_create(void)
 * 
 * Scope: PUBLIC
 *
 * Description
 *
 * Allocate and initialize a ring buffer structure.
 *
 * Return Value:
 *
 * Success: A non-NULL cmyth_ringbuf_t (this type is a pointer)
 *
 * Failure: A NULL cmyth_ringbuf_t
 */
cmyth_ringbuf_t
cmyth_ringbuf_create(void)
{
	cmyth_ringbuf_t ret = ref_alloc(sizeof(*ret));

	cmyth_dbg(CMYTH_DBG_DEBUG, "%s\n", __FUNCTION__);
	if (!ret) {
		return NULL;
	}

	ret->conn_data = NULL;
	ret->ringbuf_url = NULL;
	ret->ringbuf_size = 0;
	ret->ringbuf_fill = 0;
	ret->file_pos = 0;
	ret->file_id = 0;
	ret->ringbuf_hostname = NULL;
	ret->ringbuf_port = 0;
	ref_set_destroy(ret, (ref_destroy_t)cmyth_ringbuf_destroy);
	return ret;
}
Exemplo n.º 20
0
/*
 * cmyth_conn_create(void)
 * 
 * Scope: PRIVATE (static)
 *
 * Description
 *
 * Allocate and initialize a cmyth_conn_t structure.  This should only
 * be called by cmyth_connect(), which establishes a connection.
 *
 * Return Value:
 *
 * Success: A non-NULL cmyth_conn_t (this type is a pointer)
 *
 * Failure: A NULL cmyth_conn_t
 */
static cmyth_conn_t
cmyth_conn_create(void)
{
	cmyth_conn_t ret = ref_alloc(sizeof(*ret));

	cmyth_dbg(CMYTH_DBG_DEBUG, "%s {\n", __FUNCTION__);
	if (!ret) {
			cmyth_dbg(CMYTH_DBG_DEBUG, "%s }!\n", __FUNCTION__);
		return NULL;
	}
	ref_set_destroy(ret, (ref_destroy_t)cmyth_conn_destroy);

	ret->conn_fd = -1;
	ret->conn_buf = NULL;
	ret->conn_len = 0;
	ret->conn_buflen = 0;
	ret->conn_pos = 0;
	ret->conn_hang = 0;
	cmyth_dbg(CMYTH_DBG_DEBUG, "%s }\n", __FUNCTION__);
	return ret;
}
Exemplo n.º 21
0
cmyth_input_t
cmyth_input_create(void)
{
	cmyth_input_t ret = ref_alloc(sizeof(*ret));

	cmyth_dbg(CMYTH_DBG_DEBUG, "%s {\n", __FUNCTION__);
	if (!ret) {
		cmyth_dbg(CMYTH_DBG_DEBUG, "%s }!\n", __FUNCTION__);
		return NULL;
	}
	ref_set_destroy(ret, (ref_destroy_t)cmyth_input_destroy);

	ret->inputname = NULL;
	ret->sourceid = 0;
	ret->inputid = 0;
	ret->cardid = 0;
	ret->multiplexid = 0;

	cmyth_dbg(CMYTH_DBG_DEBUG, "%s }\n", __FUNCTION__);
	return ret;
}
Exemplo n.º 22
0
static void
parse_progs(struct myth_conn *c)
{
	int i, j, count;
	struct prog_map *progs;

	count = cmyth_proglist_get_count(c->list);

	progs = ref_alloc(sizeof(*progs)*count);

	for (i=0; i<count; i++) {
		cmyth_proginfo_t prog;
		char *title, *subtitle;
		prog = cmyth_proglist_get_item(c->list, i);
		title = cmyth_proginfo_title(prog);
		subtitle = cmyth_proginfo_subtitle(prog);
		progs[i].prog = ref_hold(prog);
		progs[i].suffix = 0;
		for (j=0; j<i; j++) {
			char *t, *s;
			t = cmyth_proginfo_title(progs[j].prog);
			s = cmyth_proginfo_subtitle(progs[j].prog);
			if ((strcmp(title, t) == 0) &&
			    (strcmp(subtitle, s) == 0)) {
				progs[i].suffix++;
			}
			ref_release(t);
			ref_release(s);
		}
		ref_release(title);
		ref_release(subtitle);
		ref_release(prog);
	}

	for (i=0; i<c->nprogs; i++) {
		ref_release(c->progs[i].prog);
	}
	ref_release(c->progs);
	c->progs = progs;
}
Exemplo n.º 23
0
/*
 * cmyth_recorder_create()
 *
 * Scope: PUBLIC
 *
 * Description
 *
 * Allocate and initialize a cmyth recorder structure.
 *
 * Return Value:
 *
 * Success: A non-NULL cmyth_recorder_t (this type is a pointer)
 *
 * Failure: A NULL cmyth_recorder_t
 */
cmyth_recorder_t
cmyth_recorder_create(void)
{
	cmyth_recorder_t ret = ref_alloc(sizeof(*ret));

	cmyth_dbg(CMYTH_DBG_DEBUG, "%s\n", __FUNCTION__);
	if (!ret) {
		return NULL;
	}
	ref_set_destroy(ret, (ref_destroy_t)cmyth_recorder_destroy);

	ret->rec_server = NULL;
	ret->rec_port = 0;
	ret->rec_have_stream = 0;
	ret->rec_id = 0;
	ret->rec_ring = NULL;
	ret->rec_conn = NULL;
	ret->rec_framerate = 0.0;
	ret->rec_livetv_chain = NULL;
	ret->rec_livetv_file = NULL;
	return ret;
}
Exemplo n.º 24
0
Arquivo: file.c Projeto: tsp/cmyth
/*
 * cmyth_file_create(cmyth_conn_t control)
 * 
 * Scope: PRIVATE (mapped to __cmyth_file_create)
 *
 * Description
 *
 * Allocate and initialize a cmyth_file_t structure.  This should only
 * be called by cmyth_connect_file(), which establishes a file
 * transfer connection.
 *
 * Return Value:
 *
 * Success: A non-NULL cmyth_file_t (this type is a pointer)
 *
 * Failure: A NULL cmyth_file_t
 */
cmyth_file_t
cmyth_file_create(cmyth_conn_t control)
{
	cmyth_file_t ret = ref_alloc(sizeof(*ret));

	cmyth_dbg(CMYTH_DBG_DEBUG, "%s {\n", __FUNCTION__);
 	if (!ret) {
		cmyth_dbg(CMYTH_DBG_DEBUG, "%s }\n", __FUNCTION__);
 		return NULL;
	}
	ref_set_destroy(ret, (ref_destroy_t)cmyth_file_destroy);

	ret->file_control = ref_hold(control);
	ret->file_data = NULL;
	ret->file_id = -1;
	ret->file_start = 0;
	ret->file_length = 0;
	ret->file_pos = 0;
	ret->closed_callback = NULL;
	cmyth_dbg(CMYTH_DBG_DEBUG, "%s }\n", __FUNCTION__);
	return ret;
}
Exemplo n.º 25
0
/*
 * cmyth_livetv_chain_create(void)
 *
 * Scope: PUBLIC
 *
 * Description
 *
 * Allocate and initialize a ring buffer structure.
 *
 * Return Value:
 *
 * Success: A non-NULL cmyth_livetv_chain_t (this type is a pointer)
 *
 * Failure: A NULL cmyth_livetv_chain_t
 */
cmyth_livetv_chain_t
cmyth_livetv_chain_create(char * chainid)
{
    cmyth_livetv_chain_t ret = ref_alloc(sizeof(*ret));

    cmyth_dbg(CMYTH_DBG_DEBUG, "%s\n", __FUNCTION__);
    if (!ret) {
        return NULL;
    }

    ret->chainid = ref_strdup(chainid);
    ret->chain_ct = 0;
    ret->chain_switch_on_create = 0;
    ret->chain_current = -1;
    ret->chain_urls = NULL;
    ret->chain_files = NULL;
    ret->progs = NULL;
    ret->livetv_watch = 0; /* JLB: Manage program breaks. Set to 1 on chain setup */
    ret->livetv_tcp_rcvbuf = 0;
    ref_set_destroy(ret, (ref_destroy_t)cmyth_livetv_chain_destroy);
    return ret;
}
Exemplo n.º 26
0
static int lua_create_client(lua_State *L)
{
  lua_redis_client_t *lua_redis_client = NULL;
  redisAsyncContext *redis_async_context = NULL;
  luv_ref_t *ref;
  const char *host = luaL_checkstring(L, 1);
  int port = luaL_checkint(L, 2);

  redis_async_context = redisAsyncConnect(host, port);

  if(redis_async_context->err)
  {
    redisAsyncFree(redis_async_context);
    return luaL_error(L, redis_async_context->errstr);
  }

  redisLibevAttach(EV_DEFAULT_ redis_async_context);

  lua_redis_client = (lua_redis_client_t *)lua_newuserdata(
                                          L, sizeof(lua_redis_client_t));


  lua_redis_client->redis_async_context = redis_async_context;

  luaL_getmetatable(L, LUA_REDIS_CLIENT_MT);
  lua_setmetatable(L, -2);

  lua_newtable(L);
  lua_setfenv(L, -2);

  ref = ref_alloc();
  ref->L = L;
  lua_pushvalue(L, -1);
  ref->r = luaL_ref(L, LUA_REGISTRYINDEX);
  redis_async_context->data = ref;

  return 1;
}
Exemplo n.º 27
0
static int lua_client_command(lua_State *L)
{
  lua_redis_client_t *lua_redis_client = (lua_redis_client_t *)
                                    luaL_checkudata(L, 1, LUA_REDIS_CLIENT_MT);

  static const char *argv[LUA_REDIS_MAX_ARGS];
  static size_t argvlen[LUA_REDIS_MAX_ARGS];

  int nargs, ltop, i;
  luv_ref_t *ref = NULL;
  int commandStatus;
  redisCallbackFn *redisCallback = NULL;

  if (lua_redis_client->redis_async_context == NULL)
  {
    return luaL_error(L, "RedisAsyncContext is null");
  }

  /* consume callback, if any */
  if (lua_isfunction(L, -1))
  {
    ref = ref_alloc();
    ref->L = L;
    ref->r = luaL_ref(L, LUA_REGISTRYINDEX);
    redisCallback = on_redis_response;
  }

  /* get arguments */
  ltop = lua_gettop(L);
  nargs = 0;
  for (i = 2; i <= ltop; ++i) {
    /* unpack tables of arguments */
    if (lua_istable(L, i)) {
      int j;
      for (j = 1; j <= lua_objlen(L, i); ++j) {
        lua_rawgeti(L, i, j);
        argv[nargs] = lua_tolstring(L, -1, &argvlen[nargs]);
        lua_pop(L, 1);
        if (argv[nargs] == NULL) {
          return luaL_argerror(L, i,
              "expected an array table of string or number values"
            );
        }
        if (++nargs >= LUA_REDIS_MAX_ARGS) {
          return luaL_error(L, "too many arguments");
        }
      }
    } else {
      argv[nargs] = lua_tolstring(L, i, &argvlen[nargs]);
      if (argv[nargs] == NULL) {
        return luaL_argerror(L, i, "expected a string or number value");
      }
      if (++nargs >= LUA_REDIS_MAX_ARGS) {
        return luaL_error(L, "too many arguments");
      }
    }
  }
  if (nargs <= 0) {
    return luaL_error(L, "missing command name");
  }

  commandStatus = redisAsyncCommandArgv(lua_redis_client->redis_async_context,
                                        redisCallback,
                                        ref,
                                        nargs,
                                        argv,
                                        argvlen);

  if (commandStatus != REDIS_OK)
  {
    lua_rawgeti(L, LUA_REGISTRYINDEX, ref->r);
    luaL_unref(L, LUA_REGISTRYINDEX, ref->r);
    ref_free(ref);
    luv_push_async_error_raw(L, NULL, "Redis connection problem", "client_command", NULL);
    lua_pushnil(L);
    lua_call(L, 2, 0);
  }

  return 0;
}
Exemplo n.º 28
0
/*
 * cmyth_conn_connect_file(char *server, unsigned short port, unsigned buflen
 *                         cmyth_proginfo_t prog)
 *
 * Scope: PUBLIC
 *
 * Description:
 *
 * Create a file structure containing a data connection for use
 * transfering a file within the MythTV protocol.  Return a pointer to
 * the newly created file structure.  The connection in the file
 * structure is returned held as is the file structure itself.  The
 * connection will be released when the file structure is released.
 * The file structure can be released using ref_release().
 *
 * Return Value:
 *
 * Success: Non-NULL cmyth_file_t (this is a pointer type)
 *
 * Failure: NULL cmyth_file_t
 */
cmyth_file_t
cmyth_conn_connect_file(cmyth_proginfo_t prog,  cmyth_conn_t control,
			unsigned buflen, int tcp_rcvbuf)
{
	cmyth_conn_t conn = NULL;
	char *announcement = NULL;
	char *myth_host = NULL;
	char reply[16];
	int err = 0;
	int count = 0;
	int r;
	int ann_size = sizeof("ANN FileTransfer []:[][]:[]");
	cmyth_file_t ret = NULL;

	if (!prog) {
		cmyth_dbg(CMYTH_DBG_ERROR, "%s: prog is NULL\n", __FUNCTION__);
		goto shut;
	}
	if (!prog->proginfo_host) {
		cmyth_dbg(CMYTH_DBG_ERROR, "%s: prog host is NULL\n",
			  __FUNCTION__);
		goto shut;
	}
	if (!prog->proginfo_pathname) {
		cmyth_dbg(CMYTH_DBG_ERROR, "%s: prog has no pathname in it\n",
			  __FUNCTION__);
		goto shut;
	}
	ret = cmyth_file_create(control);
	if (!ret) {
		cmyth_dbg(CMYTH_DBG_ERROR, "%s: cmyth_file_create() failed\n",
			  __FUNCTION__);
		goto shut;
	}
	cmyth_dbg(CMYTH_DBG_PROTO, "%s: connecting data connection\n",
		  __FUNCTION__);
	if (control->conn_version >= 17) {
		myth_host = cmyth_conn_get_setting(control, prog->proginfo_host,
		                                   "BackendServerIP");
	}
	if (!myth_host) {
		cmyth_dbg(CMYTH_DBG_PROTO,
		          "%s: BackendServerIP setting not found. Using proginfo_host: %s\n",
		          __FUNCTION__, prog->proginfo_host);
		myth_host = ref_alloc(strlen(prog->proginfo_host) + 1);
		strcpy(myth_host, prog->proginfo_host);
	}
	conn = cmyth_connect(myth_host, prog->proginfo_port,
			     buflen, tcp_rcvbuf);
	cmyth_dbg(CMYTH_DBG_PROTO,
		  "%s: done connecting data connection, conn = %d\n",
		  __FUNCTION__, conn);
	if (!conn) {
		cmyth_dbg(CMYTH_DBG_ERROR,
			  "%s: cmyth_connect(%s, %d, %d) failed\n",
			  __FUNCTION__,
			  myth_host, prog->proginfo_port, buflen);
		goto shut;
	}
	ann_size += strlen(prog->proginfo_pathname) + strlen(my_hostname);
	announcement = malloc(ann_size);
	if (!announcement) {
		cmyth_dbg(CMYTH_DBG_ERROR,
			  "%s: malloc(%d) failed for announcement\n",
			  __FUNCTION__, ann_size);
		goto shut;
	}
	if (control->conn_version >= 44) {
		sprintf(announcement, "ANN FileTransfer %s[]:[]%s[]:[]",
			  my_hostname, prog->proginfo_pathname);
	}
	else {
		sprintf(announcement, "ANN FileTransfer %s[]:[]%s",
			  my_hostname, prog->proginfo_pathname);
	}

	if (cmyth_send_message(conn, announcement) < 0) {
		cmyth_dbg(CMYTH_DBG_ERROR,
			  "%s: cmyth_send_message('%s') failed\n",
			  __FUNCTION__, announcement);
		goto shut;
	}
	ret->file_data = ref_hold(conn);
	count = cmyth_rcv_length(conn);
	if (count < 0) {
		cmyth_dbg(CMYTH_DBG_ERROR,
			  "%s: cmyth_rcv_length() failed (%d)\n",
			  __FUNCTION__, count);
		goto shut;
	}
	reply[sizeof(reply) - 1] = '\0';
	r = cmyth_rcv_string(conn, &err, reply, sizeof(reply) - 1, count); 
	if (err != 0) {
		cmyth_dbg(CMYTH_DBG_ERROR,
			  "%s: cmyth_rcv_string() failed (%d)\n",
			  __FUNCTION__, err);
		goto shut;
	}
	if (strcmp(reply, "OK") != 0) {
		cmyth_dbg(CMYTH_DBG_ERROR, "%s: reply ('%s') is not 'OK'\n",
			  __FUNCTION__, reply);
		goto shut;
	}
	count -= r;
	r = cmyth_rcv_long(conn, &err, &ret->file_id, count);
	if (err) {
		cmyth_dbg(CMYTH_DBG_ERROR,
			  "%s: (id) cmyth_rcv_long() failed (%d)\n",
			  __FUNCTION__, err);
		goto shut;
	}
	count -= r;
	r = cmyth_rcv_u_long_long(conn, &err, &ret->file_length, count);
	if (err) {
		cmyth_dbg(CMYTH_DBG_ERROR,
			  "%s: (length) cmyth_rcv_longlong() failed (%d)\n",
			  __FUNCTION__, err);
		goto shut;
	}
	count -= r;
	free(announcement);
	ref_release(conn);
	ref_release(myth_host);
	return ret;

    shut:
	if (announcement) {
		free(announcement);
	}
	ref_release(ret);
	ref_release(conn);
	ref_release(myth_host);
	return NULL;
}
Exemplo n.º 29
0
static void
cmyth_chain_update(cmyth_chain_t chain, cmyth_recorder_t rec, char *msg)
{
	char *p;
	cmyth_proginfo_t prog = NULL;
	cmyth_chain_entry_t entry;
	int size, tip;
	long long offset;
	int start = 0;
	char *path;

	if ((p=strchr(msg, ' ')) != NULL) {
		*(p++) = '\0';

		if (strcmp(msg, "LIVETV_CHAIN UPDATE") != 0) {
			return;
		}
	} else {
		p = msg;
	}

	prog = cmyth_recorder_get_cur_proginfo(rec);

	if (prog == NULL) {
		return;
	}

	path = cmyth_proginfo_pathname(prog);

	if (path == NULL) {
		return;
	}

	if (strlen(path) == 0) {
		ref_release(path);
		ref_release(prog);
		return;
	}
	ref_release(path);

	pthread_mutex_lock(&chain->chain_mutex);

	if (!chain->chain_id || (strncmp(p, chain->chain_id, strlen(p)) != 0)) {
		goto out;
	}

	tip = chain->chain_count - 1;

	if (tip >= 0) {
		path = cmyth_proginfo_pathname(chain->chain_list[tip]->prog);
		ref_release(path);

		if (cmyth_proginfo_compare(prog,
					   chain->chain_list[tip]->prog) == 0) {
			ref_release(prog);
			goto out;
		}

		offset = chain->chain_list[tip]->offset +
			cmyth_proginfo_length(chain->chain_list[tip]->prog);
	} else {
		offset = 0;
		start = 1;
	}

	size = sizeof(*chain->chain_list) * (++chain->chain_count);

	chain->chain_list = ref_realloc(chain->chain_list, size);

	entry = ref_alloc(sizeof(*entry));

	entry->prog = prog;
	entry->file = NULL;
	entry->offset = offset;

	chain->chain_list[tip+1] = entry;

	pthread_cond_broadcast(&chain->chain_cond);

out:
	pthread_mutex_unlock(&chain->chain_mutex);

	if (start) {
		chain->chain_current = 0;
		cmyth_chain_switch(chain, 0);
	}
}
Exemplo n.º 30
0
int
cmyth_mysql_get_prog_finder_time(cmyth_database_t db, cmyth_program_t **prog,  time_t starttime, char *program_name) 
{
	MYSQL_RES *res=NULL;
	MYSQL_ROW row;
        char query[630];
	char *N_title;
	int rows=0;
	int n = 50;
	int ch;


	if(cmyth_db_check_connection(db) != 0)
	{
               cmyth_dbg(CMYTH_DBG_ERROR, "%s: cmyth_db_check_connection failed\n",
                           __FUNCTION__);
               fprintf(stderr,"%s: cmyth_db_check_connection failed\n", __FUNCTION__);
	       return -1;
	}

	N_title = ref_alloc(strlen(program_name)*2+1);
	mysql_real_escape_string(db->mysql,N_title,program_name,strlen(program_name)); 

        //sprintf(query, "SELECT chanid,starttime,endtime,title,description,subtitle,programid,seriesid,category FROM program WHERE starttime >= '%s' and title ='%s' ORDER BY `starttime` ASC ", starttime, N_title);
        snprintf(query, 630, "SELECT program.chanid,UNIX_TIMESTAMP(program.starttime),UNIX_TIMESTAMP(program.endtime),program.title,program.description,program.subtitle,program.programid,program.seriesid,program.category, channel.channum, channel.callsign, channel.name, channel.sourceid FROM program LEFT JOIN channel on program.chanid=channel.chanid WHERE starttime >= FROM_UNIXTIME(%d) and title ='%s' ORDER BY `starttime` ASC ", (int)starttime, N_title);
	ref_release(N_title);
	fprintf(stderr, "%s\n", query);
        cmyth_dbg(CMYTH_DBG_ERROR, "%s: query= %s\n", __FUNCTION__, query);
        if(mysql_query(db->mysql,query)) {
                 cmyth_dbg(CMYTH_DBG_ERROR, "%s: mysql_query() Failed: %s\n", 
                           __FUNCTION__, mysql_error(db->mysql));
		return -1;
       	}
	cmyth_dbg(CMYTH_DBG_ERROR, "n =  %d\n",n);
        res = mysql_store_result(db->mysql);
	cmyth_dbg(CMYTH_DBG_ERROR, "n =  %d\n",n);
	while((row = mysql_fetch_row(res))) {
			cmyth_dbg(CMYTH_DBG_ERROR, "n =  %d\n",n);
        	if (rows == n) {
                	n++;
			cmyth_dbg(CMYTH_DBG_ERROR, "realloc n =  %d\n",n);
                       	*prog=realloc(*prog,sizeof(**prog)*(n));
               	}
			cmyth_dbg(CMYTH_DBG_ERROR, "rows =  %d\nrow[0]=%d\n",rows, row[0]);
			cmyth_dbg(CMYTH_DBG_ERROR, "row[1]=%d\n",row[1]);
			ch = atoi(row[0]);
			(*prog)[rows].chanid=ch;
			cmyth_dbg(CMYTH_DBG_ERROR, "prog[%d].chanid =  %d\n",rows, (*prog)[rows].chanid);
			(*prog)[rows].recording=0;
			(*prog)[rows].starttime = atoi(row[1]);
			(*prog)[rows].endtime = atoi(row[2]);
			sizeof_strncpy ((*prog)[rows].title, row[3]);
			sizeof_strncpy ((*prog)[rows].description, row[4]);
			sizeof_strncpy ((*prog)[rows].subtitle, row[5]);
			sizeof_strncpy ((*prog)[rows].programid, row[6]);
			sizeof_strncpy ((*prog)[rows].seriesid, row[7]);
			sizeof_strncpy ((*prog)[rows].category, row[8]);
			(*prog)[rows].channum = atoi (row[9]);
			sizeof_strncpy ((*prog)[rows].callsign,row[10]);
			sizeof_strncpy ((*prog)[rows].name,row[11]);
			(*prog)[rows].sourceid = atoi (row[12]);
        		cmyth_dbg(CMYTH_DBG_ERROR, "prog[%d].chanid =  %d\n",rows, (*prog)[rows].chanid);
        		cmyth_dbg(CMYTH_DBG_ERROR, "prog[%d].title =  %s\n",rows, (*prog)[rows].title);
			rows++;
        }
        mysql_free_result(res);
        cmyth_dbg(CMYTH_DBG_ERROR, "%s: rows= %d\n", __FUNCTION__, rows);
	return rows;
}