|
| 1 | +/* |
| 2 | + * Client.cpp |
| 3 | + * |
| 4 | + * Created on: Sep 25, 2014 |
| 5 | + * Author: wangli |
| 6 | + */ |
| 7 | +#include <stdlib.h> |
| 8 | +#include <unistd.h> |
| 9 | +#include <stdio.h> |
| 10 | +#include <fstream> |
| 11 | +#include <string> |
| 12 | +#include "./startup.h" |
| 13 | +#include "Client/ClientResponse.h" |
| 14 | +#include "Client/Client.h" |
| 15 | +#define GLOG_NO_ABBREVIATED_SEVERITIES |
| 16 | +#include "common/log/logging.h" |
| 17 | +#include "common/Block/ResultSet.h" |
| 18 | +#include "utility/command_line.h" |
| 19 | +#include "utility/rdtsc.h" |
| 20 | + |
| 21 | +void readStrigFromTerminal(string& input) { |
| 22 | + while (true) { |
| 23 | + std::cin.clear(); |
| 24 | + std::cin.sync(); |
| 25 | + std::string str; |
| 26 | + if (getline(std::cin, str)) { |
| 27 | + bool finish = false; |
| 28 | + for (unsigned i = 0; i < str.length(); i++) { |
| 29 | + if (str[i] == ';') { |
| 30 | + input += str.substr(0, i + 1); |
| 31 | + finish = true; |
| 32 | + break; |
| 33 | + } |
| 34 | + } |
| 35 | + if (finish) break; |
| 36 | + input += str + " "; |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +void submit_command(Client& client, std::string& command) { |
| 42 | + ResultSet rs; |
| 43 | + std::string message; |
| 44 | + switch (client.submit(command, message, rs)) { |
| 45 | + case Client::result: |
| 46 | + rs.print(); |
| 47 | + // if(i!=0) |
| 48 | + // total_time+=rs.query_time_; |
| 49 | + break; |
| 50 | + case Client::message: |
| 51 | + printf("%s", message.c_str()); |
| 52 | + break; |
| 53 | + case Client::error: |
| 54 | + printf( |
| 55 | + "\e[0;31m" |
| 56 | + "%s\033[0m\n", |
| 57 | + message.c_str()); |
| 58 | + break; |
| 59 | + default: |
| 60 | + assert(false); |
| 61 | + break; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +void submit_command_repeated(Client& client, std::string& command, |
| 66 | + int repeated) { |
| 67 | + double total_time = 0; |
| 68 | + for (int i = 0; i < repeated; i++) { |
| 69 | + ResultSet rs; |
| 70 | + std::string message; |
| 71 | + switch (client.submit(command, message, rs)) { |
| 72 | + case Client::result: |
| 73 | + if (i != 0) total_time += rs.query_time_; |
| 74 | + break; |
| 75 | + case Client::message: |
| 76 | + printf("%s", message.c_str()); |
| 77 | + break; |
| 78 | + case Client::error: |
| 79 | + printf( |
| 80 | + "\e[0;31m" |
| 81 | + "%s\033[0m\n", |
| 82 | + message.c_str()); |
| 83 | + break; |
| 84 | + default: |
| 85 | + assert(false); |
| 86 | + break; |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | +void PrintUsage() { |
| 91 | + cout << "Welcome to use Ginkgo. " << endl; |
| 92 | + cout << "Type: " << endl; |
| 93 | + cout << "\t help; for usage of Ginkgo." << endl; |
| 94 | + cout << "\t copyright; for distribution terms." << endl; |
| 95 | + cout << "\t exit; or shutdown; for exiting Ginkgo." << endl; |
| 96 | +} |
| 97 | + |
| 98 | +void PrintCopyright() { |
| 99 | + cout << "Copyright [2012-2015] DaSE@ECNU " << endl |
| 100 | + << " Licensed to the Apache Software Foundation (ASF) under one or more" |
| 101 | + << " contributor license agreements. See the NOTICE file distributed " |
| 102 | + "with" |
| 103 | + << " this work for additional information regarding copyright ownership." |
| 104 | + << " The ASF licenses this file to You under the Apache License, " |
| 105 | + "Version 2.0" |
| 106 | + << " (the \" License\"); you may not use this file except in compliance " |
| 107 | + "with" |
| 108 | + << " the License. You may obtain a copy of the License at" << endl |
| 109 | + << " http://www.apache.org/licenses/LICENSE-2.0" << endl |
| 110 | + << " Unless required by applicable law or agreed to in writing, software" |
| 111 | + << " distributed under the License is distributed on an \" AS IS \" " |
| 112 | + "BASIS," |
| 113 | + << " WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or " |
| 114 | + "implied." |
| 115 | + << " See the License for the specific language governing permissions and" |
| 116 | + << " limitations under the License." << endl; |
| 117 | +} |
| 118 | +int main(int argc, char** argv) { |
| 119 | + /* Client */ |
| 120 | + |
| 121 | + if (argc != 3) { |
| 122 | + printf( |
| 123 | + "argc=%d, Illegal input. \nPlease use client master_ip " |
| 124 | + "client_listener_port.\n", |
| 125 | + argc); |
| 126 | + printf( |
| 127 | + "HINT: the master ip and the client_listener_port can be found in the " |
| 128 | + "configure file.\n"); |
| 129 | + return 0; |
| 130 | + } |
| 131 | + |
| 132 | + ginkgo::common::Logging claims_logging(argv[0]); |
| 133 | + print_welcome(); |
| 134 | + PrintUsage(); |
| 135 | + |
| 136 | + Client client; |
| 137 | + client.connection(argv[1], atoi(argv[2])); |
| 138 | + std::cout << std::endl; |
| 139 | + init_command_line(); |
| 140 | + |
| 141 | + while (1) { |
| 142 | + std::string command, message; |
| 143 | + |
| 144 | + get_one_command(command); |
| 145 | + |
| 146 | + command = trimSpecialCharactor(command); |
| 147 | + |
| 148 | + if (command == "exit;" || command == "shutdown;") { |
| 149 | + break; |
| 150 | + } else if (command.empty()) { |
| 151 | + continue; |
| 152 | + } else if (command == "help;") { |
| 153 | + PrintUsage(); |
| 154 | + continue; |
| 155 | + } else if (command == "copyright;") { |
| 156 | + PrintCopyright(); |
| 157 | + continue; |
| 158 | + } |
| 159 | + |
| 160 | + submit_command(client, command); |
| 161 | + |
| 162 | + /* |
| 163 | + * the following command execute the query for a given time and p |
| 164 | + * rint the averaged query response time.*/ |
| 165 | + // submit_command_repeated(client,command,50); |
| 166 | + } |
| 167 | + client.shutdown(); |
| 168 | +} |
0 commit comments