Package de.desy.tine.console

Source Code of de.desy.tine.console.TCommandList

package de.desy.tine.console;

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;

import de.desy.tine.addrUtils.TSrvEntry;
import de.desy.tine.client.TLinkFactory;
import de.desy.tine.dataUtils.TDataTime;
import de.desy.tine.definitions.TAccess;
import de.desy.tine.definitions.TErrorList;
import de.desy.tine.headers.TPHdr;
import de.desy.tine.server.equipment.TEquipmentModuleFactory;
import de.desy.tine.server.logger.DbgLog;
import de.desy.tine.server.logger.MsgLog;
import de.desy.tine.startup.TInitializer;
import de.desy.tine.startup.TInitializerFactory;

public class TCommandList
{
  private static boolean initialized = false;
  private static void initialize()
  {
    if (initialized) return;
    try
    {
      addCommandItem(new TCommandItem("help","available debug commands",TCommandList.class.getMethod("listItems", (Class<?>[])null),(int)TAccess.CA_NULL));
      addCommandItem(new TCommandItem("connections","current connection list",TLinkFactory.class.getMethod("dumpLinkTable", (Class<?>[])null)));
      addCommandItem(new TCommandItem("addresses","current connection list",TLinkFactory.class.getMethod("dumpLinkAddresses", (Class<?>[])null)));
      addCommandItem(new TCommandItem("globals","current globals table",TLinkFactory.class.getMethod("dumpGlobals", (Class<?>[])null)));
      addCommandItem(new TCommandItem("groups","current link groups list",TLinkFactory.class.getMethod("dumpGroups", (Class<?>[])null)));
      addCommandItem(new TCommandItem("redirections","current redirection list",TLinkFactory.class.getMethod("dumpRedirectionTable", (Class<?>[])null)));
      addCommandItem(new TCommandItem("relinks","current relinked links list",TLinkFactory.class.getMethod("dumpRelinkTable", (Class<?>[])null)));
      addCommandItem(new TCommandItem("clients","current client list",TLinkFactory.class.getMethod("dumpClientTable", (Class<?>[])null)));
      addCommandItem(new TCommandItem("contracts","current contract list",TLinkFactory.class.getMethod("dumpContractTable", (Class<?>[])null)));
      addCommandItem(new TCommandItem("messages","message table entries",MsgLog.class.getMethod("dumpMessages", (Class<?>[])null)));
      addCommandItem(new TCommandItem("stats","relevant system statistics",TLinkFactory.class.getMethod("dumpStats", (Class<?>[])null)));
      addCommandItem(new TCommandItem("users","users in the access control list",TLinkFactory.class.getMethod("dumpUserLists", (Class<?>[])null)));
      addCommandItem(new TCommandItem("nets","nets in the access control list",TLinkFactory.class.getMethod("dumpNetsLists", (Class<?>[])null)));
      addCommandItem(new TCommandItem("settings","current system settings",TLinkFactory.class.getMethod("dumpSettings", (Class<?>[])null)));
      addCommandItem(new TCommandItem("debug","debug level",
          TCommandList.class.getMethod("dumpDebugLevel", (Class<?>[])null),
          TLinkFactory.class.getMethod("setOutputDebugLevel", new Class[]{int.class}),
          TAccess.CA_READ|TAccess.CA_WRITE));
      addCommandItem(new TCommandItem("textbuffersize","maximum console display size (bytes)",
          TConsole.class.getMethod("getMaxTextWindowSize", (Class<?>[])null),
          TConsole.class.getMethod("setMaxTextWindowSize", new Class[]{int.class}),
          TAccess.CA_READ|TAccess.CA_WRITE));         
      addCommandItem(new TCommandItem("filter","debug filter",
          DbgLog.class.getMethod("dumpFilters", (Class<?>[])null),
          DbgLog.class.getMethod("setFilter", new Class[]{String.class}),
          TAccess.CA_READ|TAccess.CA_WRITE));
      addCommandItem(new TCommandItem("filterlinks","current filter list",TLinkFactory.class.getMethod("dumpFilterList", (Class<?>[])null)));
      addCommandItem(new TCommandItem("which","address check",TSrvEntry.class.getMethod("which", new Class[]{String.class}),(int)TAccess.CA_NULL));
      addCommandItem(new TCommandItem("modules","registered equipment modules",TLinkFactory.class.getMethod("dumpModules", (Class<?>[])null)));
      addCommandItem(new TCommandItem("properties","registered properties for equipment module given",TLinkFactory.class.getMethod("dumpProperties", new Class[]{String.class}),TAccess.CA_READ));
      addCommandItem(new TCommandItem("devices","registered devices for equipment module given",TLinkFactory.class.getMethod("dumpDevices", new Class[]{String.class}),TAccess.CA_READ));
      addCommandItem(new TCommandItem("deadbands","property access deadband list",TLinkFactory.class.getMethod("dumpDeadbands"new Class[]{String.class}),TAccess.CA_READ));
      addCommandItem(new TCommandItem("version","current system version",TCommandList.class.getMethod("dumpVersion", (Class<?>[])null)));
      addCommandItem(new TCommandItem("time","current system time",TDataTime.class.getMethod("dumpTime", (Class<?>[])null)));
      addCommandItem(new TCommandItem("reset","reset link groups",TLinkFactory.class.getMethod("resetGroups", (Class<?>[])null),(int)TAccess.CA_NULL));
      addCommandItem(new TCommandItem("connection","connection info",TLinkFactory.class.getMethod("dumpLinkEntry", new Class[]{int.class})));
      addCommandItem(new TCommandItem("lockout","simulate timeouts",
          TPHdr.class.getMethod("getLockOutAsInt", (Class<?>[])null),
          TPHdr.class.getMethod("setLockOut", new Class[]{int.class}),
          TAccess.CA_READ|TAccess.CA_WRITE));
      addCommandItem(new TCommandItem("trace","trace link",
          TCommandList.class.getMethod("dumpTrace", (Class<?>[])null),
          TCommandList.class.getMethod("setTrace", new Class[]{String.class}),
          TAccess.CA_READ|TAccess.CA_WRITE));
      addCommandItem(new TCommandItem("flush","remove contracts list (or targeted list)",TCommandList.class.getMethod("flush", new Class[]{String.class}),(int)TAccess.CA_NULL));     
    }
    catch (Exception e)
    {
      MsgLog.log("TCommandList.initialize", "could not add command : "+e.getMessage(),TErrorList.code_failure,e,1);
    }
    initialized = true;
  }
  TCommandList()
  {
    initialize();
  }
  private static HashMap<String,TCommandItem> cmdLst = new HashMap<String,TCommandItem>();
  public static void addCommandItem(TCommandItem item)
  {
    if (item == null) return;
    if (cmdLst.containsKey(item.getCommandString())) return;
    cmdLst.put(item.getCommandString(), item);
  }
  private static OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
  public static void flush(String target)
  {
    if (target == null) target = "contracts";
    if (target.compareToIgnoreCase("contracts") == 0)
    {
      TEquipmentModuleFactory.getInstance().flushContractTable();
      return;
    }
    if (target.compareToIgnoreCase("filters") == 0)
    {
      TLinkFactory.flushFilterLinks();
      return;
    }
  }
  public static void dumpVersion()
  {
    TInitializer ti = TInitializerFactory.getInstance().getInitializer();
    TLinkFactory.dbgPrint("TINE library version: "+ti.getVersion());
    TLinkFactory.dbgPrint("TINE library build id: "+ti.getBuildId());
    TLinkFactory.dbgPrint("host platform: "+os.getName()+" "+os.getVersion());
    TLinkFactory.dbgPrint("java version: "+System.getProperty("java.version"));

  }
  public static void dumpDebugLevel()
  {
    TLinkFactory.dbgPrint("current debug level: "+TLinkFactory.getOutputDebugLevel());   
  }
  public static void setTrace(String traceString)
  {
    TLinkFactory.setTraceLinkKey(traceString);
    dumpTrace();
  }
  public static void dumpTrace()
  {
    String trace = TLinkFactory.getTraceLinkKey();
    TLinkFactory.dbgPrint("current link trace: "+(trace == null ? "not set" : trace));
  }
  public static void listItems()
  {
    if (!initialized) initialize();
    Iterator<TCommandItem> it = cmdLst.values().iterator();
    TCommandItem cmd;
    String prefix;
    while (it.hasNext())
    {
      cmd = it.next();
      if (cmd.canSet()) prefix = "set ";
      else if (cmd.canGet()) prefix = "get ";
      else prefix = "";
      String cmdstr = prefix + cmd.getCommandString();
      //if (cmdstr.length() < 8) cmdstr += "\t"; <- this is hopeless due to different (possibly remote) fonts
      TLinkFactory.dbgPrint(cmdstr+"  ->  "+cmd.getCommandDescription());
    }
  }
  public static void parseCommand(String cmdstr)
  {
    String cmd;
    StringTokenizer st = new StringTokenizer(cmdstr," \t\n\r\f=()");
    if (!st.hasMoreTokens()) return;
    cmd = st.nextToken();
    if (cmd.compareToIgnoreCase("get") == 0)
    {
      if (!st.hasMoreTokens())
      {
        TLinkFactory.dbgPrint("command incomplete!");
        return;
      }
      cmd = st.nextToken();
      if (!st.hasMoreTokens())
      {
        executeCommand(cmd,TAccess.CA_READ);
        return;       
      }
      String strval = st.nextToken();
      try
      {
        int ival = Integer.parseInt(strval);
        executeCommand(cmd,ival,TAccess.CA_READ);
      }
      catch (Exception e)
      {
        executeCommand(cmd,strval,TAccess.CA_READ);
      }
      return;
    }
    if (cmd.compareToIgnoreCase("set") == 0)
    {
      if (!st.hasMoreTokens())
      {
        TLinkFactory.dbgPrint("command incomplete!");
        return;
      }
      cmd = st.nextToken();
      String strval = "";
      while (st.hasMoreTokens())
      {
        strval += st.nextToken();
      }
      try
      {
        int ival = Integer.parseInt(strval);
        executeCommand(cmd,ival,TAccess.CA_WRITE);
      }
      catch (Exception e)
      {
        executeCommand(cmd,strval,TAccess.CA_WRITE);
      }
      return;
    }
    if (cmd.compareToIgnoreCase("which") == 0)
    {
      if (!st.hasMoreTokens())
      {
        TLinkFactory.dbgPrint("command incomplete!");
        return;
      }
      String strval = st.nextToken();
      try
      {
        executeCommand(cmd,strval,TAccess.CA_READ);
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
      return;
    }
    if (cmd.compareToIgnoreCase("flush") == 0)
    {
      String strval = "contracts";
      if (st.hasMoreTokens())
      {
        strval = st.nextToken();
      }
      try
      {
        executeCommand(cmd,strval,TAccess.CA_READ);
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
      return;
    }
    executeCommand(cmd,TAccess.CA_READ);
  }
  private static boolean isExecuting = false;
  public static boolean isExecuting() { return isExecuting; }
  public static void executeCommand(String cmd,short access)
  {
    if (!initialized) initialize();
    int ival = 0;
    String sval = null;
    int idxo = cmd.indexOf("(");
    int idxc = cmd.indexOf(")");
    boolean intarg = false;
    boolean strarg = false;
    if (idxo > 0 && idxc > 0)
    {
      String arg = cmd.substring(idxo+1,idxc);
      cmd = cmd.substring(0,idxo);
      try
      {
        ival = Integer.parseInt(arg);
        intarg = true;
      } catch (Exception ignore) {};
      if (!intarg)
      {
        strarg = true;
        sval = arg;
      }
    }
   
    TCommandItem ci = cmdLst.get(cmd);
    if (ci == null)
    {
      TLinkFactory.dbgPrint("command "+cmd+" not found in command list");
      return;
    }
    isExecuting = true;
    if (intarg)
      ci.invoke(ival,access);
    else if (strarg)
      ci.invoke(sval,access);
    else
      ci.invoke(access);
   
    isExecuting = false;
  }
  public static void executeCommand(String cmd,int value,short access)
  {
    if (!initialized) initialize();
    TCommandItem ci = cmdLst.get(cmd);
    if (ci == null)
    {
      System.out.println("command "+cmd+" not found in command list");
      return;
    }
    isExecuting = true;
    ci.invoke(value,access);
    isExecuting = false;
  }
  public static void executeCommand(String cmd,String value,short access)
  {
    if (!initialized) initialize();
    TCommandItem ci = cmdLst.get(cmd);
    if (ci == null)
    {
      System.out.println("command "+cmd+" not found in command list");
      return;
    }
    isExecuting = true;
    ci.invoke(value,access);
    isExecuting = false;
  }
}
TOP

Related Classes of de.desy.tine.console.TCommandList

TOP
Copyright © 2018 www.massapi.com. 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.