Example #1
0
/*
 * Initializes the RW.CLI controller.
 */
static void controller_init(rwcli_controller_t* inst)
{
  memset(inst, 0, sizeof(rwcli_controller_t));

  // Initialize the trace module and set appropriate severity
  controller_init_trace(inst);

  // Allocate the receive buffer
  inst->recv_buf = (uint8_t*)calloc(sizeof(uint8_t), RW_CLI_MAX_BUF);
  inst->recv_buf_len = RW_CLI_MAX_BUF;

  if (!getenv("RIFT_VAR_ROOT")) {
    if (rift_cmdargs.use_rift_var_root == -1) {
      char *rift_var_root = NULL;
      asprintf(&rift_var_root, "%s/var/rift", getenv("RIFT_INSTALL"));
      setenv("RIFT_VAR_ROOT", rift_var_root, true);
      free(rift_var_root);
    } else {
      setenv("RIFT_VAR_ROOT", rift_cmdargs.rift_var_root, true);
    }
  }

  RWTRACE_INFO(inst->trace_ctxt, RWTRACE_CATEGORY_RWCLI,
          "RIFT_VAR_ROOT=%s", getenv("RIFT_VAR_ROOT"));

  // Decide the RW.CLI communication mode with RW.MgmtAgent
  if (rift_cmdargs.use_netconf == -1) {
    const char* agent_mode = rw_getenv("RWMGMT_AGENT_MODE");
    if (agent_mode && strcmp(agent_mode, "XML") == 0) {
      // If the agent mode is XML only RW.MSG mode is possible. Override the
      // command line option for netconf and opt for RW.MSG mode.
      inst->agent_type = RWCLI_TRANSPORT_MODE_RWMSG;
    } else {
      // Transport mode not chosen in command line and not XML mode. Choose
      // Netconf mode by default.
      inst->agent_type = RWCLI_TRANSPORT_MODE_NETCONF;
    }
  } else if (rift_cmdargs.use_netconf) {
    inst->agent_type = RWCLI_TRANSPORT_MODE_NETCONF;
  } else {
    inst->agent_type = RWCLI_TRANSPORT_MODE_RWMSG;
  }

  inst->is_netconf_agent_loaded = false;

  // To support background processes and job control, there should
  // be multiple channels available

  // Create two agent channels one for RWMSG and the other for Netconf
  controller_agent_channel_init(inst, RWCLI_TRANSPORT_MODE_RWMSG);
  controller_agent_channel_init(inst, RWCLI_TRANSPORT_MODE_NETCONF);
}
Example #2
0
int
boot_(Module m)
{
  char* dummy = NULL;

  // Initialize the trace module and set appropriate severity
  rift_init_trace();

  recv_buf = (uint8_t*)calloc(sizeof(uint8_t), RW_CLI_MAX_BUF);
  recv_buf_len = RW_CLI_MAX_BUF;
  
  // Decide the RW.CLI communication mode with RW.MgmtAgent
  if (rift_cmdargs.use_netconf == -1) {
    const char* agent_mode = rw_getenv("RWMGMT_AGENT_MODE");
    if (agent_mode && strcmp(agent_mode, "XML") == 0) {
      // If the agent mode is XML only RW.MSG mode is possible. Override the
      // command line option for netconf and opt for RW.MSG mode.
      rift_cmdargs.use_netconf = 0;
    } else {
      // Transport mode not chosen in command line and not XML mode. Choose
      // Netconf mode by default.
      rift_cmdargs.use_netconf = 1;
    }
  }

  // Authenticate in case of RW.MSG mode
  rwcli_transport_mode_t transport_mode =  RWCLI_TRANSPORT_MODE_NETCONF;
  rwcli_user_mode_t user_mode = RWCLI_USER_MODE_NONE;
  if (!rift_cmdargs.use_netconf) {
    user_mode = rift_authenticate(&rift_cmdargs);
    if (user_mode == RWCLI_USER_MODE_INVALID) {
      printf("\nERROR: Invalid username/password\n");
      exit(-1);
    }
    transport_mode = RWCLI_TRANSPORT_MODE_RWMSG;
  }

  rwcli_zsh_plugin_init(transport_mode, user_mode);
  rwcli_set_messaging_hook(messaging_hook);
  rwcli_set_history_hook(history_hook);

  // To support background processes and job control, there should
  // be multiple channels available
  msg_channel_init(&rwcli_agent_ch);
  msg_channel_create(&rwcli_agent_ch);

  // Always load the rwmsg_agent module, If netconf is enabled then 
  // this module will only receive logging notifications
  if (load_module("zsh/rwmsg_agent", NULL, 0) != 0) {
    printf("\nCRITICAL: Loading the messaging agent module failed\n");
    fflush(stdout);
    return -1;
  }

  // Load the agent that is required
  if (rift_cmdargs.use_netconf) {
    if (load_module("zsh/rwnetconf_agent", NULL, 0) != 0) {
      printf("\nCRITICAL: Loading the netconf agent module failed\n");
      fflush(stdout);
      return -1;
    }
  }
  /* Register the completion widget */
  w_comp = addzlefunction("rift-complete", rift_complete, 0);
  Keymap km = openkeymap("main");
  t_orig_tab_bind = keybind(km, "\t", &dummy);
  bindkey(km, "\t", refthingy(w_comp->first), NULL);

  /* Bind ? to generate help */
  w_gen_help = addzlefunction("rift-gen-help", rift_generate_help, 0);
  bindkey(km, "?", refthingy(w_gen_help->first), NULL);

  /* Set the lookup hook */
  rw_lookup_fn = rift_lookup;

  /* Set the prompt hook */
  addprepromptfn(rift_prompt);

  return 0;
}