Example #1
0
/**
 * oh_create_handler
 * @handler_config: Hash table containing the configuration for a handler
 * read from the configuration file.
 * @hid: pointer where hid of newly created handler will be stored.
 *
 * Returns: SA_OK on success. If handler failed to open, then @hid will
 * contain a valid handler id, but SA_ERR_HPI_INTERNAL_ERROR will be
 * returned.
 **/
SaErrorT oh_create_handler (GHashTable *handler_config, unsigned int *hid)
{
        struct oh_handler *handler = NULL;

        if (!handler_config || !hid) {
                err("ERROR creating handler. Invalid parameters.");
                return SA_ERR_HPI_INVALID_PARAMS;
        }

	*hid = 0;

        handler = new_handler(handler_config);
        if (!handler) return SA_ERR_HPI_ERROR;

        *hid = handler->id;
        g_static_rec_mutex_lock(&oh_handlers.lock);
        oh_handlers.list = g_slist_append(oh_handlers.list, handler);
        g_hash_table_insert(oh_handlers.table,
                            &(handler->id),
                            g_slist_last(oh_handlers.list));

        handler->hnd = handler->abi->open(handler->config,
                                          handler->id,
                                          &oh_process_q);
        if (!handler->hnd) {
                err("A handler #%d on the %s plugin could not be opened.",
                    handler->id, handler->plugin_name);
		g_static_rec_mutex_unlock(&oh_handlers.lock);
		return SA_ERR_HPI_INTERNAL_ERROR;
        }
        g_static_rec_mutex_unlock(&oh_handlers.lock);

        return SA_OK;
}
Example #2
0
	/**
	 * Adds an event handler.
	 * An event with a nonempty ID will not be added if an event with that
	 * ID already exists.
	 */
	void t_event_handlers::add_event_handler(const config & cfg, manager & man, bool is_menu_item)
	{
		const std::string name = cfg["name"];
		std::string id = cfg["id"];

		if(!id.empty()) {
			// Ignore this handler if there is already one with this ID.
			id_map_t::iterator find_it = id_map_.find(id);
			if ( find_it != id_map_.end()  &&  !find_it->second.expired() ) {
				DBG_EH << "ignoring event handler for name='" << name
				       << "' with id '" << id << "'\n";
				return;
			}
		}

		// Create a new handler.
		DBG_EH << "inserting event handler for name=" << name <<
			" with id=" << id << "\n";
		handler_ptr new_handler(new event_handler(cfg, is_menu_item, active_.size(), man));
		active_.push_back(new_handler);

		// File by name.
		if ( utils::might_contain_variables(name) )
			dynamic_.push_back(new_handler);
		else {
			std::vector<std::string> name_list = utils::split(name);
			BOOST_FOREACH( const std::string & single_name, name_list )
				by_name_[standardize_name(single_name)].push_back(new_handler);
		}
		// File by ID.
		if ( !id.empty() )
			id_map_[id] = new_handler;

		log_handlers();
	}
Example #3
0
/**
 * Register a handler for general events.
 */
void
RegisterEventHandler(int type, void (*event_callback)(void *, void *), void *data)
{
	handler *	h;

	h = new_handler(HANDLER_EVENT, NULL);
	h->event_type = type;
	h->event_handler = event_callback;
	h->data = data;
}
Example #4
0
/**
 * Register a handler for file descriptor events.
 */
void
RegisterFileHandler(int fd, int type, int (*file_handler)(int, void *), void *data)
{
	handler *	h;
	
	h = new_handler(HANDLER_FILE, data);
	h->file_type = type;
	h->fd = fd;
	h->error = 0;
	h->file_handler = file_handler;
}
Example #5
0
/**
 * oh_load_handler
 * @handler_config
 *
 * Returns: 0 on Failure, otherwise the handler id
 **/
unsigned int oh_load_handler (GHashTable *handler_config)
{
    struct oh_handler *handler;
    static int first_handler = 1;

    if (!handler_config) {
        dbg("ERROR loading handler. Invalid handler configuration passed.");
        return 0;
    }

    data_access_lock();
    if (oh_init_handler_table()) {
        data_access_unlock();
        dbg("ERROR. Could not initialize handler table.");
        return 0;
    }

    handler = new_handler(handler_config);

    if (handler == NULL) {
        data_access_unlock();
        return 0;
    }

    g_hash_table_insert(handler_table,
                        &(handler->id),
                        handler);
    handler_ids = g_slist_append(handler_ids, &(handler->id));

    handler->hnd = handler->abi->open(handler->config);
    if (!handler->hnd) {
        dbg("A plugin instance could not be opened.");
        g_hash_table_remove(handler_table, &(handler->id));
        handler_ids = g_slist_remove(handler_ids, &(handler->id));
        g_free(handler);
        data_access_unlock();
        return 0;
    }

    /* register atexit callback to close handlers
     * and handler connections to avoid zombies
     */
    if (first_handler) {
        (void) atexit(close_handlers_atexit);
        first_handler = 0;
    }


    data_access_unlock();

    return handler->id;
}
Example #6
0
KSG_Proactor_Handler* KSG_Proactor_Acceptor::make_handler()
{
	ACE_DEBUG((LM_DEBUG,"开始申请 ACE HANDLER ..."));
	ACE_GUARD_RETURN(ACE_Recursive_Thread_Mutex,ace_mon,*ACE_Static_Object_Lock::instance(),NULL);
	handler_type* ih;
	if(_clients.empty())
	{
		ih = new_handler();
		_total_cli++;
		ih->set_handler_id(static_cast<int>(_total_cli));
		ACE_DEBUG((LM_DEBUG,"创建第 [%d] 个句柄对象",_total_cli));
	}
	else
	{
		ih = _clients.front();
		_clients.pop_front();
	}
	_used_clts.insert(ih);
	ACE_DEBUG((LM_DEBUG,"申请 ACE HANDLER 成功!"));
	return ih;
}
Example #7
0
int load_handler (GHashTable *handler_config)
{
    struct oh_handler *handler;

    data_access_lock();

    handler = new_handler(handler_config);

    if(handler == NULL) {
        data_access_unlock();
        return -1;
    }

    global_handler_list = g_slist_append(
                              global_handler_list,
                              (gpointer) handler
                          );

    data_access_unlock();

    return 0;
}
Example #8
0
/**
 * oh_create_handler
 * @handler_config: Hash table containing the configuration for a handler
 * read from the configuration file.
 *
 * Returns: 0 on Failure, otherwise the handler id (id > 0) of
 * the newly created handler
 **/
unsigned int oh_create_handler (GHashTable *handler_config)
{
        struct oh_handler *handler;
        unsigned int new_hid = 0;

        if (!handler_config) {
                dbg("ERROR loading handler. Invalid handler configuration passed.");
                return 0;
        }

        handler = new_handler(handler_config);
        if (handler == NULL) {
                return 0;
        }

        new_hid = handler->id;
        g_static_rec_mutex_lock(&oh_handlers.lock);
        oh_handlers.list = g_slist_append(oh_handlers.list, handler);
        g_hash_table_insert(oh_handlers.table,
                            &(handler->id),
                            g_slist_last(oh_handlers.list));

        handler->hnd = handler->abi->open(handler->config);
        if (!handler->hnd) {
                g_hash_table_remove(oh_handlers.table, &handler->id);
                oh_handlers.list = g_slist_remove(oh_handlers.list, &(handler->id));
                g_static_rec_mutex_unlock(&oh_handlers.lock);
                dbg("A handler #%d on the %s plugin could not be opened.",
                    handler->id, handler->plugin_name);
                __delete_handler(handler);
                return 0;
        }
        g_static_rec_mutex_unlock(&oh_handlers.lock);

        return new_hid;
}
Example #9
0
bool JAMediaPlayer::on_bus_message(const Glib::RefPtr<Gst::Bus>& bus,
    const Glib::RefPtr<Gst::Message>& message){

    switch(message->get_message_type()){
        case Gst::MESSAGE_ELEMENT: {
            if (message->get_structure().has_name("prepare-xwindow-id")){
                Glib::RefPtr<Gst::Element> element =
                    Glib::RefPtr<Gst::Element>::cast_dynamic(message->get_source());
                Glib::RefPtr< Gst::ElementInterfaced<Gst::XOverlay> > xoverlay =
                    Gst::Interface::cast <Gst::XOverlay>(element);
                if (xoverlay){
                    xoverlay->set_xwindow_id(xid);}}
            break;}

        case Gst::MESSAGE_STATE_CHANGED:{
            Glib::RefPtr<Gst::MessageStateChanged> stateChangeMsg =
                Glib::RefPtr<Gst::MessageStateChanged>::cast_dynamic(message);
            if (stateChangeMsg){
                Gst::State oldState, newState, pendingState;
                stateChangeMsg->parse(oldState, newState, pendingState);
                if (estado != newState){
                    estado = newState;
                    if (estado == Gst::STATE_PLAYING){
                        signal_estado_update.emit("playing");
                        new_handler(true);}
                    else {signal_estado_update.emit("paused");
                        new_handler(false);}}}
            break;}

        case Gst::MESSAGE_EOS:{
            new_handler(false);
            signal_end.emit();
            return false;
            break;}

        case Gst::MESSAGE_ERROR:{
            Glib::RefPtr<Gst::MessageError> msgError =
                Glib::RefPtr<Gst::MessageError>::cast_static(message);
            if(msgError){
                Glib::Error err;
                err = msgError->parse();
                std::cerr << "Player Error: " << err.what() << std::endl;}
            else{
                std::cerr << "Player Error." << std::endl;}
            new_handler(false);
            return false;
            break;}

        case Gst::MESSAGE_TAG:{
            Glib::RefPtr<Gst::MessageTag> msg =
                Glib::RefPtr<Gst::MessageTag>::cast_static(message);
            Glib::ustring info = msg->get_structure().to_string();
            size_t found = info.find("video-codec");
            if (found!=std::string::npos){
                signal_video.emit();}
            signal_info_update.emit(info);
            break;}

        //elif message.type == gst.MESSAGE_BUFFERING:
        //    buf = int(message.structure["buffer-percent"])
        //    if buf < 100 and self.estado == gst.STATE_PLAYING:
        //        self.emit("loading-buffer", buf)
        //        self.__pause()
        //    elif buf > 99 and self.estado != gst.STATE_PLAYING:
        //        self.emit("loading-buffer", buf)
        //        self.play()

    }return true;}
Example #10
0
void JAMediaPlayer::stop(){
    new_handler(false);
    posicion = 0;
    playbin->set_state(Gst::STATE_NULL);
    signal_progress_update.emit(posicion);}
Example #11
0
int main(int n, char **v)
{
  extern int volatile gui_logd;
  char *database_filename = NULL;
  void *database;
  char *ip = DEFAULT_REMOTE_IP;
  int port = DEFAULT_REMOTE_PORT;
  char **on_off_name;
  int *on_off_action;
  int on_off_n = 0;
  int *is_on;
  int number_of_events;
  int i;
  event_handler *h;
  gui *g;
  vcd_data vcd_data;

  /* write on a socket fails if the other end is closed and we get SIGPIPE */
  if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) abort();

  on_off_name = malloc(n * sizeof(char *)); if (on_off_name == NULL) abort();
  on_off_action = malloc(n * sizeof(int)); if (on_off_action == NULL) abort();

  for (i = 1; i < n; i++) {
    if (!strcmp(v[i], "-h") || !strcmp(v[i], "--help")) usage();
    if (!strcmp(v[i], "-d"))
      { if (i > n-2) usage(); database_filename = v[++i]; continue; }
    if (!strcmp(v[i], "-ip")) { if (i > n-2) usage(); ip = v[++i]; continue; }
    if (!strcmp(v[i], "-p"))
      { if (i > n-2) usage(); port = atoi(v[++i]); continue; }
    if (!strcmp(v[i], "-on")) { if (i > n-2) usage();
      on_off_name[on_off_n]=v[++i]; on_off_action[on_off_n++]=1; continue; }
    if (!strcmp(v[i], "-off")) { if (i > n-2) usage();
      on_off_name[on_off_n]=v[++i]; on_off_action[on_off_n++]=0; continue; }
    if (!strcmp(v[i], "-ON"))
      { on_off_name[on_off_n]=NULL; on_off_action[on_off_n++]=1; continue; }
    if (!strcmp(v[i], "-OFF"))
      { on_off_name[on_off_n]=NULL; on_off_action[on_off_n++]=0; continue; }
    if (!strcmp(v[i], "-debug-gui")) { gui_logd = 1; continue; }
    usage();
  }

  if (database_filename == NULL) {
    printf("ERROR: provide a database file (-d)\n");
    exit(1);
  }

  database = parse_database(database_filename);

  load_config_file(database_filename);

  number_of_events = number_of_ids(database);
  is_on = calloc(number_of_events, sizeof(int));
  if (is_on == NULL) abort();

  h = new_handler(database);

  g = gui_init();
  new_thread(gui_thread, g);

  vcd_main_gui(g, h, database);

  on_off(database, "VCD_FUNCTION", is_on, 1);

  for (i = 0; i < on_off_n; i++)
    on_off(database, on_off_name[i], is_on, on_off_action[i]);

  vcd_data.socket = -1;
  vcd_data.is_on = is_on;
  vcd_data.nevents = number_of_events;
  if (pthread_mutex_init(&vcd_data.lock, NULL)) abort();
  setup_event_selector(g, database, is_on, is_on_changed, &vcd_data);

  vcd_data.socket = connect_to(ip, port);

  /* send the first message - activate selected traces */
  is_on_changed(&vcd_data);

  /* read messages */
  while (1) {
    char v[T_BUFFER_MAX];
    event e;
    e = get_event(vcd_data.socket, v, database);
    if (e.type == -1) abort();
    handle_event(h, e);
  }

  return 0;
}
Example #12
0
/** 
 * Collects user input and performs the main program loop.
 * @param argc - Number of arguments; should be 3.
 * @param argv - Command line arguments; 1 should be the server; 2 should be the
 * 				port.
 * @return int - Returns 0 if no errors; nonzero otherwise.
 */
int 
main(int argc, char *argv[]) 
{
	int server_exit;	// = 1 if the server should terminate.
	int ret;			// General purpose return value storage.
	struct sockaddr *sa = malloc(SA_SZ);	// Used to convert network addresses.
	
	pthread_t io_tid;	// Thread ID of the user input thread.
	
	unsigned next_hid = 1;			// Used to assign handler IDs.
	pthread_attr_t h_attr;			// Handler thread attributes.
	struct handler *new_h = NULL;	// Used to allocate memory for new handlers.
	init_handler_list(&g_list);		// List of active client handlers (global).
	
	struct p_conn_params server;	// Server connection parameters.
	char s_ipstr[INET6_ADDRSTRLEN];	// Server address.
	int s_port;						// Server port - int.
	char str_port[10];
	
	fd_set listenfds;			// 1-element set containing the listening socket.
	struct timeval timeout;	// Timeout before server checks if it should exit.
	
	
	// Set up the handler thread attribute to be in detached state - we do not 
	// need the main thread to join with them; as soon as their client has
	// disconnected they can have their resources reallocated.
	ret = pthread_attr_init(&h_attr);
	if (ret) {
		ret = p_perror(P_ETATTR, strerror(ret));
		clean_exit(ret, &h_attr, server.socket);
	}	
	
	ret = pthread_attr_setdetachstate(&h_attr, PTHREAD_CREATE_DETACHED);
	if (ret) {
		ret = p_perror(P_ETATTR, strerror(ret));
		clean_exit(ret, &h_attr, server.socket);
	}
	
	
	// Start up the IO thread.
	if (pthread_create(&io_tid, &h_attr, &io_task, NULL)) {
		return p_perror(P_EIOTASK, NULL);
	}
	sstatus("Server input started. Enter *quit to exit.\n");	
	
	// Print the list of interface addresses for this machine.
	p_print_if_addrs();
	
	// Get the port number the user wishes to listen on, if available.
	if (argc == 2) {
		strncpy(str_port, argv[1], 6);
		str_port[5] = '\0';
	}
	else {
		strncpy(str_port, DEFAULT_PORT, 6);
	}
		
	
	// Set up the server socket information
	p_init_conn_params(&server);
	p_init_addrinfo(&server.params, AI_PASSIVE, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP);
	
	ret = setup_server(&server, str_port);
	
	if (ret > 0) {
		clean_exit(ret, &h_attr, server.socket);
	}	
	
	else if (ret == -1) {
		sstatus("The attempted port number was in use: ");
		printf("%s", str_port);
				
		// Loop until we get a usable port..
		int p = atoi(str_port);
		if (p < 1024) p = 1024;
	
		while (ret == -1 && p < 65535) {
			p++;
			sprintf(str_port, "%d", p);
		
			freeaddrinfo(server.server_info);
			
			sstatus("Attempting to bind to port: ");
			printf("%s", str_port);
			
			ret = setup_server(&server, str_port);
			
			if (ret > 0) {
				clean_exit(ret, &h_attr, server.socket);
			}
		}
		
		if (p == 65535) {
			sstatus("There are no ports available to bind to.");
			clean_exit(ret, &h_attr, server.socket);
		}
	}

	freeaddrinfo(server.server_info);

	
	// Alert user to port.
	sstatus("Server set up and listening.");
	if (p_get_sock_name(server.socket, sa, SA_SZ) > 0) {
		printf(" Port = %i\n", p_port_from_sa(sa));
	}
	
		
	MXLOCK(&mx_exit_var);
	server_exit = g_exit_var;
	MXUNLOCK(&mx_exit_var);

	// Main server client-handling loop.
	while (server_exit == 0) {
		
		// Wait for incoming connections.
		FD_ZERO(&listenfds);
		FD_SET(server.socket, &listenfds);
		timeout.tv_sec = SERV_TO_SEC;
		timeout.tv_usec = 0;
		
		ret = select(server.socket + 1, &listenfds, NULL, NULL, &timeout);
		
		if (ret == -1) {
			ret = p_perror(P_ESELECT, strerror(errno));
			clean_exit(ret, &h_attr, server.socket);	
		}
		
		// There is an incoming connection.
		else if (FD_ISSET(server.socket, &listenfds)) {	
			sstatus("Incoming connection. ");
			
			new_h = new_handler();			
			new_h->sock = accept(server.socket, (struct sockaddr *)&new_h->addr,
								 &new_h->addr_sz);
					
			
			if (new_h->sock == -1) {	// accept() failed
				ret = p_perror(P_EACCEPT, strerror(errno));
				clean_exit(ret, &h_attr, server.socket);
			}
			
			else {				
				// Get some info on the new client and assign an ID.
				if (p_get_sock_name(new_h->sock, sa, SA_SZ) > 0) {
					new_h->port = p_port_from_sa(sa);
					p_ip_from_sa(sa, new_h->ipstr);
				}				
				new_h->hid = next_hid++;
				
				// Spawn a new handler.
				ret = pthread_create(&new_h->tid, &h_attr, &handler_task, 
									 (void *)new_h);
				if (ret == -1) {
					ret = p_perror(P_ETHREAD, strerror(ret));
					clean_exit(ret, &h_attr, server.socket);
				}
				
				sstatus("New connection established. ID = ");
				printf("%hi. ", new_h->hid);
				fflush(stdout);
				
				if (new_h->ipstr != NULL) printf("%s", new_h->ipstr);
				printf(" : %i\n", new_h->port);
				fflush(stdout);
				
				// Add the handler information to the global list.
				MXLOCK(&mx_list);
				hl_add_to_head(&g_list, new_h);
				MXUNLOCK(&mx_list);
			}			
		}
		
		
		MXLOCK(&mx_exit_var);
		server_exit = g_exit_var;
		MXUNLOCK(&mx_exit_var);
	}
	
	
	// Clean up threads, data structures, etc., and exit.
	sstatus("Server shutting down.. ");
	clean_exit(0, &h_attr, server.socket);		
}