2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief A client for an lpp solving server.
9 * @author Sebastian Hack
19 #include <sys/socket.h>
20 #include <sys/types.h>
21 #include <sys/resource.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
31 #define INADDR_NONE (in_addr_t)(-1)
51 static int winsock_init(void)
53 WORD wVersionRequested;
57 wVersionRequested = MAKEWORD( 2, 2 );
59 err = WSAStartup( wVersionRequested, &wsaData );
61 /* Tell the user that we could not find a usable */
66 /* Confirm that the WinSock DLL supports 2.2.*/
67 /* Note that if the DLL supports versions greater */
68 /* than 2.2 in addition to 2.2, it will still return */
69 /* 2.2 in wVersion since that is the version we */
72 if ( LOBYTE( wsaData.wVersion ) != 2 ||
73 HIBYTE( wsaData.wVersion ) != 2 ) {
74 /* Tell the user that we could not find a usable */
83 static int connect_tcp(const char *host, uint16_t port)
87 struct sockaddr_in sin;
94 memset(&sin, 0, sizeof(sin));
95 sin.sin_family = AF_INET;
96 sin.sin_port = htons(port);
98 if ((phe = gethostbyname(host)))
99 memcpy(&sin.sin_addr, phe->h_addr_list[0], phe->h_length);
100 else if((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
101 lpp_print_err("cannot get host entry for %s", host);
105 ppe = getprotobyname("tcp");
106 ERRNO_CHECK_RETURN(s = socket(PF_INET, SOCK_STREAM, ppe->p_proto), <, 0, -1);
107 ERRNO_CHECK_RETURN(connect(s, (struct sockaddr *) &sin, sizeof(sin)), <, 0, -1);
112 char **lpp_get_solvers(const char *host)
118 ERR_CHECK_RETURN(fd = connect_tcp(host, LPP_PORT), <, 0,
119 ("could not connect to %s", host), NULL);
121 comm = lpp_comm_new(fd, LPP_BUFSIZE);
123 lpp_writel(comm, LPP_CMD_SOLVERS);
126 res = XMALLOCN(char*, n+1);
131 for(i = 0; i < n; ++i)
132 res[i] = lpp_reads(comm);
135 lpp_writel(comm, LPP_CMD_BYE);
142 void lpp_set_dbg(const char *host, int mask)
147 ERR_CHECK_RETURN_VOID(fd = connect_tcp(host, LPP_PORT), <, 0, ("could not connect to %s", host));
149 comm = lpp_comm_new(fd, LPP_BUFSIZE);
151 lpp_writel(comm, LPP_CMD_SET_DEBUG);
152 lpp_writel(comm, mask);
154 lpp_writel(comm, LPP_CMD_BYE);
160 void lpp_solve_net(lpp_t *lpp, const char *host, const char *solver)
165 ir_timer_t *t_send, *t_recv;
167 ERR_CHECK_RETURN_VOID(fd = connect_tcp(host, LPP_PORT), <, 0,
168 ("could not connect to %s", host));
170 comm = lpp_comm_new(fd, LPP_BUFSIZE);
173 lpp_writel(comm, LPP_CMD_SOLVER);
174 lpp_writes(comm, solver);
177 t_send = ir_timer_new();
178 t_recv = ir_timer_new();
180 ir_timer_start(t_send);
181 lpp_writel(comm, LPP_CMD_PROBLEM);
182 lpp_serialize(comm, lpp, 1);
183 lpp_serialize_values(comm, lpp, lpp_value_start);
185 ir_timer_stop(t_send);
186 lpp->send_time = ir_timer_elapsed_usec(t_send);
190 int cmd = lpp_readl(comm);
192 case LPP_CMD_SOLUTION:
193 ir_timer_push(t_recv);
194 lpp_deserialize_stats(comm, lpp);
195 lpp_deserialize_values(comm, lpp, lpp_value_solution);
196 ir_timer_stop(t_recv);
197 lpp->recv_time = ir_timer_elapsed_usec(t_recv);
201 lpp_readbuf(comm, buf, sizeof(buf));
202 buf[sizeof(buf) - 1] = '\0';
204 if(lpp->log != NULL) {
205 fputs(buf, lpp->log);
207 if(buf[n - 1] != '\n')
208 putc('\n', lpp->log);
213 fprintf(stderr, "solver process died unexpectedly\n");
216 fprintf(stderr, "invalid command: %s(%d)\n", lpp_get_cmd_name(cmd), cmd);
221 lpp_writel(comm, LPP_CMD_BYE);