File indexing completed on 2023-05-30 09:03:12
0001 /* 0002 SPDX-License-Identifier: GPL-2.0-or-later 0003 SPDX-FileCopyrightText: 2015 Minh Ngo <minh@fedoraproject.org> 0004 */ 0005 0006 #include <iostream> 0007 #include <csignal> 0008 #include <vector> 0009 #include <cstring> 0010 0011 #include "pythonserver.h" 0012 0013 using namespace std; 0014 0015 const char messageEnd = 29; 0016 const char recordSep = 30; 0017 const char unitSep = 31; 0018 0019 PythonServer server; 0020 bool isInterrupted = false; 0021 0022 string LOGIN("login"); 0023 string EXIT("exit"); 0024 string CODE("code"); 0025 string FILEPATH("setFilePath"); 0026 string MODEL("model"); 0027 0028 void signal_handler(int signal) 0029 { 0030 if (signal == SIGINT) 0031 { 0032 isInterrupted = true; 0033 server.interrupt(); 0034 } 0035 } 0036 0037 vector<string> split(string s, char delimiter) 0038 { 0039 vector<string> results; 0040 0041 size_t pos = 0; 0042 std::string token; 0043 while ((pos = s.find(delimiter)) != std::string::npos) { 0044 token = s.substr(0, pos); 0045 results.push_back(token); 0046 s.erase(0, pos + 1); 0047 } 0048 results.push_back(s); 0049 0050 return results; 0051 } 0052 0053 int main() 0054 { 0055 std::signal(SIGINT, signal_handler); 0056 0057 std::cout << "ready" << std::endl; 0058 0059 std::string input; 0060 while (getline(std::cin, input, messageEnd)) 0061 { 0062 const vector<string>& records = split(input, recordSep); 0063 0064 if (records.size() == 2) 0065 { 0066 if (records[0] == EXIT) 0067 { 0068 //Exit from cycle and finish program 0069 break; 0070 } 0071 else if (records[0] == LOGIN) 0072 { 0073 server.login(); 0074 } 0075 if (records[0] == FILEPATH) 0076 { 0077 const vector<string>& args = split(records[1], unitSep); 0078 if (args.size() == 2) 0079 server.setFilePath(args[0], args[1]); 0080 } 0081 else if (records[0] == CODE) 0082 { 0083 server.runPythonCommand(records[1]); 0084 0085 if (!isInterrupted) 0086 { 0087 const string& result = 0088 server.getOutput() 0089 + unitSep 0090 + server.getError() 0091 + unitSep 0092 + to_string((int)server.isError()) 0093 + messageEnd; 0094 0095 std::cout << result.c_str(); 0096 } 0097 else 0098 { 0099 // No replay when interrupted 0100 isInterrupted = false; 0101 } 0102 } 0103 else if (records[0] == MODEL) 0104 { 0105 bool ok, val; 0106 try { 0107 val = (bool)stoi(records[1]); 0108 ok = true; 0109 } catch (const std::invalid_argument &e) { 0110 ok = false; 0111 }; 0112 0113 string result; 0114 if (ok) 0115 result = server.variables(val) + unitSep; 0116 else 0117 result = unitSep + string("Invalid argument for 'model' command"); 0118 result += messageEnd; 0119 0120 std::cout << result.c_str(); 0121 } 0122 std::cout.flush(); 0123 } 0124 } 0125 0126 return 0; 0127 }