Example #1
0
void
toolbar_client_show(Client *c)
{
  Wm   *w = c->wm;
  long win_state; 

  c->mapped = True;

  if (w->stack_top_app 
      && (w->stack_top_app->flags & CLIENT_FULLSCREEN_FLAG))
    main_client_manage_toolbars_for_fullscreen(c, True);

   win_state = client_get_state(c);

   if (win_state == WithdrawnState)    /* initial show() state */
     {
       client_set_state(c,NormalState);
       wm_update_layout(c->wm, c, - c->height);
     } 
   else if (win_state == IconicState) /* minimised, set maximised */
     {
       client_set_state(c,NormalState);

       /* Make sure desktop flag is unset */
       c->flags &= ~CLIENT_IS_MINIMIZED;

       wm_update_layout(c->wm, c, -(c->height - toolbar_win_offset(c)));

       if (c->flags & CLIENT_TITLE_HIDDEN_FLAG)
	 {
	   c->x = wm_get_offsets_size(c->wm, WEST,  NULL, False);
	 }
       else
	 {
	   c->x = theme_frame_defined_width_get(w->mbtheme,
						FRAME_UTILITY_MAX )
	     + wm_get_offsets_size(c->wm, WEST,  NULL, False);
	   c->y = c->y - ( c->height - toolbar_win_offset(c));
	 }

       /* destroy buttons so they get recreated ok */   
       client_buttons_delete_all(c);   

       toolbar_client_move_resize(c);

       toolbar_client_redraw(c, False);
     } 

   stack_move_client_above_type(c, MBCLIENT_TYPE_APP|MBCLIENT_TYPE_DESKTOP);

   XMapSubwindows(w->dpy, c->frame);
   XMapWindow(w->dpy, c->frame);

   comp_engine_client_show(w, c);
}
Example #2
0
Client*
toolbar_client_new(Wm *w, Window win)
{
   Client *c = base_client_new(w, win); 

   if (!c) return NULL;

   /* If theres not room for the toolbar, just make it an app window */
   /* In practise one would expect this to never happen              */
   if ((c->height) > w->dpy_height - (theme_frame_defined_height_get(w->mbtheme, FRAME_MAIN)*2) )
     {
       dbg("%s() not enough room for toolbar client\n", __func__);
       base_client_destroy(c);
       return main_client_new(w, win);
     }

   c->type = MBCLIENT_TYPE_TOOLBAR;
   
   c->configure    = &toolbar_client_configure;
   c->reparent     = &toolbar_client_reparent;
   c->button_press = &toolbar_client_button_press;
   c->redraw       = &toolbar_client_redraw;
   c->hide         = &toolbar_client_hide;
   c->iconize      = &toolbar_client_hide;
   c->show         = &toolbar_client_show;
   c->move_resize  = &toolbar_client_move_resize;
   c->get_coverage = &toolbar_client_get_coverage;
   c->destroy      = &toolbar_client_destroy;

   client_set_state(c,WithdrawnState); 	/* set initially to signal show() */
   
   return c;
}
Example #3
0
static void connection_handler(IOCHAN iochan, int event)
{
    struct connection *co = iochan_getdata(iochan);
    struct client *cl;
    struct host *host = co->host;

    yaz_mutex_enter(host->mutex);
    cl = co->client;
    if (!cl) 
    {
        /* no client associated with it.. We are probably getting
           a closed connection from the target.. Or, perhaps, an unexpected
           package.. We will just close the connection */
        yaz_log(YLOG_LOG, "timeout connection %p event=%d", co, event);
        remove_connection_from_host(co);
        yaz_mutex_leave(host->mutex);
        connection_destroy(co);
    }
    else if (event & EVENT_TIMEOUT)
    {
        if (co->state == Conn_Connecting)
        {
            yaz_log(YLOG_WARN, "%p connect timeout %s", co, client_get_id(cl));

            client_set_state(cl, Client_Error);
            remove_connection_from_host(co);
            yaz_mutex_leave(host->mutex);
            connection_destroy(co);
        }
        else
        {
            yaz_log(YLOG_LOG,  "%p Connection idle timeout %s", co, client_get_id(cl));
            remove_connection_from_host(co);
            yaz_mutex_leave(host->mutex);
            connection_destroy(co);
        }
    }
    else
    {
        yaz_mutex_leave(host->mutex);

        client_lock(cl);
        non_block_events(co);

        ZOOM_connection_fire_event_socket(co->link, event);
        
        non_block_events(co);
        client_unlock(cl);

        if (co->link)
        {
            iochan_setflags(iochan, ZOOM_connection_get_mask(co->link));
            iochan_setfd(iochan, ZOOM_connection_get_socket(co->link));
        }
    }
}
Example #4
0
File: dawm.c Project: dstenb/dawm
void
handler_unmapnotify(XEvent *ev)
{
	struct client *c;
	XUnmapEvent *uev = &ev->xunmap;

	dbg_print(__func__);

	if ((c = find_client_by_window(mons, uev->window))) {
		if(uev->send_event)
			client_set_state(c, WithdrawnState);
		else
			remove_client(c, false);
	}
}
Example #5
0
void
dialog_client_show(Client *c)
{
  Wm *w = c->wm;
  Client *highest_client = NULL;
  MBList *transient_list = NULL, *list_item = NULL;

  dbg("%s() called for %s\n", __func__, c->name);

  if (!c->mapped)
    {
      if (c->flags & CLIENT_IS_MINIMIZED)
	{
	  Client *p = NULL;

	  client_set_state(c, NormalState);
	  c->flags &= ~CLIENT_IS_MINIMIZED;

	  /* Make sure any transients are un minimized too */
	  stack_enumerate(w, p)
	    if (p->trans == c)
	      {
		p->show(p);
		XMapSubwindows(w->dpy, p->frame);
		XMapWindow(w->dpy, p->frame);
	      }

	  if (c->flags & CLIENT_IS_MODAL_FLAG)
	    c->wm->n_modals_present++;
	}

      if (c->win_modal_blocker)
	{
	  XMapWindow(w->dpy, c->win_modal_blocker);
	  dbg("%s() blocker win mapped for '%s'\n", __func__, c->name);
	}

      /* Set flag below to delay actual mapping of window 
       * till *after* we've synced the stack. This hacky  
       * solution avoids problem of dialogs mapping for
       * clients below and causing flicker.
       *
       * Note could still cause issues with transients and remmapping
       * them if minimised.. 
      */
      c->flags |= CLIENT_DELAY_MAPPING;
    }
Example #6
0
File: client.c Project: EBone/Faust
static int get_server_state(AvahiClient *client, int *ret_error) {
    DBusMessage *message = NULL, *reply = NULL;
    DBusError error;
    int32_t state;
    int e = AVAHI_ERR_NO_MEMORY;

    assert(client);

    dbus_error_init(&error);

    if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetState")))
        goto fail;

    reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);

    if (!reply || dbus_error_is_set (&error))
        goto fail;

    if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID) ||
        dbus_error_is_set (&error))
        goto fail;

    client_set_state(client, (AvahiClientState) state);

    dbus_message_unref(message);
    dbus_message_unref(reply);

    return AVAHI_OK;

fail:
    if (dbus_error_is_set(&error)) {
        e = avahi_error_dbus_to_number (error.name);
        dbus_error_free(&error);
    }

    if (ret_error)
        *ret_error = e;

    if (message)
        dbus_message_unref(message);
    if (reply)
        dbus_message_unref(reply);

    return e;
}
Example #7
0
void client_unmanage(Client *c, bool destroyed) {
	Monitor *m = c->mon;

	/* The server grab construct avoids race conditions. */
	client_detach(c);
	client_detach_stack(c);
	printf("unmanage %i, %i\n", destroyed, c->win);
	if(!destroyed) {
		uint32_t values[] = { c->oldbw };
		xcb_grab_server(conn);
		xcb_configure_window_checked(conn, c->win, 
			XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
		xcb_ungrab_button_checked(conn, XCB_BUTTON_INDEX_ANY, c->win, 
			XCB_GRAB_ANY);
		client_set_state(c, XCB_ICCCM_WM_STATE_WITHDRAWN);
		xcb_flush(conn);
		xcb_ungrab_server(conn);
	}
	free(c);
	client_focus(NULL);
	arrange(m);
}
Example #8
0
void
toolbar_client_hide(Client *c)
{
  Wm   *w = c->wm;

  if (c->flags & CLIENT_IS_MINIMIZED || client_get_state(c) == IconicState) 
    return;
   
  client_set_state(c,IconicState);
  c->flags |= CLIENT_IS_MINIMIZED; 

  c->ignore_unmap++;
  XUnmapWindow(w->dpy, c->window);
  
  if (c->flags & CLIENT_TITLE_HIDDEN_FLAG)
    {
      XUnmapWindow(w->dpy, c->frame);
    }
  else
    {
      client_buttons_delete_all(c);   
  
      c->x = wm_get_offsets_size(c->wm, WEST,  NULL, False);
  
      c->y = c->y + c->height 
               - theme_frame_defined_height_get(c->wm->mbtheme, FRAME_UTILITY_MIN);
    }

  toolbar_client_move_resize(c);

  toolbar_client_redraw(c, False);
  
  dbg("hiding toolbar y is now %i", c->y);

  wm_update_layout(c->wm, c, c->height - toolbar_win_offset(c));
}
Example #9
0
File: client.c Project: EBone/Faust
AvahiClient *avahi_client_new(const AvahiPoll *poll_api, AvahiClientFlags flags, AvahiClientCallback callback, void *userdata, int *ret_error) {
    AvahiClient *client = NULL;
    DBusError error;
    DBusMessage *message = NULL, *reply = NULL;

    avahi_init_i18n();

    dbus_error_init(&error);

    if (!(client = avahi_new(AvahiClient, 1))) {
        if (ret_error)
            *ret_error = AVAHI_ERR_NO_MEMORY;
        goto fail;
    }

    client->poll_api = poll_api;
    client->error = AVAHI_OK;
    client->callback = callback;
    client->userdata = userdata;
    client->state = (AvahiClientState) -1;
    client->flags = flags;

    client->host_name = NULL;
    client->host_name_fqdn = NULL;
    client->domain_name = NULL;
    client->version_string = NULL;
    client->local_service_cookie_valid = 0;

    AVAHI_LLIST_HEAD_INIT(AvahiEntryGroup, client->groups);
    AVAHI_LLIST_HEAD_INIT(AvahiDomainBrowser, client->domain_browsers);
    AVAHI_LLIST_HEAD_INIT(AvahiServiceBrowser, client->service_browsers);
    AVAHI_LLIST_HEAD_INIT(AvahiServiceTypeBrowser, client->service_type_browsers);
    AVAHI_LLIST_HEAD_INIT(AvahiServiceResolver, client->service_resolvers);
    AVAHI_LLIST_HEAD_INIT(AvahiHostNameResolver, client->host_name_resolvers);
    AVAHI_LLIST_HEAD_INIT(AvahiAddressResolver, client->address_resolvers);
    AVAHI_LLIST_HEAD_INIT(AvahiRecordBrowser, client->record_browsers);

    if (!(client->bus = avahi_dbus_bus_get(&error)) || dbus_error_is_set(&error)) {
        if (ret_error)
            *ret_error = AVAHI_ERR_DBUS_ERROR;
        goto fail;
    }

    if (avahi_dbus_connection_glue(client->bus, poll_api) < 0) {
        if (ret_error)
            *ret_error = AVAHI_ERR_NO_MEMORY; /* Not optimal */
        goto fail;
    }

    if (!dbus_connection_add_filter(client->bus, filter_func, client, NULL)) {
        if (ret_error)
            *ret_error = AVAHI_ERR_NO_MEMORY;
        goto fail;
    }

    dbus_bus_add_match(
        client->bus,
        "type='signal', "
        "interface='" AVAHI_DBUS_INTERFACE_SERVER "', "
        "sender='" AVAHI_DBUS_NAME "', "
        "path='" AVAHI_DBUS_PATH_SERVER "'",
        &error);

    if (dbus_error_is_set(&error))
        goto fail;

    dbus_bus_add_match (
        client->bus,
        "type='signal', "
        "interface='" DBUS_INTERFACE_DBUS "', "
        "sender='" DBUS_SERVICE_DBUS "', "
        "path='" DBUS_PATH_DBUS "'",
        &error);

    if (dbus_error_is_set(&error))
        goto fail;

    dbus_bus_add_match(
        client->bus,
        "type='signal', "
        "interface='" DBUS_INTERFACE_LOCAL "'",
        &error);

    if (dbus_error_is_set(&error))
        goto fail;

    if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, "org.freedesktop.DBus.Peer", "Ping")))
        goto fail;

    reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);

    if (!reply || dbus_error_is_set (&error)) {
        /* We free the error so its not set, that way the fail target
         * will return the NO_DAEMON error rather than a DBUS error */
        dbus_error_free(&error);

        if (!(flags & AVAHI_CLIENT_NO_FAIL)) {

            if (ret_error)
                *ret_error = AVAHI_ERR_NO_DAEMON;

            goto fail;
        }

        /* The user doesn't want this call to fail if the daemon is not
         * available, so let's return succesfully */
        client_set_state(client, AVAHI_CLIENT_CONNECTING);

    } else {

        if (init_server(client, ret_error) < 0)
            goto fail;
    }

    dbus_message_unref(message);

    if (reply)
        dbus_message_unref(reply);

    return client;

fail:

    if (message)
        dbus_message_unref(message);
    if (reply)
        dbus_message_unref(reply);

    if (client)
        avahi_client_free(client);

    if (dbus_error_is_set(&error)) {

        if (ret_error) {
            if (strcmp(error.name, DBUS_ERROR_FILE_NOT_FOUND) == 0)
                /* DBUS returns this error when the DBUS daemon is not running */
                *ret_error = AVAHI_ERR_NO_DAEMON;
            else
                *ret_error = avahi_error_dbus_to_number(error.name);
        }

        dbus_error_free(&error);
    }

    return NULL;
}
Example #10
0
File: client.c Project: EBone/Faust
static DBusHandlerResult filter_func(DBusConnection *bus, DBusMessage *message, void *userdata) {
    AvahiClient *client = (AvahiClient *)userdata;
    DBusError error;

    assert(bus);
    assert(message);

    dbus_error_init(&error);

/*     fprintf(stderr, "dbus: interface=%s, path=%s, member=%s\n", */
/*             dbus_message_get_interface (message), */
/*             dbus_message_get_path (message), */
/*             dbus_message_get_member (message)); */

    if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {

        /* The DBUS server died or kicked us */
        avahi_client_set_errno(client, AVAHI_ERR_DISCONNECTED);
        goto fail;

    } else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameAcquired")) {

        /* Ignore this message */

    } else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
        char *name, *old, *newname;

        if (!dbus_message_get_args(
                  message, &error,
                  DBUS_TYPE_STRING, &name,
                  DBUS_TYPE_STRING, &old,
                  DBUS_TYPE_STRING, &newname,
                  DBUS_TYPE_INVALID) || dbus_error_is_set(&error)) {

            fprintf(stderr, "WARNING: Failed to parse NameOwnerChanged signal: %s\n", error.message);
            avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
            goto fail;
        }

        if (strcmp(name, AVAHI_DBUS_NAME) == 0) {

            if (old[0] &&
                avahi_client_is_connected(client)) {

                /* Regardless if the server lost its name or
                 * if the name was transfered: our services are no longer
                 * available, so we disconnect ourselves */
                avahi_client_set_errno(client, AVAHI_ERR_DISCONNECTED);
                goto fail;

            } else if (client->state == AVAHI_CLIENT_CONNECTING && (!old || *old == 0)) {
                int ret;

                /* Server appeared */

                if ((ret = init_server(client, NULL)) < 0) {
                    avahi_client_set_errno(client, ret);
                    goto fail;
                }
            }
        }

    } else if (!avahi_client_is_connected(client)) {

        /* Ignore messages we get in unconnected state */

    } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVER, "StateChanged")) {
        int32_t state;
        char *e = NULL;
        int c;

        if (!dbus_message_get_args(
                  message, &error,
                  DBUS_TYPE_INT32, &state,
                  DBUS_TYPE_STRING, &e,
                  DBUS_TYPE_INVALID) || dbus_error_is_set (&error)) {

            fprintf(stderr, "WARNING: Failed to parse Server.StateChanged signal: %s\n", error.message);
            avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
            goto fail;
        }

        if ((c = avahi_error_dbus_to_number(e)) != AVAHI_OK)
            avahi_client_set_errno(client, c);

        client_set_state(client, (AvahiClientState) state);

    } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged")) {
        const char *path;
        AvahiEntryGroup *g;
        path = dbus_message_get_path(message);

        for (g = client->groups; g; g = g->groups_next){
            if (strcmp(g->path, path) == 0)
                break;
		}

        if (g) {
            int32_t state;
            char *e;
            int c;

            if (!dbus_message_get_args(
                      message, &error,
                      DBUS_TYPE_INT32, &state,
                      DBUS_TYPE_STRING, &e,
                      DBUS_TYPE_INVALID) ||
                dbus_error_is_set(&error)) {

                fprintf(stderr, "WARNING: Failed to parse EntryGroup.StateChanged signal: %s\n", error.message);
                avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
                goto fail;
            }

            if ((c = avahi_error_dbus_to_number(e)) != AVAHI_OK)
                avahi_client_set_errno(client, c);

            avahi_entry_group_set_state(g, (AvahiEntryGroupState)state);
        }

        return avahi_domain_browser_event(client, AVAHI_BROWSER_NEW, message);
	}
    else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "ItemRemove"))
        return avahi_domain_browser_event(client, AVAHI_BROWSER_REMOVE, message);
    else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "CacheExhausted"))
        return avahi_domain_browser_event(client, AVAHI_BROWSER_CACHE_EXHAUSTED, message);
    else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "AllForNow"))
        return avahi_domain_browser_event(client, AVAHI_BROWSER_ALL_FOR_NOW, message);
    else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "Failure"))
        return avahi_domain_browser_event(client, AVAHI_BROWSER_FAILURE, message);

    else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemNew"))
        return avahi_service_type_browser_event (client, AVAHI_BROWSER_NEW, message);
    else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemRemove"))
        return avahi_service_type_browser_event (client, AVAHI_BROWSER_REMOVE, message);
    else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "CacheExhausted"))
        return avahi_service_type_browser_event (client, AVAHI_BROWSER_CACHE_EXHAUSTED, message);
    else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "AllForNow"))
        return avahi_service_type_browser_event (client, AVAHI_BROWSER_ALL_FOR_NOW, message);
    else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "Failure"))
        return avahi_service_type_browser_event (client, AVAHI_BROWSER_FAILURE, message);

    else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemNew"))
        return avahi_service_browser_event (client, AVAHI_BROWSER_NEW, message);
    else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemRemove"))
        return avahi_service_browser_event (client, AVAHI_BROWSER_REMOVE, message);
    else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "CacheExhausted"))
        return avahi_service_browser_event (client, AVAHI_BROWSER_CACHE_EXHAUSTED, message);
    else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "AllForNow"))
        return avahi_service_browser_event (client, AVAHI_BROWSER_ALL_FOR_NOW, message);
    else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "Failure"))
        return avahi_service_browser_event (client, AVAHI_BROWSER_FAILURE, message);

    else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_RESOLVER, "Found"))
        return avahi_service_resolver_event (client, AVAHI_RESOLVER_FOUND, message);
    else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_RESOLVER, "Failure"))
        return avahi_service_resolver_event (client, AVAHI_RESOLVER_FAILURE, message);

    else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_HOST_NAME_RESOLVER, "Found"))
        return avahi_host_name_resolver_event (client, AVAHI_RESOLVER_FOUND, message);
    else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_HOST_NAME_RESOLVER, "Failure"))
        return avahi_host_name_resolver_event (client, AVAHI_RESOLVER_FAILURE, message);

    else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER, "Found"))
        return avahi_address_resolver_event (client, AVAHI_RESOLVER_FOUND, message);
    else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER, "Failure"))
        return avahi_address_resolver_event (client, AVAHI_RESOLVER_FAILURE, message);

    else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_RECORD_BROWSER, "ItemNew"))
        return avahi_record_browser_event (client, AVAHI_BROWSER_NEW, message);
    else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_RECORD_BROWSER, "ItemRemove"))
        return avahi_record_browser_event (client, AVAHI_BROWSER_REMOVE, message);
    else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_RECORD_BROWSER, "CacheExhausted"))
        return avahi_record_browser_event (client, AVAHI_BROWSER_CACHE_EXHAUSTED, message);
    else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_RECORD_BROWSER, "AllForNow"))
        return avahi_record_browser_event (client, AVAHI_BROWSER_ALL_FOR_NOW, message);
    else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_RECORD_BROWSER, "Failure"))
        return avahi_record_browser_event (client, AVAHI_BROWSER_FAILURE, message);

    else {

        fprintf(stderr, "WARNING: Unhandled message: interface=%s, path=%s, member=%s\n",
               dbus_message_get_interface(message),
               dbus_message_get_path(message),
               dbus_message_get_member(message));

        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
    }

    return DBUS_HANDLER_RESULT_HANDLED;

fail:

    if (dbus_error_is_set(&error)) {
        avahi_client_set_errno(client, avahi_error_dbus_to_number(error.name));
        dbus_error_free(&error);
    }

    client_set_state(client, AVAHI_CLIENT_FAILURE);

    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
Example #11
0
// Ensure that client has a connection associated
int client_prep_connection(struct client *cl,
                           int operation_timeout, int session_timeout,
                           iochan_man_t iochan_man,
                           const struct timeval *abstime)
{
    struct connection *co;
    struct session_database *sdb = client_get_database(cl);
    const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
    const char *url = session_setting_oneval(sdb, PZ_URL);
    const char *sru = session_setting_oneval(sdb, PZ_SRU);
    struct host *host = 0;
    int default_port = *sru ? 80 : 210;

    if (zproxy && zproxy[0] == '\0')
        zproxy = 0;

    if (!url || !*url)
        url = sdb->database->id;

    host = find_host(client_get_session(cl)->service->server->database_hosts,
                     url, zproxy, default_port, iochan_man);

    yaz_log(YLOG_DEBUG, "client_prep_connection: target=%s url=%s",
            client_get_id(cl), url);
    if (!host)
        return 0;

    co = client_get_connection(cl);

    if (co)
    {
        assert(co->host);
        if (co->host == host && client_get_state(cl) == Client_Idle)
        {
            return 2;
        }
        client_incref(cl);
        connection_release(co);
        co = 0;
    }
    if (!co)
    {
        int max_connections = 0;
        int reuse_connections = 1;
        const char *v = session_setting_oneval(client_get_database(cl),
                                               PZ_MAX_CONNECTIONS);
        if (v && *v)
            max_connections = atoi(v);
        
        v = session_setting_oneval(client_get_database(cl),
                PZ_REUSE_CONNECTIONS);
        if (v && *v)
            reuse_connections = atoi(v);

        // See if someone else has an idle connection
        // We should look at timestamps here to select the longest-idle connection
        yaz_mutex_enter(host->mutex);
        while (1)
        {
            int num_connections = 0;
            for (co = host->connections; co; co = co->next)
                num_connections++;
            if (reuse_connections)
            {
                for (co = host->connections; co; co = co->next)
                {
                    if (connection_is_idle(co) &&
                        (!co->client || client_get_state(co->client) == Client_Idle) &&
                        !strcmp(ZOOM_connection_option_get(co->link, "user"),
                                session_setting_oneval(client_get_database(cl),
                                                       PZ_AUTHENTICATION)))
                    {
                        if (zproxy == 0 && co->zproxy == 0)
                            break;
                        if (zproxy && co->zproxy && !strcmp(zproxy, co->zproxy))
                            break;
                    }
                }
                if (co)
                {
                    yaz_log(YLOG_LOG, "num_connections = %d (reusing)", num_connections);
                    break;
                }
            }
            if (max_connections <= 0 || num_connections < max_connections)
            {
                yaz_log(YLOG_LOG, "num_connections = %d (new); max = %d",
                        num_connections, max_connections);
                break;
            }
            yaz_log(YLOG_LOG, "num_connections = %d (waiting) max = %d",
                    num_connections, max_connections);
            if (yaz_cond_wait(host->cond_ready, host->mutex, abstime))
            {
                yaz_log(YLOG_LOG, "out of connections %s", client_get_id(cl));
                client_set_state(cl, Client_Error);
                yaz_mutex_leave(host->mutex);
                return 0;
            }
        }
        if (co)
        {
            yaz_log(YLOG_LOG,  "%p Connection reuse. state: %d", co, co->state);
            connection_release(co);
            client_set_connection(cl, co);
            co->client = cl;
            /* ensure that connection is only assigned to this client
               by marking the client non Idle */
            client_set_state(cl, Client_Working);
            yaz_mutex_leave(host->mutex);
            co->operation_timeout = operation_timeout;
            co->session_timeout = session_timeout;
            /* tells ZOOM to reconnect if necessary. Disabled becuase
               the ZOOM_connection_connect flushes the task queue */
            ZOOM_connection_connect(co->link, 0, 0);
        }
        else
        {
            yaz_mutex_leave(host->mutex);
            co = connection_create(cl, host, operation_timeout, session_timeout,
                                   iochan_man);
        }
        assert(co->host);
    }

    if (co && co->link)
        return 1;
    else
        return 0;
}
Example #12
0
static int connection_connect(struct connection *con, iochan_man_t iochan_man)
{
    struct host *host = connection_get_host(con);
    ZOOM_options zoptions = ZOOM_options_create();
    const char *auth;
    const char *charset;
    const char *sru;
    const char *sru_version = 0;

    struct session_database *sdb = client_get_database(con->client);
    const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);

    assert(con);

    ZOOM_options_set(zoptions, "async", "1");
    ZOOM_options_set(zoptions, "implementationName", PACKAGE_NAME);
    ZOOM_options_set(zoptions, "implementationVersion", VERSION);
	
    if ((charset = session_setting_oneval(sdb, PZ_NEGOTIATION_CHARSET)))
        ZOOM_options_set(zoptions, "charset", charset);
    
    assert(host->ipport);
    if (host->proxy)
    {
        yaz_log(YLOG_LOG, "proxy=%s", host->ipport);
        ZOOM_options_set(zoptions, "proxy", host->ipport);
    }
    else
    {
        assert(host->tproxy);
        yaz_log(YLOG_LOG, "tproxy=%s", host->ipport);
        ZOOM_options_set(zoptions, "tproxy", host->ipport);
    }   

    if (apdulog && *apdulog)
        ZOOM_options_set(zoptions, "apdulog", apdulog);

    if ((auth = session_setting_oneval(sdb, PZ_AUTHENTICATION)))
        ZOOM_options_set(zoptions, "user", auth);
    if ((sru = session_setting_oneval(sdb, PZ_SRU)) && *sru)
        ZOOM_options_set(zoptions, "sru", sru);
    if ((sru_version = session_setting_oneval(sdb, PZ_SRU_VERSION)) 
        && *sru_version)
        ZOOM_options_set(zoptions, "sru_version", sru_version);
    if (!(con->link = ZOOM_connection_create(zoptions)))
    {
        yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
        ZOOM_options_destroy(zoptions);
        return -1;
    }

    if (sru && *sru)
    {
        char http_hostport[512];
        strcpy(http_hostport, "http://");
        strcat(http_hostport, host->url);
        yaz_log(YLOG_LOG, "SRU connect to : %s", http_hostport);
        ZOOM_connection_connect(con->link, http_hostport, 0);
    }
    else
    {
        ZOOM_connection_connect(con->link, host->url, 0);
    }
    con->iochan = iochan_create(-1, connection_handler, 0, "connection_socket");
    con->state = Conn_Connecting;
    iochan_settimeout(con->iochan, con->operation_timeout);
    iochan_setdata(con->iochan, con);
    iochan_add(iochan_man, con->iochan);

    client_set_state(con->client, Client_Connecting);
    ZOOM_options_destroy(zoptions);
    return 0;
}
Example #13
0
static void non_block_events(struct connection *co)
{
    int got_records = 0;
    IOCHAN iochan = co->iochan;
    ZOOM_connection link = co->link;
    while (1)
    {
        struct client *cl = co->client;
        int ev;
        int r = ZOOM_event_nonblock(1, &link);
        if (!r)
            break;
        if (!cl)
            continue;
        ev = ZOOM_connection_last_event(link);
        
#if 1
        yaz_log(YLOG_DEBUG, "%p Connection ZOOM_EVENT_%s", co, ZOOM_get_event_str(ev));
#endif
        switch (ev) 
        {
        case ZOOM_EVENT_END:
            {
                const char *error, *addinfo;
                int err;
                if ((err = ZOOM_connection_error(link, &error, &addinfo)))
                {
                    yaz_log(YLOG_LOG, "Error %s from %s",
                            error, client_get_id(cl));
                    client_set_diagnostic(cl, err, addinfo);
                    client_set_state(cl, Client_Error);
                }
                else
                {
                    iochan_settimeout(iochan, co->session_timeout);
                    client_set_state(cl, Client_Idle);
                }
                yaz_cond_broadcast(co->host->cond_ready);
            }
            break;
        case ZOOM_EVENT_SEND_DATA:
            break;
        case ZOOM_EVENT_RECV_DATA:
            break;
        case ZOOM_EVENT_UNKNOWN:
            break;
        case ZOOM_EVENT_SEND_APDU:
            client_set_state(co->client, Client_Working);
            iochan_settimeout(iochan, co->operation_timeout);
            break;
        case ZOOM_EVENT_RECV_APDU:
            break;
        case ZOOM_EVENT_CONNECT:
            yaz_log(YLOG_LOG, "Connected to %s", client_get_id(cl));
            co->state = Conn_Open;
            break;
        case ZOOM_EVENT_RECV_SEARCH:
            client_search_response(cl);
            break;
        case ZOOM_EVENT_RECV_RECORD:
            client_record_response(cl);
            got_records = 1;
            break;
        default:
            yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
                    ev, client_get_id(cl));
            break;
        }
    }
    if (got_records)
    {
        struct client *cl = co->client;
        if (cl)
        {
            client_check_preferred_watch(cl);
            client_got_records(cl);
        }
    }
}