zoukankan      html  css  js  c++  java
  • 在mac守护进程中启动一个新进程

    以websocketpp的example为基础,
     
    #include <websocketpp/config/asio_no_tls.hpp>
    #include <websocketpp/server.hpp>
    #include <iostream>
    #include <stdio.h>
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/types.h>
    #import <Foundation/Foundation.h>
     
    typedef websocketpp::server<websocketpp::config::asio> server;
     
    using websocketpp::lib::placeholders::_1;
    using websocketpp::lib::placeholders::_2;
    using websocketpp::lib::bind;
    typedef server::message_ptr message_ptr;
    /*唤起指定的应用程序启动*/
    void launchAppliction(char* appPath)
    {
    //appPath指向可执行文件的绝对地址
    NSTask *softTask = [[NSTask alloc]init];
    NSString * strPath = [NSString stringWithUTF8String:appPath];
    [softTask setLaunchPath:strPath];
    // [softTask setArguments:[NSArray arrayWithObjects:@"hello world", "2016", nil]];//设置运行参数
    [softTask launch];
    }
     
    /*创建进程,启动应用程序*/
    void make_process()
    {
    char app[] = "/Users/liuxuetao/work/sampleApp.app/Contents/MacOS/sampleApp";
    launchAppliction(app);
    }
     
     
    // Define a callback to handle incoming messages
    void on_message(server* s, websocketpp::connection_hdl hdl, message_ptr msg) {
    std::cout << "on_message called with hdl: " << hdl.lock().get()
    << " and message: " << msg->get_payload()
    << std::endl;
     
    // check for a special command to instruct the server to stop listening so
    // it can be cleanly exited.
    if (msg->get_payload() == "stop-listening") {
    s->stop_listening();
    return;
    }
    std::string a_str = msg->get_payload();
    std::string b_str = "startprocess";
    int ret = strcmp(a_str.c_str(),b_str.c_str());
    if ( ret == 0 ) {
    make_process();
    return;
    }
     
    try {
    s->send(hdl, msg->get_payload(), msg->get_opcode());
    } catch (const websocketpp::lib::error_code& e) {
    std::cout << "Echo failed because: " << e
    << "(" << e.message() << ")" << std::endl;
    }
    }
     
    int main() {
    // Create a server endpoint
    server echo_server;
     
    try {
    // Set logging settings
    echo_server.set_access_channels(websocketpp::log::alevel::all);
    echo_server.clear_access_channels(websocketpp::log::alevel::frame_payload);
     
    // Initialize Asio
    echo_server.init_asio();
     
    // Register our message handler
    echo_server.set_message_handler(bind(&on_message,&echo_server,::_1,::_2));
     
    // Listen on port 9002
    echo_server.listen(9060);
     
    // Start the server accept loop
    echo_server.start_accept();
     
    // Start the ASIO io_service run loop
    echo_server.run();
    } catch (websocketpp::exception const & e) {
    std::cout << e.what() << std::endl;
    } catch (...) {
    std::cout << "other exception" << std::endl;
    }
    return 0;
    }
    当收到了远程发来的指令startprocess后,守护进程会调用make_process()接口创建一个进程,启动指定的应用程序。

    本博客所有内容均为原创,转载请说明出处。欢迎音视频多媒体领域的朋友来人来函交流心得。
  • 相关阅读:
    HZNU 2019 Summer training 6
    HZNU 2019 Summer training 5
    HZNU 2019 Summer training 4
    Garlands CodeForces
    HZNU 2019 Summer training 3
    UVA
    HZNU 2019 Summer training 2
    Serge and Dining Room(线段树)
    HZNU 2019 Summer training 1
    【7-10 PAT】树的遍历
  • 原文地址:https://www.cnblogs.com/liuxt/p/8065033.html
Copyright © 2011-2022 走看看