Package org.apache.vinci.transport

Examples of org.apache.vinci.transport.VinciFrame


     */
    public void endElement(String uri, String name, String qName) {
      // Debug.p("End element: " + qName);
      if (top.sub_entries.size() == 0) {
        // Debug.p("Empty element.");
        top.component = new VinciFrame();
      } else {
        StackEntry child = (StackEntry) top.sub_entries.get(0);
        // Debug.p("Frameleaf.");
        if (top.sub_entries.size() == 1 && child.sub_entries == null) {
          // This is a frameleaf
          top.component = new FrameLeaf(child.ename_or_pcdata);
        } else {
          VinciFrame c = new VinciFrame();
          top.component = c;
          for (int i = 0; i < top.sub_entries.size(); i++) {
            child = (StackEntry) top.sub_entries.get(i);
            if (child.component != null) {
              c.add(child.ename_or_pcdata, child.component);
            } else {
              c.fadd(TransportConstants.PCDATA_KEY, child.ename_or_pcdata);
            }
            child.sub_entries = null;
          }
        }
      }
View Full Code Here


   * @pre priority &ge; -1
   * @pre instance &ge; 0
   */
  static public Transportable composeQuery(String service_name, String host_name, int priority,
          int instance) {
    VinciFrame query = (VinciFrame) composeQuery(service_name, host_name, priority);
    query.fadd(VNSConstants.INSTANCE_KEY, instance);
    return query;
  }
View Full Code Here

   * @pre service_name != null
   * @pre host_name != null
   * @pre priority &ge; -1
   */
  static public Transportable composeQuery(String service_name, String host_name, int priority) {
    VinciFrame query = (VinciFrame) composeQuery(service_name, host_name);
    query.fadd(VNSConstants.LEVEL_KEY, priority);
    return query;
  }
View Full Code Here

   *
   * @pre service_name != null
   * @pre host_name != null
   */
  static public Transportable composeQuery(String service_name, String host_name) {
    VinciFrame query = new VinciFrame();
    query.fadd(TransportConstants.COMMAND_KEY, VNSConstants.SERVEON_COMMAND);
    query.fadd(VNSConstants.SERVICE_KEY, service_name);
    query.fadd(VNSConstants.HOST_KEY, host_name);
    return query;
  }
View Full Code Here

  }

  /* Main processing routine */
  public Transportable eval(Transportable inp) throws ServiceException {
    VinciFrame in = (VinciFrame) inp;
    VinciFrame out = null;

    Debug.p("Request from " + in.fgetString("vinci:REMOTEIP"));

    String command;
    command = in.fgetString("vinci:COMMAND");
    if (command == null) {
      out = new VinciFrame();
      out.fadd("vinci:ERROR", "Malformed request");
      return out;
    }

    try {
      if (VNSConstants.RESOLVE_COMMAND.equals(command)) {
        out = resolve(in);
      } else if (VNSConstants.SERVEON_COMMAND.equals(command)) {
        out = serveon(in);
      } else if (command.equals(dirCmdAddService))
        out = addService(in);
      else if (command.equals(dirCmdAddAlias))
        out = addAlias(in);
      else if (command.equals(dirCmdUpdateService))
        out = updateService(in);
      else if (command.equals(dirCmdDelService))
        out = delService(in);
      else if (command.equals(dirCmdDelAlias))
        out = delAlias(in);
      else if (command.equals(dirCmdGetList))
        out = getList(in);
      else if (command.equals(dirCmdGetNames))
        out = getNames(in);
      else if (command.equals(dirCmdGetHits))
        out = getHits(in);
      else {
        out = new VinciFrame();
        out.fadd("vinci:ERROR", "Unrecognized command");
      }
    } catch (Exception e) {
      // Exact translation of the Python code [may not be necessary]
      out = new VinciFrame();
      out
              .fadd(
                      "vinci:ERROR",
                      "Critical error :\n"
                              + e
                              + "\nThe server MAY not respond to further requests.\nPlease notify the administrator\n");
View Full Code Here

  /* Various processing routines */
  VinciFrame resolveLocal(VinciFrame in) {

    Debug.p("Local resolve");

    VinciFrame out = new VinciFrame();

    String name = in.fgetString("SERVICE");
    if (strip(name) == null)
      return new ErrorFrame("No service name specified");

    String host = strip(in.fgetString("HOST"));
    String realhost = strip(in.fgetString("IP"));
    if (strip(realhost) == null && strip(host) != null) {
      try {
        // Try to resolve the IP if possible
        realhost = (InetAddress.getByName(host)).getHostAddress();
      } catch (Exception e) {
        // realhost = null;

        // return error message
        return new ErrorFrame("Could not resolve IP address for service " + name);
      }
    }

    String instance = in.fgetString("INSTANCE");
    int inst = -1;
    try {
      inst = Integer.parseInt(instance);
    } catch (Exception e) {
      inst = -1;
    }

    String level = in.fgetString("LEVEL");

    // Default as specified in SPEC.txt
    if (strip(level) == null || level.trim().toLowerCase().equals("none"))
      level = "-1";
    else if (level.toLowerCase().equals("all")) {
      Debug.p("Specific level must be given (not all)");
      out.fadd("vinci:ERROR", "Specific level must be given (not all)");
      return out;
    }

    synchronized (SR) {
      // Construct a valid number from the level string specified
      level = "" + SR.getLevel(name, level);
    }
    Service[] servicesList;

    // Check the cache for matches
    CachedItem citem = (CachedItem) checkCache(name + "[" + level + "]");
    if (citem == null) {
      synchronized (SR) {
        // Find all services that match the specified name and are <= level specified
        // Also, resolve all aliases to actual services
        servicesList = SR.getServices(name, level, true);
      }
      // Cache the result (if any matches found)
      if (servicesList != null && servicesList.length > 0) {
        // Cache the resolve result under the actual name and level returned
        cache(name + "[" + servicesList[0].level + "]", new CachedItem(servicesList));
        // Have a proxy pointer to the entry created if it is not the same
        if (!servicesList[0].level.equals(level)) {
          cache(name + "[" + level + "]", new ProxyCachedItem(name + "[" + servicesList[0].level
                  + "]"));
        }
      }
    } else
      servicesList = citem.servicesList;

    if (servicesList == null) {
      Debug.p("Service " + name + " not found");
      out.fadd("vinci:ERROR", "Service " + name + " not found");
      return out;
    }

    Debug.p("Number of services found with name = " + name + ", and level = " + level + " : "
            + servicesList.length);

    if (servicesList.length == 0)
      System.err.println("NO SERVICES FOUND WITH REALHOST = " + realhost + " name = " + name);

    ArrayList v = new ArrayList();

    // Filter the services with matching realhost and instance
    for (int i = 0; i < servicesList.length; i++) {
      if (realhost != null && !servicesList[i].realhost.equals(realhost))
        continue;
      if (inst > -1 && inst != servicesList[i].instance)
        continue;
      v.add(servicesList[i]);
    }

    servicesList = new Service[v.size()];

    for (int i = 0; i < v.size(); i++)
      servicesList[i] = (Service) v.get(i);

    if (servicesList == null || servicesList.length == 0) {
      Debug.p("Service " + name + " not found");
      out.fadd("vinci:ERROR", "Service " + name + " not found");
      return out;
    }

    VinciFrame temp;
    for (int j = 0; j < servicesList.length; j++) {
      temp = new VinciFrame();

      temp.fadd("HOST", (String) servicesList[j].getAttr("host"));
      temp.fadd("PORT", "" + servicesList[j].port);
      temp.fadd("INSTANCE", "" + servicesList[j].instance);

      if (servicesList[j].meta) {
        Object o = servicesList[j].dict.get("META");
        if (o instanceof String)
          temp.fadd("META", (String) o);
        if (o instanceof Frame)
          temp.fadd("META", (Frame) o);
      }

      out.fadd("SERVER", temp);
    }

View Full Code Here

      // New spec - assume it is local
      return resolveLocal(in);
    }

    Service[] services = SR.getServices(ENV_PROXY);
    VinciFrame out = new VinciFrame();
    if (services == null || services.length == 0) {
      out.fadd("vinci:ERROR", "Cannot locate proxy service");
      return out;
    }

    // Iterate through all the registered proxy services and try to resolve at each
    for (int i = 0; i < services.length; i++) {
      Service S = services[i];
      if (S.realhost == null) {
        continue; // Can't do anything if realhost isn't specified
      }

      try {
        // If the host specified is the local one, then use resolveLocal()
        // else it will have an infinite loop
        if (S.port == srvPort && InetAddress.getLocalHost().getHostAddress().equals(S.realhost))
          out = resolveLocal(in);
        else {
          Debug.p("Resolving with VNS: " + S.realhost + ":" + S.port);
          out = BaseClient.rpc(in, S.realhost, S.port);
        }
        System.out.println(out.toXML());
        if (out == null || out instanceof ErrorFrame || strip(out.fgetString("LEVEL")) == null
                || out.fgetString("vinci:ERROR") != null) {
          continue; // Check if resolve actually worked
        }
        return out;
      } catch (ServiceException se) {
        // Don't need to worry - caused due to an ErrorFrame as the server was unable to
        // resolve
        System.out.println(se);
        se.printStackTrace();
      } catch (Exception e) {
        e.printStackTrace();
        out.fadd("vinci:ERROR", "Proxy forwarding failed due to " + e);
        return out;
      }
    }
    return new ErrorFrame("Proxy forwarding failed");
  }
View Full Code Here

  VinciFrame resolveDefaults(VinciFrame in) {
    String name = in.fgetString("SERVICE");
    String workspace;

    VinciFrame out;

    if (WS.workspace == null || WS.search.size() == 0)
      return resolveLocal(in); // Use resolveLocal() if WS was not defined

    for (int i = 0; i < WS.search.size(); i++) {
      workspace = ((String) WS.search.get(i));

      if (workspace.equals(WS.workspace)) {
        Debug.p("Resolving on local workspace ...");

        out = resolveLocal(in);
      } else {
        Debug.p("Resolving on workspace " + workspace + " ...");
        out = resolveProxy(in, workspace);
      }

      if (out.fgetString("vinci:ERROR") == null)
        return out;
    }

    Debug.p("Resolution failed");
    return new ErrorFrame("Could not find service " + name + " in default workspaces");
View Full Code Here

  VinciFrame serveon(VinciFrame in) {
    logRequest(VNSConstants.SERVEON_COMMAND, in.fgetString("vinci:REMOTEIP"), null);

    Service S = null, srv = null;
    VinciFrame out = new VinciFrame();

    String name = in.fgetString("SERVICE");
    if (strip(name) == null)
      return new ErrorFrame("Invalid service name specified : " + name);
    String host = in.fgetString("HOST");

    if (strip(host) == null) {
      Debug.p("Getting host from socket peer info");
      host = in.fgetString("vinci:REMOTEHOST");
      if (host == null)
        host = in.fgetString("vinci:REMOTEIP");
      if (host == null)
        return new ErrorFrame("Host could not be parsed - specify HOST");
      Debug.p("Peer host is : " + host);
    }

    String level = in.fgetString("LEVEL");

    // Default as specified in SPEC.txt
    if (strip(level) == null || level.trim().toLowerCase().equals("none"))
      level = "0";

    if (level.equals("all")) {
      out.fadd("vinci:ERROR", "Specific level must be given or none at all");
      return out;
    }

    String instance = in.fgetString("INSTANCE");
    try {
      instance = "" + Integer.parseInt(instance);
    } catch (Exception e) {
      instance = null;
    }

    if (instance == null)
      instance = "0";

    Debug.p("Host = " + host);
    String realhost = in.fgetString("IP");
    try {
      if (realhost == null)
        realhost = InetAddress.getByName(host).getHostAddress();
    } catch (Exception e) {
      out.fadd("vinci:ERROR", "Could not resolve IP due to Exception " + e);
      return out;
    }

    Debug.p("search: realhost = " + realhost + " - instance = " + instance);

    synchronized (SR) {
      level = "" + SR.getLevel(name, level);
    }

    Debug.p("Level = " + level);

    // Update the cache
    updateCache(name + "[" + level + "]");

    Object[] services;
    synchronized (SR) {
      services = SR.getServices(name, level);
    }

    if (services != null) {
      for (int i = 0; i < services.length; i++) {
        S = (Service) services[i];
        Debug.p("current: realhost = " + S.realhost + " - instance = " + S.instance);
        if (S.realhost.equals(realhost) && instance.equals("" + S.instance)
                && S.level.equals(level)) {
          srv = S;
          break;
        }
      }
    }

    if (srv == null) {
      Debug.p("Creating now");
      Hashtable H = new Hashtable();
      H.put("NAME", name);
      H.put("HOST", host);
      H.put("LEVEL", level);
      H.put("INSTANCE", instance);
      H.put("IP", realhost);
      srv = new Service(H);
      boolean ok = false;
      synchronized (SR) {
        Debug.p("Adding service : " + H.get("NAME") + ", lvl=" + H.get("LEVEL") + ",instance="
                + H.get("INSTANCE") + ",ip=" + H.get("IP"));
        ok = SR.addService(srv);
      }
      if (!ok) {
        out.fadd("vinci:ERROR", "COuld not find or add service " + name);
        return out;
      }
    } else {
      if (srv.minport != srv.maxport) { // if it isn't running on a fixed port
        final String s_host = srv.host;
        final int s_port = srv.port;
        new Thread(new Runnable() {
          public void run() {
            Debug.p("Trying to shutdown old service ...");
            VinciFrame shutdown = new VinciFrame();
            shutdown
                    .fadd(
                            "vinci:SHUTDOWN",
                            "Identical service started on this host. Use the INSTANCE tag to run multiple instances of the same service on a single host.");
            try {
              VinciFrame f = BaseClient.rpc(shutdown, s_host, s_port, 10000);
              Debug.p("Shutdown response received: " + f.toXML());
            } catch (Exception e) {
              Debug.p("Old service already closed: " + e);
            }
          }
        }).start();
View Full Code Here

  }

  VinciFrame addService(VinciFrame in) {
    logRequest(dirCmdAddService, in.fgetString("vinci:REMOTEIP"), null);

    VinciFrame service = in.fgetVinciFrame("SERVICE");

    Hashtable H = new Hashtable();

    int total = service.getKeyValuePairCount();
    KeyValuePair P;
    for (int i = 0; i < total; i++) {
      P = service.getKeyValuePair(i);
      if (P.isValueALeaf()) {
        H.put(P.getKey(), P.getValueAsString());
      } else {
        H.put(P.getKey(), P.getValue());
      }
View Full Code Here

TOP

Related Classes of org.apache.vinci.transport.VinciFrame

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.