/**
   * Constructs a new server session listener per specifications in uri and resources. This listener
   * will accept requests from clients for new server sessions.
   *
   * @param uri contains specifications for the server session listener and for the server session
   *     transport stack.
   * @param resources additional resources to aid in constructing new server sessions.
   * @param implFactory factory used to construct a new instance implementing WebLinkServer. The new
   *     instance will receive and process messages from the client session.
   * @return a server session listener.
   * @throws Exception
   */
  public static ServerFactory newListener(
      final String uri, final Resources resources, final WebLinkServerFactory implFactory)
      throws Exception {
    final Resources res = initResources(resources);

    final Transport<ServerFactory> listener = TransportFactory.getListener(uri, res);

    return new DefaultServerFactory(listener, implFactory) {
      public void newServer(TransportMessage t, String uri, Resources r) throws Exception {
        ValueFactory vf = (ValueFactory) r.get(Transport.VALUE_FACTORY);
        MailboxManager x = new PlainMailboxManager(t, uri, r);
        DeliveryService d = new DefaultDeliveryService(x, uri, r);
        RemoteWebLinkClient client = new RemoteWebLinkClient(d, vf);
        WebLinkServer server = implFactory.newWebLinkServer(client);
        Pool qp = (Pool) r.get(QUEUED_POOL);
        Pool fp = (Pool) r.get(FREE_POOL);
        new StubWebLinkServer(d, server, qp, fp);
        client._start();
      }

      public ValueFactory newValueFactory(String uri) {
        return new ValueFactoryWebLink(uri);
      }

      @Override
      public String toString() {
        return "WebLinkHelper.ServerFactory/" + listener;
      }
    };
  }
  /**
   * Constructs a new client session per specifications in uri and resources.
   *
   * @param uri contains specifications for the client session transport stack.
   * @param resources additional resources to aid in constructing new client sessions.
   * @param implFactory factory used to construct a new instance implementing WebLinkClient. The new
   *     instance will receive and process messages from the server session.
   * @return an instance of RemoteWebLinkServer initialized by uri and resources which may be used
   *     to send messages to the server session.
   * @throws Exception
   */
  public static RemoteWebLinkServer newServer(
      String uri, Resources resources, WebLinkClientFactory implFactory) throws Exception {
    final Resources res = initResources(resources);

    final ValueFactoryWebLink vf = new ValueFactoryWebLink(uri);
    res.put(Transport.VALUE_FACTORY, vf);

    URL u = new URL(uri);

    TransportMessage m = TransportFactory.getTransport(uri, res);
    MailboxManager r = new PlainMailboxManager(m, u, resources);
    DeliveryService d = new DefaultDeliveryService(r, u, resources);
    RemoteWebLinkServer server = new RemoteWebLinkServer(d, vf);
    WebLinkClient client = implFactory.newWebLinkClient(server);
    Pool qp = (Pool) res.get(QUEUED_POOL);
    Pool fp = (Pool) res.get(FREE_POOL);
    new StubWebLinkClient(d, client, qp, fp);

    return server;
  }