Implemented setvalue and getvalue commands
This commit is contained in:
+118
-24
@@ -9,16 +9,24 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <string.h>
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#define SOCK_PATH "/tmp/growcontrol.socket"
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
void printUsage(std::ostream& stream, std::string name) {
|
||||||
{
|
stream << "Usage: " << name << " [options] GETVAL|SETVAL ..." << std::endl
|
||||||
|
<< "\tOptions:" << std::endl
|
||||||
|
<< "\t\t-s ... path of ipc socket" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string sendCommand(std::string socketPath, std::string command) {
|
||||||
|
std::string retval;
|
||||||
int s, t, len;
|
int s, t, len;
|
||||||
struct sockaddr_un remote;
|
struct sockaddr_un remote;
|
||||||
char str[100];
|
char str[100];
|
||||||
@@ -28,35 +36,121 @@ int main(int argc, char **argv)
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Trying to connect...\n");
|
|
||||||
|
|
||||||
remote.sun_family = AF_UNIX;
|
remote.sun_family = AF_UNIX;
|
||||||
strcpy(remote.sun_path, SOCK_PATH);
|
strncpy(remote.sun_path, socketPath.c_str(), sizeof(remote.sun_path));
|
||||||
len = strlen(remote.sun_path) + sizeof(remote.sun_family);
|
len = sizeof(remote);
|
||||||
if (connect(s, (struct sockaddr *)&remote, len) == -1) {
|
if (connect(s, (struct sockaddr *)&remote, len) == -1) {
|
||||||
perror("connect");
|
perror("connect");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Connected.\n");
|
if (send(s, command.c_str(), command.size() + 1, 0) == -1) {
|
||||||
|
perror("send");
|
||||||
while(printf("> "), fgets(str, 100, stdin), !feof(stdin)) {
|
exit(1);
|
||||||
if (send(s, str, strlen(str)+1, 0) == -1) {
|
|
||||||
perror("send");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((t=recv(s, str, 100, 0)) > 0) {
|
|
||||||
str[t] = '\0';
|
|
||||||
printf("echo> %s", str);
|
|
||||||
} else {
|
|
||||||
if (t < 0) perror("recv");
|
|
||||||
else printf("Server closed connection\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ((t=recv(s, str, 100, 0)) > 0) {
|
||||||
|
if (strlen(str) != (unsigned)t-1) {
|
||||||
|
std::cerr << "ERROR: Invalid null-termination (" << strlen(str) << ", " << t << ")" << std::endl;
|
||||||
|
exit(1);
|
||||||
|
} else {
|
||||||
|
retval = str;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (t < 0) perror("recv");
|
||||||
|
else std::cerr << "Server closed connection" << std::endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
close(s);
|
close(s);
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
int command = -1; // 0 .. GETVAL, 1 .. SETVAL
|
||||||
|
std::string socketPath = "/opt/growcontrol/growcontrold.socket";
|
||||||
|
int commandPos = -1;
|
||||||
|
|
||||||
|
for (int t = 1; t < argc; t++) {
|
||||||
|
if (strcmp(argv[t], "-s") == 0) {
|
||||||
|
if (t+1 < argc && argv[t+1][0] != '-') {
|
||||||
|
t++;
|
||||||
|
socketPath = argv[t];
|
||||||
|
} else {
|
||||||
|
std::cerr << "Option -s is missing an argument." << std::endl << std::endl;
|
||||||
|
printUsage(std::cerr, argv[0]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else if (strcmp(argv[t], "--help") == 0) {
|
||||||
|
printUsage(std::cerr, argv[0]);
|
||||||
|
exit(0);
|
||||||
|
} else if (strcmp(argv[t], "GETVAL") == 0) {
|
||||||
|
command = 0;
|
||||||
|
commandPos = t;
|
||||||
|
break;
|
||||||
|
} else if (strcmp(argv[t], "SETVAL") == 0) {
|
||||||
|
command = 1;
|
||||||
|
commandPos = t;
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
std::cerr << "ERROR: Unknown argument \"" << argv[t] << "\"." << std::endl << std::endl;
|
||||||
|
printUsage(std::cerr, argv[0]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (command == 0) { // GETVAL
|
||||||
|
std:: ostringstream ss;
|
||||||
|
unsigned argcount = 0;
|
||||||
|
ss << "GETVAL";
|
||||||
|
for (int t = commandPos+1; t < argc; t++) {
|
||||||
|
ss << " " << argv[t];
|
||||||
|
argcount++;
|
||||||
|
}
|
||||||
|
if (argcount == 0) {
|
||||||
|
std::cerr << "ERROR: No arguments for GETVAL given." << std::endl << std::endl;
|
||||||
|
exit(1);
|
||||||
|
} else {
|
||||||
|
std::string resp = sendCommand(socketPath, ss.str());
|
||||||
|
std::istringstream commandStream(resp);
|
||||||
|
std::string firstPart;
|
||||||
|
std::getline(commandStream, firstPart, ' ');
|
||||||
|
if (firstPart.compare("ACK") == 0) {
|
||||||
|
std::cout << resp.substr(4) << std::endl;
|
||||||
|
} else {
|
||||||
|
std::cerr << resp << std::endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (command == 1) { // SETVAL
|
||||||
|
std:: ostringstream ss;
|
||||||
|
unsigned argcount = 0;
|
||||||
|
ss << "SETVAL";
|
||||||
|
for (int t = commandPos+1; t < argc; t++) {
|
||||||
|
ss << " " << argv[t];
|
||||||
|
argcount++;
|
||||||
|
}
|
||||||
|
if (argcount == 0) {
|
||||||
|
std::cerr << "ERROR: No arguments for SETVAL given." << std::endl << std::endl;
|
||||||
|
exit(1);
|
||||||
|
} else if (argcount == 0) {
|
||||||
|
std::cerr << "ERROR: Too many arguments for SETVAL given." << std::endl << std::endl;
|
||||||
|
exit(1);
|
||||||
|
} else {
|
||||||
|
std::string resp = sendCommand(socketPath, ss.str());
|
||||||
|
std::istringstream commandStream(resp);
|
||||||
|
std::string firstPart;
|
||||||
|
std::getline(commandStream, firstPart, ' ');
|
||||||
|
if (firstPart.compare("ACK") == 0) {
|
||||||
|
} else {
|
||||||
|
std::cerr << resp << std::endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
std::cerr << "ERROR: No command given." << std::endl << std::endl;
|
||||||
|
printUsage(std::cerr, argv[0]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user