1 //
2 // Copyright (C) 2015 Google, Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at:
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "base.h"
18 #include <rapidjson/document.h>
19 #include <rapidjson/writer.h>
20 #include <rapidjson/stringbuffer.h>
21 #include "utils/command_receiver.h"
22
23 #include <arpa/inet.h>
24 #include <errno.h>
25 #include <iostream>
26 #include <netinet/in.h>
27 #include <pthread.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <unistd.h>
33
34 const int kBacklogInt = 10;
35 #define PORT 8080
36 // TODO: Set to a lower buffer size and read socket data until termination
37 #define SOCK_BUF_LEN 4096
38 #define MEMSET_VALUE 0
39
40 int client_sock;
41 int socket_desc;
42
SockTest()43 void SockTest() {
44 char str[SOCK_BUF_LEN];
45 int listen_fd, comm_fd, c;
46 struct sockaddr_in servaddr, client;
47 rapidjson::Document d;
48 rapidjson::StringBuffer buffer;
49 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
50 CommandReceiver cr;
51
52 listen_fd = socket(AF_INET, SOCK_STREAM, 0);
53 memset (&servaddr, MEMSET_VALUE, sizeof(servaddr));
54 servaddr.sin_family = AF_INET;
55 servaddr.sin_addr.s_addr = INADDR_ANY;
56 servaddr.sin_port = htons(PORT);
57
58 int bind_result = bind(
59 listen_fd, (struct sockaddr *) &servaddr, sizeof(servaddr));
60 if (bind_result != 0) {
61 LOG(ERROR) << sl4n::kTagStr <<
62 ": Failed to assign the address to the socket."
63 << " Error: " << strerror(errno) << ", " << errno;
64 exit(1);
65 }
66
67 int listen_result = listen(listen_fd, kBacklogInt);
68 if (listen_result != 0) {
69 LOG(ERROR) << sl4n::kTagStr << ": Failed to setup the passive socket."
70 << " Error: " << strerror(errno) << ", " << errno;
71 exit(1);
72 }
73
74 comm_fd = accept(listen_fd, (struct sockaddr*)&client, (socklen_t*)&c);
75 if (comm_fd == -1) {
76 LOG(ERROR) << sl4n::kTagStr << ": Failed to accept the socket."
77 << " Error: " << strerror(errno) << ", " << errno;
78 exit(1);
79 }
80
81 while (true) {
82 memset(str, MEMSET_VALUE, sizeof(str));
83 int read_result = read(comm_fd, str, SOCK_BUF_LEN);
84 if (read_result < 0) {
85 LOG(FATAL) << sl4n::kTagStr << ": Failed to write to the socket."
86 << " Error: " << strerror(errno) << ", " << errno;
87 exit(1);
88 }
89
90 d.Parse(str);
91 cr.Call(d);
92 d.Accept(writer);
93 std::string str2 = buffer.GetString();
94 str2 += '\n';
95 strncpy(str, str2.c_str(), sizeof(str)-1);
96 int result = write(comm_fd, str, strlen(str)+1);
97 if (result < 0) {
98 LOG(FATAL) << sl4n::kTagStr << ": Failed to write to the socket."
99 << " Error: " << strerror(errno) << ", " << errno;
100 exit(1);
101 }
102 d.RemoveAllMembers(); // Remove all members from the json object
103 buffer.Clear();
104 }
105 }
106
main(int argc,char ** argv)107 int main(int argc, char **argv) {
108 logging::LoggingSettings log_settings;
109 if (!logging::InitLogging(log_settings)) {
110 LOG(ERROR) << "Failed to set up logging";
111 return EXIT_FAILURE;
112 }
113 SockTest();
114 return 0;
115 }
116