PYRO - Name Server

## 5. Pyro Name Server

- The commands for starting a Name Server
- What does the server listen for?
- Pyro's hierarchical object naming scheme
- The different Name Server types available in Pyro
- Security plugin features
- Paired (failover) mode
- The Name Server Locator
- The special `PYRONAME://`, `PYROLOC://` and `PYROLOCSSL://` URIs
- The methods on the Name Server Pyro object

### The commands for starting a Name Server

There are a few commands (actually scripts) supplied in the `bin` directory that will start a Pyro Name Server for you:

`pyro-ns` (Name Server)
  - Arguments: [-h] [-k] [-m] [-r] [-x] [-n hostname] [-p port] [-b bcport] [-c bcaddr] [-i identification] [-d [databaselocation]] [-s securitymodule] [-1 [host:port]] [-2 [host:port]] [-v]
  - Starts the Pyro Name Server. The '-h' argument prints some help, '-k' makes it immune to shutdown requests (a crude form of security against malicious shutdown requests), '-m' allows multiple Name Servers to be started in the same network segment (default=not allowed), '-r' means that no lookup will be done to check for an already existing nameserver, '-x' means that no broadcast listener will be started, '-n hostname' selects the hostname the server should bind on (useful on systems with multiple network adapters/hostnames), '-p port' override the Name Server port number (0=random), '-b port' specifies the broadcast port number. '-c ipaddress' specifies a broadcast listener ip address override. With '-i identification' you can supply the authentication passphrase that will be required to connect to this server. When it contains spaces, use quotes around it. '-d' starts the persistent server. You can provide an optional name to specify the database location (directory). The '-s' option specifies the Python module for security plugins (make sure it is in your PYTHONPATH). Use '-v' to enable slightly more verbose output.
  The '-1' and '-2' options are used for paired mode. More info below.
`pyro-ns.cmd`
  The Windows script for the above.
`pyro-nssvc` (Windows-only Name Server 'NT-service' control script)
  - Arguments: [options] install|update|remove|start [...]|stop|restart [...]
  - On windows NT (2000/XP) systems, it's possible to register and start the Name server as a NT-service. You'll have to use the `nssvc.cmd` script to register it as a service. Make sure you have Pyro properly installed in your Python's site-packages. Or make sure to register the service using an account with the correct PYTHONPATH setting, so that Pyro can be located. The NS service logs to `C:\Pyro_NS_svc.log`, and writes its URI to `C:\Pyro_NS_URI.txt` (C: being your system drive).
  You can configure command line arguments for this service in the Registry. The key is: `HKLM\System\CurrentControlSet\Services\PyroNS`, and the value under that key is: `PyroServiceArguments` (REG_SZ, it will be asked and created for you when doing a `nssvc.cmd install` from a command prompt).
  Running the NS as a windows NT service it not well supported.
You can also use `python -m` to start the tools:
  `python -m Pyro.naming` - start the name server
  `python -m Pyro.nsc` - start the nsc tool. Also works with xnsc and wxnsc.

Using the `pyro-nsc` command (explained in the Usage chapter) you can control a Name Server that is already running.

Consider setting PYRO_CHECKSUM to 1 before starting the NS. It will communicate more reliably and the overhead is very small.

If you want to start the NS from within your own program, you can ofcourse start it by executing the start script mentioned above. You could also use the `Pyro.naming.NameServerStarter` class to start it directly (this is what the script also does). Be sure to start it in a separate process or thread because it will run in its own endless loop. You probably have to wait until the NS has been fully started, call the `waitUntilStarted()` method on the starter object. It returns true if the NS has been started, false if it is not yet ready. You can provide a timeout argument (in seconds).

### What does the server listen for?

When you start the name server it usually does two things. It starts a broadcast listener to respond to broadcast messages that may arrive, and it starts the name server itself. It binds (listens) on the default network interface, unless you specify otherwise with the hostname argument. Pyro does things a bit differently when it discovers that the server is listening on the local loopback adapter (also called localhost or 127.0.0.1). In this case, no broadcast server is started at all because it is not useful to have any: the name server is only accessible on the local system! Note that there could be a number of reasons of this happening: you force it to bind on localhost by specifying that as the hostname parameter, or there is no external network available, or the DNS of your system is wrongly configured. The Pyro log wil contain notes of this happening (and you can see it on the screen as well if you enable the verbose flag).

### Pyro's hierarchical object naming scheme

Brief: it's just like a filesystem with directories and files, and path separators.
Verbose: Pyro's object naming is hierarchical. Let's call the full set of names the namespace. A namespace has a single root that contains all other names. A name in the namespace is the name of a group or an object. A group can contain other groups, and objects. The root is a group.

Object names can be absolute or relative. Relative object names are searched in the default group (which has a special name, set by `PYRO_NS_DEFAULTGROUP`). Absolute names are always searched from the root. Absolute object names start with a special character that signifies the root; the colon ('`:`'). Relative object names don't have this character at the start.

Object names can be simple or compound. Compound object names consist of multiple parts separated by a special group separator character: the dot ('`.`'). Compound names are used to search within the namespace hierarchy. Each component of the name is the name of a group. The last name component can be the name of a group or an object (usually the latter).

Object name parts can only consist of the ASCII characters in the range 33-126 ('!'-'~') and except the backslash (\), dot ('.') and colon (':'). So spaces are also illegal in names, but normal slashes ('/') are okay.

Let's finish with a few examples to clarify all this. Note that by name alone you cannot distinguish a group name or an object name.

`:` | The namespace root group
`:TestObj` | The TestObj name in the root (most likely an object name)
`:Test.simple.object1` | The object1 name in the simple group in the Test group in the root.
`Test.simple.object1` | The object1 name in the simple group in the Test group in the default group (which is ":Default" if not configured otherwise. This is the Default group in the root).
`object1` | The object1 name in the default group.

### The different Name Server types available

There are two kinds of Name Servers available at the moment:

- Regular, non-persistent (`Pyro.naming.NameServer`)
- Persistent (`Pyro.naming.PersistentNameServer`)

The non-persistent NS stores its naming database only in memory. It is really fast because of that, but when it stops or crashes, the naming database is lost. All objects that were registered in the NS have to be re-registered.

The persistent NS stores its naming database on disk. Currently this is implemented the easy way; there is a direct mapping between the group names and directories on disk, and between object names + URIs and files in these directories on disk. The database by default is stored in a "Pyro_NS_database" directory that is created in the directory configured by `PYRO_STORAGE`. You can specify a different name if needed (with the '-d' option of the name server start script, and a parameter to the start method of the `NameServerStarter` class in the code).

Usually you don't access the `NameServer` or `PersistentNameServer` classes directly: there are scripts to start the right name server. Or you use the `NameServerStarter` helper class from the `Pyro.naming` module.

### Connecting objects to the Pyro Daemon with persistent naming

Usually you'll use the `connect` method of the daemon to connect your object instances to the daemon on the server. The daemon will register your object with the name server too, if you supplied a NS to the daemon and your object isn't transient (it has a name).

But, when using the persistent name server, there is a complication here: if you didn't explicitly remove your object from the NS, the entry will still be there the next time. Your connect attempt will then fail because your object cannot be registered again in the NS.

The solution is to use the `connectPersistent` method of the Pyro daemon. Except for the method name, you call it exactly like the regular `connect` method. It tries to find your object in the NS. If it's there already, the previous URI is used for your object (that also means that the object's GUID is replaced by the previous GUID that was found in the NS). If it isn't there, the regular `connect` call takes over.

Of course you could always play safe and explicitly unregister any possible previous occurrences from the NS before you connect new instances. This is what all examples do by the way, so you can safely run an example again and again.

For your information, the code that starts the Persistent Name Server uses `connectPersistent` to connect the name server object to the daemon. Why? because the name server itself is also registered in the NS database, and it is necessary that when the NS restarts, it uses the URI of the previous instance if found in the persistent database.

### Security plugin features

The Name Server supports security plugins, to facilitate access control to the Name Server. When starting the NS, you can supply an option that tells the NS to load that specific Python module and use the security plugins that you defined in there. For more information, see the Security chapter.

### Paired (failover) mode

For improved availability you can run the Name Server in paired mode. This means that you start another Name Server instance that works together with the other one, as a pair, but providing a single Namespace. If one of them dies, the other takes over. They synchronize every namespace change and update so that they are always each other's exact copy. If a dead NS is restarted, it resyncs the full Namespace from the other copy that is still running.
Pyro clients that do a NS lookup automatically get the other NS if the first is dead and vice versa, you don't have to change anything in the client code.

Paired mode is activated by using the -1 and -2 options when starting the NS. 1 means: this is the first one, 2 means: this is the second one of the pair (this distinction is necessary because of a slightly different startup procedure depending on being the first or the second).
You can add a hostname:port argument to the -1 and -2 option, that specify the location of the other name server. So, for instance:
`pyro-ns -n atlantis -p 4000 -1 atlantis:5000` to start NS #1,
`pyro-ns -n atlantis -p 5000 -2 atlantis:4000` to start NS #2. But usually just -1 or -2 is good enough.

There are three new config items dealing with paired mode NS: `PYRO_NS2_HOSTNAME`, `PYRO_NS2_PORT` and `PYRO_NS2_BC_PORT`. They can be used to specify non-default values for the hostname and port number that the second NS will use. See the config chapter.

Resync mechanism: Start A, then B. B remembers A's location (either discovered trough NS lookup or via command line URL). B notifies A that it has started, provides own location, and gets a copy of A's namespace database. A remembers B's location. Any namespace change in A is replicated in B and vice versa, using ONEWAY calls. If an error occurs, the reference to the faulty NS is discarded. If you discover that somehow the namespaces get out-of-sync, just kill the one that is faulty and restart it. It will automatically resync with the 'good' one.

With Identification: If you want to use paired mode together with identification (-i), you must supply the same identification argument to both name servers.

### The Name Server Locator

Pyro's NS is actually two servers: the normal Pyro server but when using TCP/IP sockets, also a broadcast listener. The latter helps clients find the location of the Name Server by answering to broadcast packet requests. To hide that broadcast lookup mechanism from you, and to make the lookup very easy, we have the NameServer Locator, defined in the `Pyro.naming` package. This object gets a Pyro proxy for the NS for you. Because this is the recommended (and easiest way) to gain access to the Name Server, you're not interested in the internal name Pyro uses for the Name Server. But for consistency, it is defined, and the Name Server object itself is known in the Name Server's namespace under the name available in `Pyro.constants.NAMESERVER_NAME`.

There are essentially three ways to get a reference to the Name Server:

- Use the `NameServerLocator`'s broadcast mechanism. This only works if your network supports broadcasting and the NS is reachable by a broadcast request.

         locator = Pyro.naming.NameServerLocator()
         ns = locator.getNS()

  If your network doesn't allow broadcasts, or the broadcast can't reach the NS, this mechanism doesn't work. There is a simple workaround: just set the `PYRO_NS_HOSTNAME` config option to the hostname on which your NS can be found. This disables the broadcast lookup and uses the one below instead.

  For simplicity, if Pyro cannot find the name server using a broadcast search, it tries to contact it directly on the machine itself and localhost. This allows you to use it even on a system where the name server is running on localhost (for some reason) without having to set `PYRO_NS_HOSTNAME` to `localhost`.

  You can override the default broadcast address (255.255.255.255) by setting the `bcaddr` parameter:

      ns=locator.getNS(bcaddr="192.168.1.255")

- Use the `NameServerLocator`'s direct host mechanism. This only works if you know the host on which the NS is located. The `port` argument is optional. If the NS has been started using a non-default port number you can use it to specify the port number.

         locator = Pyro.naming.NameServerLocator()
         ns = locator.getNS(host='hostname', port=7777)

  If you specify the hostname yourself, the locator doesn't attempt to find the NS with the broadcast mechanism, and therefore there is no lookup delay. Also you can specify a port number different from the default port. If you set the `PYRO_NS_HOSTNAME` config option, the locator automatically uses the specified host for a direct lookup. You don't have to pass `Pyro.config.PYRO_NS_HOSTNAME` to the `getNS` method.
- Use the URI string the NS writes to a special file. This always works but is a bit more cumbersome, and your file should be copied or accessible across a network file system. Anyway, assume the NS writes to '`Pyro_NS_URI`':

         uri = open('Pyro_NS_URI','r').read()
         uri = Pyro.core.PyroURI(uri)   # convert string to real URI object
         ns = Pyro.naming.NameServerProxy(uri)    # create a proxy for the NS
         ... you can now invoke methods, such as ns.ping() ...

  Note: The URI changes every time a Pyro server or object is created. You cannot use a previously written URI when you have restarted the server.
  Note: It's highly recommended to just use the provided proxy to access the NS! Using a dynamic proxy not obtained trough the locator, or some other means to access the Name Server, will probably screw up the name scheme and default name groups, unless your own code explicitly takes care of this (which is unlikely). This is cumbersome and the built-in proxy (`Pyro.naming.NameServerProxy`) contains all necessary logic already. So just use that one. If you use the locator (see above) you will get a correct proxy automatically.
  Note: the constructor of the `NameServerProxy` has an optional second argument: the connection authentication information.

### The special `PYRONAME://`, `PYROLOC://` and `PYROLOCSSL://` URIs

For the lazy ones among us, there is an even simpler way of using the Name Server to look up objects by their name. Instead of using the `PYRO://` URIs that the Name Server returns, and where you then get a proxy object for, you use another URI format. This format is as follows:
`PYRONAME://nshostname:port/objectname` (PYRONAMESSL is not yet implemented)
The `nshostname` is the name of the host the Name Server is running on, and `port` may be a non-default port the Name Server is listening on. Both may be omitted. `objectname` is the name of the object you want to find! So, the next code fragment will find the NS using the default lookup mechanism, resolve the object name to a real URI, create a proxy for that, and call a method:

        Pyro.core.getProxyForURI('PYRONAME://:Test.MyObject').getQuote()

So we now have remote object method invocation in one statement :-) There is one important point: each time a `PYRONAME://` URI is used, a lookup for the Name Server has to be performed, and then a lookup for the object name. This is much slower than the regular method. However, once you've constructed a proxy for this URI, no more lookups are performed.

There is another special URI, that bypasses the Name Server completely:
`PYROLOC://hostname:port/objectname`
`PYROLOCSSL://hostname:port/objectname` (use this if the server is running in SSL mode)
This time `hostname` is required and is the name of the host that your target Pyro object runs on. `port` may be a non-default port the Pyro daemon is running on, and may be omitted. `objectname` is the internal name for the Pyro object you want to access. When you use this URI, the Name Server is bypassed and the target server is contacted directly to get the regular URI for the desired object. The advantage of this is that you don't have to have a Pyro Name Server running. The disatvantages are obvious; you miss all the features of the Name Server and you have to administrate server object names yourself somehow. You must use the name that is passed to the `connect` method of the Daemon. There is no hierarchical naming scheme because the Name Server is not used at all. Once the object is found, the real URI is stored and no more lookups need to be done. The next code fragment shows how to call a remote object, without requiring a Name Server to be present:

        Pyro.core.getProxyForURI('PYROLOC://localhost/MyObject').getQuote()

(Use `PYROLOCSSL://` if the server is running in SSL mode) Remember that your server does not have to rely on a Name Server when you want to use this mechanism. When you enable Pyro logging, you might get a WARN that a Name Server is not specified. You can ignore this. Please also see the "noNS" example, that shows how to use this URI, and also how you could connect directly by using an URI string that comes off the server.

### The methods on the Name Server Pyro object

Ok, you've got a proxy for the Name Server. Now what to do with it? You've already seen the most important method: `resolve`. There are more methods, see below. One very important thing to realize: all names you supply must be absolute, i.e. ":Group.Objectname" instead of just "Objectname". The `NameServerProxy` that you'll usually be working with has some logic that takes care of this.

`resolve(name)`
  Look up the object with the given name and returns the Pyro URI. (it will always return a real PyroURI object, not a string).
`register(name, URI)`
  Registers an object with the given name and the given Pyro URI. The URI can be a PyroURI object or just a string.
`unregister(name)`
  Removes the object with the given name from the naming database.
`ping()`
  Does nothing, just to test if the Name Server is running.
`list(groupname)`
  Returns a list of the given group. If groupname is None, lists the root group. The list contains tuples (name,type) where name is the object name, and type is 0 for a naming group, and 1 for an object name.
`flatlist()`
  Returns a list of all objects in the naming database. The list contains tuples (name, uri) where name is the absolute object name and uri is the associated PyroURI object.
`createGroup(name)`
  Create a new empty naming group.
`deleteGroup(name)`
  Delete a naming group, including all its contents.
`fullName(name)`
  Returns the full (absolute) name of the object (prefixed by the default group)
`setMeta(name, meta)`
  sets user meta information for the given name. meta can be any object that can be pickled
`getMeta(name)`
  retrieves the user meta information for the given name.

See the source code of the (x)nsc tools (`nsc.py / xnsc.py`) for more info.

#### Important notice:

By default, there is no access control on the Name Server. It is possible for anybody to remove and overwrite an existing registered object. You must be very aware of this, because it is very easy to sneak a trojan in by overwriting a name with a reference to the trojan object! This free access is necessary for instance to be able to use the "pyro-nsc" tool without restriction.