diff options
author | Christophe Fergeau <cfergeau@redhat.com> | 2012-10-02 22:01:45 +0200 |
---|---|---|
committer | Christophe Fergeau <cfergeau@redhat.com> | 2012-10-02 23:10:06 +0200 |
commit | 60c53563604bbf33096f968f1058c8d5f3750d6d (patch) | |
tree | 45e3ee1b0ecd8a638fc31c83b579b6787cd57acf | |
parent | 3aa68d49c44a7e879138dfdb441c1e135009fb05 (diff) |
Split SPICEConsoleAPI::connect
Split it into several smaller methods. This makes the code
more readable, and should also make it easier to port to other
platforms.
-rw-r--r-- | SPICEConsoleAPI.cpp | 217 | ||||
-rw-r--r-- | SPICEConsoleAPI.h | 5 |
2 files changed, 126 insertions, 96 deletions
diff --git a/SPICEConsoleAPI.cpp b/SPICEConsoleAPI.cpp index 406a5ab..f2d6609 100644 --- a/SPICEConsoleAPI.cpp +++ b/SPICEConsoleAPI.cpp @@ -78,42 +78,26 @@ SPICEConsolePtr SPICEConsoleAPI::getPlugin() // Methods -void SPICEConsoleAPI::connect() +bool SPICEConsoleAPI::initSocket(void) { - int port = 0; - int sport = 0; - try { - port = getAttribute("port").cast<int>(); - sport = getAttribute("SecurePort").cast<int>(); - } catch (FB::bad_variant_cast &e) { - g_warning("invalid port/SecurePort value"); - } - - if (port < 0) - g_warning("invalid port: '%d'", port); - if (sport < 0) - g_warning("invalid secure port: '%d'", sport); - - if (port <= 0 && sport <= 0) - { - m_connected_status = 1; - fire_disconnected(m_connected_status); - return; - } - std::string socket_file(m_tmp_dir); socket_file += "/spice-xpi"; if (setenv("SPICE_XPI_SOCKET", socket_file.c_str(), 1)) { - g_critical("could not set SPICE_XPI_SOCKET env variable"); - return; + return false; } + m_external_controller.SetFilename(socket_file); + + return true; +} +bool SPICEConsoleAPI::startClient(void) +{ /* use a pipe for the children to wait until it gets tracked */ int pipe_fds[2] = { -1, -1 }; if (pipe(pipe_fds) < 0) { perror("spice-xpi system error"); - return; + return false; } m_pid_controller = fork(); @@ -154,85 +138,128 @@ void SPICEConsoleAPI::connect() close(pipe_fds[1]); pipe_fds[1] = -1; - m_external_controller.SetFilename(socket_file); - - if (m_external_controller.Connect(10) != 0) - { - g_critical("could not connect to spice client controller"); - return; - } - - // create trust store filename - FILE *fp; - int fd = -1; - char trust_store_template[] = "/tmp/truststore.pem-XXXXXX"; - mode_t prev_umask = umask(0177); - fd = mkstemp(trust_store_template); - umask(prev_umask); - m_trust_store_file = trust_store_template; + return true; + } +} - if (fd != -1) +bool SPICEConsoleAPI::createTrustStore(void) +{ + // create trust store filename + FILE *fp; + int fd = -1; + char trust_store_template[] = "/tmp/truststore.pem-XXXXXX"; + mode_t prev_umask = umask(0177); + fd = mkstemp(trust_store_template); + umask(prev_umask); + m_trust_store_file = trust_store_template; + + if (fd != -1) + { + fp = fdopen(fd,"w+"); + if (fp != NULL) { - fp = fdopen(fd,"w+"); - if (fp != NULL) - { - std::string trust_store; - try { - trust_store = getAttribute("TrustStore").cast<std::string>(); - } catch (FB::bad_variant_cast &e) { - g_warning("invalid trust store value"); - } - fputs(trust_store.c_str(), fp); - fflush(fp); - fsync(fd); - fclose(fp); - } - else - { - g_critical("could not open truststore temp file"); - close(fd); - unlink(m_trust_store_file.c_str()); - m_trust_store_file.clear(); - return; + std::string trust_store; + try { + trust_store = getAttribute("TrustStore").cast<std::string>(); + } catch (FB::bad_variant_cast &e) { + g_warning("invalid trust store value"); } + fputs(trust_store.c_str(), fp); + fflush(fp); + fsync(fd); + fclose(fp); } else { - g_critical("could not create truststore temp file: %s", g_strerror(errno)); - return; + g_critical("could not open truststore temp file"); + close(fd); + unlink(m_trust_store_file.c_str()); + m_trust_store_file.clear(); + return false; } + } + else + { + g_critical("could not create truststore temp file: %s", g_strerror(errno)); + return false; + } - SendInit(); - - SendValue<std::string>(CONTROLLER_HOST, "hostIP"); - if (port > 0) - SendMsg(CONTROLLER_PORT, (uint32_t)port); - if (sport > 0) - SendMsg(CONTROLLER_SPORT, (uint32_t)sport); - /* - SendValue(CONTROLLER_FULL_SCREEN, - (m_fullscreen == PR_TRUE ? CONTROLLER_SET_FULL_SCREEN : 0) | - (m_admin_console == PR_FALSE ? CONTROLLER_AUTO_DISPLAY_RES : 0)); - */ - SendValue<bool>(CONTROLLER_ENABLE_SMARTCARD, "Smartcard"); - SendValue<std::string>(CONTROLLER_PASSWORD, "Password"); - SendValue<std::string>(CONTROLLER_TLS_CIPHERS, "CipherSuite"); - SendValue<std::string>(CONTROLLER_SET_TITLE, "Title"); - SendValue<bool>(CONTROLLER_SEND_CAD, "SendCtrlAltDelete"); - SendValue<bool>(CONTROLLER_ENABLE_USB_AUTOSHARE, "UsbAutoShare"); - SendMsg(CONTROLLER_USB_FILTER, m_usb_filter); - SendMsg(CONTROLLER_SECURE_CHANNELS, m_SSLChannels); - SendMsg(CONTROLLER_CA_FILE, m_trust_store_file); - SendValue<std::string>(CONTROLLER_HOST_SUBJECT, "HostSubject"); - SendValue<std::string>(CONTROLLER_HOTKEYS, "HotKey"); - //SendValue(CONTROLLER_COLOR_DEPTH, atoi(m_color_depth.c_str())); - SendValue<std::string>(CONTROLLER_DISABLE_EFFECTS, "DisableEffects"); - SendMsg(CONTROLLER_CONNECT); - SendMsg(CONTROLLER_SHOW); - - // set connected status - m_connected_status = -1; + return true; +} + +void SPICEConsoleAPI::connect() +{ + int port = 0; + int sport = 0; + try { + port = getAttribute("port").cast<int>(); + sport = getAttribute("SecurePort").cast<int>(); + } catch (FB::bad_variant_cast &e) { + g_warning("invalid port/SecurePort value"); + } + + if (port < 0) + g_warning("invalid port: '%d'", port); + if (sport < 0) + g_warning("invalid secure port: '%d'", sport); + + if (port <= 0 && sport <= 0) + { + m_connected_status = 1; + fire_disconnected(m_connected_status); + return; + } + + if (!this->initSocket()) { + g_critical("could not initialize controller socket"); + return; + } + + if (!this->startClient()) { + g_critical("failed to start SPICE client"); + return; + } + + if (m_external_controller.Connect(10) != 0) { + g_critical("could not connect to spice client controller"); + return; + } + + if (!this->createTrustStore()) { + g_critical("could not create trust store"); + return; } + + SendInit(); + + SendValue<std::string>(CONTROLLER_HOST, "hostIP"); + if (port > 0) + SendMsg(CONTROLLER_PORT, (uint32_t)port); + if (sport > 0) + SendMsg(CONTROLLER_SPORT, (uint32_t)sport); + /* + SendValue(CONTROLLER_FULL_SCREEN, + (m_fullscreen == PR_TRUE ? CONTROLLER_SET_FULL_SCREEN : 0) | + (m_admin_console == PR_FALSE ? CONTROLLER_AUTO_DISPLAY_RES : 0)); + */ + SendValue<bool>(CONTROLLER_ENABLE_SMARTCARD, "Smartcard"); + SendValue<std::string>(CONTROLLER_PASSWORD, "Password"); + SendValue<std::string>(CONTROLLER_TLS_CIPHERS, "CipherSuite"); + SendValue<std::string>(CONTROLLER_SET_TITLE, "Title"); + SendValue<bool>(CONTROLLER_SEND_CAD, "SendCtrlAltDelete"); + SendValue<bool>(CONTROLLER_ENABLE_USB_AUTOSHARE, "UsbAutoShare"); + SendMsg(CONTROLLER_USB_FILTER, m_usb_filter); + SendMsg(CONTROLLER_SECURE_CHANNELS, m_SSLChannels); + SendMsg(CONTROLLER_CA_FILE, m_trust_store_file); + SendValue<std::string>(CONTROLLER_HOST_SUBJECT, "HostSubject"); + SendValue<std::string>(CONTROLLER_HOTKEYS, "HotKey"); + //SendValue(CONTROLLER_COLOR_DEPTH, atoi(m_color_depth.c_str())); + SendValue<std::string>(CONTROLLER_DISABLE_EFFECTS, "DisableEffects"); + SendMsg(CONTROLLER_CONNECT); + SendMsg(CONTROLLER_SHOW); + + // set connected status + m_connected_status = -1; } void SPICEConsoleAPI::show() diff --git a/SPICEConsoleAPI.h b/SPICEConsoleAPI.h index eb7001e..efb2e86 100644 --- a/SPICEConsoleAPI.h +++ b/SPICEConsoleAPI.h @@ -156,8 +156,11 @@ private: void SendMsg(uint32_t id, uint32_t value); template<typename T> void SendValue(uint32_t id, std::string attributeName); - static void *ControllerWaitHelper(void *opaque); + + bool initSocket(void); + bool startClient(void); + bool createTrustStore(void); }; #endif // H_SPICEConsoleAPI |