Sun Microsystems, Inc.
spacerspacer
spacer www.sun.com docs.sun.com |
spacer
black dot
 
 
3.  XFN Programming Examples Namespace Browser Example  Previous   Contents   Next 
   
 

Compiling and Executing Browser Example

To compile Example 3-1, type:

% cc -o fnbrowse fnbrowse.c -lxfn

To browse the namespace starting from the initial context, the program is invoked as:

% fnbrowse

Or to browse a composite name and its descendents, type:

% fnbrowse composite_name

Commands

The commands supported by the fnbrowse program are summarized in Table 3-1.

Table 3-1 Namespace Browser Commands

Command

Usage

down child

Sets the browser at the subcontext of the child

up

Sets the browser at one level higher than the current context

list

Lists the names bound within the current context

show

Prints the reference of the current context

show child

Prints the reference of the current context's child

quit

Exits the browser

Sample Output

Sample output for navigating the entire namespace is displayed here.

Note the following:

  • The first list command shows the initial context bindings.

  • The fnbrowse program lists all names it finds in the namespace, including names with underscores. These names are explained in "Initial Context Bindings".

  • The three dots (...) represent the global namespace.

% fnbrowse
> list
_myorgunit  ...  _myself  thishost  myself  _orgunit  _host 
_thisens  myens  thisens  org  orgunit  thisuser  _thishost 
myorgunit  _user  thisorgunit  host  _thisorgunit  _myens  user

Navigating the namespace is accomplished with the up and down commands. In the following output, the down command brings the focus of the browser to the enterprise root of the namespace, thisens (can also be myens). The show command displays information about the reference and address type for thisens.

> down thisens
> show
Reference type: onc_fn_enterprise
Address type: on_fn_nisplus
  length: 20
  context type: enterprise root
  representation: normal
  version: 0
  internal name: eng.wiz.com
> up
> down thisorgunit

Continuing with the example, this list command shows the contexts for thisorgunit.

> list 
service  _fs  _host  _service  _site  site  _user  host  fs  user 
 
> down usr
Lookup failed: Name Not Found: 'usr'
 
> down service
> list
printer
 
> down printer 

The list command shows the printer names that are bound in the printer context. The show command displays the reference for the child, colorful.

> list 
celeste  _default  color  colorful  quartz  nuttree  puffin 
 
> show colorful
printer   
Reference type: onc_printers
Address type: onc_printers_bsdaddr
  length: 12
  data: 0x00 0x00 0x00 0x08 0x62 0x6c 0x61 0x63 0x6b 0x63 
....blackc 0x61 0x74 at
> down colorful
Could not construct context handle: No Supported Address
> quit
%

Printer Programming Example

Printer client and server software can take advantage of FNS to advertise and to browse the printers available with respect to organizations, sites, users, and hosts. The APIs used by the server and the client are XFN APIs, thereby ensuring that the application is portable across the different naming services used for storing printer bindings.

The programming example in this section shows how printer clients and servers obtain and store printer bindings. Users can then make use of the FNS commands, fnlist and fnlookup, to browse the printer context.

For example, use fnlist to look at the user printer context for jsmith:

% fnlist user/jsmith/service/printer
celeste
lp
_default
myprinter

Similarly, you can look at the organization's printers:

% fnlist org/wiz.com/service/printer
sales_printer
mktg_printer
eng_printer

Alternatively, you can type:

% fnlist thisorgunit/service/printer

You can look at the printers at a specific site, for example, the printers in the MTV site:

% fnlist thisorgunit/site/MTV/service/printer
b1_printer
b2_printer

Client

The scenario for Example 3-2is a user who would like to print to a printer named colorful in his organization's context, thisorgunit/service/printer/colorful. The example printer client illustrates how the bindings for a specific printer are obtained.

The variable printer_binding contains the reference (the binding information) of the named printer. Using the binding information, the printer client can connect to the server and send the printer request. Note that the fn_ctx_lookup() function can be replaced by fn_ctx_list_name() or fn_ctx_list_bindings() to list all the names and their bindings.


Example 3-2 Print Client source

#include <stdio.h>
#include <xfn/xfn.h>
#include <string.h>
#include <stdlib.h>
 
/*
 * Routine to obtain the address of a specific printe.
 * This routine takes the printer name and the address type
 * as the input arguments and returns the address
 * of the requested printer.
 */
 
char *
get_address_of_printer(const char *printer_name,
	    const char *address_type)
{
   FN_composite_name_t *printer_name_comp;
   FN_status_t *status = fn_status_create();
   FN_ctx_t *initial_context;
   FN_ref_t *printer_ref;
   const FN_identifier_t *addr_id;
   const FN_ref_addr_t *address;
   char *addr_data; /* Return value */
 
   void *ip;
   size_t address_type_len, addr_len;
 
   /* Convert the printer name to a composite name */
   printer_name_comp = fn_composite_name_from_string(
      (const unsigned char *)printer_name);
 
   /* Get the initial context */
   initial_context = fn_ctx_handle_from_initial(0, status);
 
   /* Check status for any error messages */
   if (!fn_status_is_success(status)){
    fprintf(stderr,
         "Unable to obtain the initial context\n");
    return (0);
	}
  /* Perform a lookup for the printer name */
  printer_ref = fn_ctx_lookup(initial_context,
     printer_name_comp, status);
 
  /* Check status for any error messages */
  if (!fn_status_is_success(status)){
    fprintf(stderr, "Lookup failed on: %s\n",
        printer_name);
    return (0);
  }
 
  fn_ctx_handle_destroy(initial_context);
  fn_composite_name_destroy(printer_name_comp);
  address_type_len = strlen(address_type);
 
  /* Obtain the requested address from the address type */
  for (address = fn_ref_first(printer_ref, &ip);
     address != NULL;
     address = fn_ref_next(printer_ref, &ip)) {
         addr_id = fn_ref_addr_type(address);
         if (addr_id->length == address_type_len &&
            strncmp(address_type,
                (char *)addr_id->contents,
                address_type_len) == 0)
                break;
   }
  if (address == NULL)
      return (0);
  addr_len = fn_ref_addr_length(address);
  addr_data = (char *)(malloc(addr_len + 1));
  strncpy(addr_data,(char*)(fn_ref_addr_data(address)),
          addr_len);
  addr_data[addr_len] = '\0';
  fn_ref_destroy(printer_ref);
  return (addr_data);
}

 
 
 
  Previous   Contents   Next