create_backend_connection added.

This routine calls the gw_mysql_connect passing the MySQL_session data available
This commit is contained in:
Massimiliano Pinto 2013-06-14 18:05:29 +02:00
parent ebbe4bf035
commit 1239420be1

View File

@ -258,3 +258,73 @@ int gw_read_gwbuff(DCB *dcb, GWBUF **head, int b) {
return 0;
}
/*
* Create a new MySQL backend connection.
*
* This routine performs the MySQL connection to the backend and fills the session->backends of the callier dcb
* with the new allocatetd dcb and adds the new socket to the epoll set
*
* - backend dcb allocation
* - MySQL session data fetch
* - backend connection using data in MySQL session
*
* @param client_dcb The client DCB struct
* @param epfd The epoll set to add the new connection
* @return 0 on Success or 1 on Failure.
*/
int create_backend_connection(DCB *client_dcb, int efd) {
struct epoll_event ee;
DCB *backend = NULL;
MySQLProtocol *ptr_proto = NULL;
MySQLProtocol *client_protocol = NULL;
SESSION *session = NULL;
MYSQL_session *s_data = NULL;
backend = (DCB *) calloc(1, sizeof(DCB));
backend->state = DCB_STATE_ALLOC;
backend->session = NULL;
backend->protocol = (MySQLProtocol *)gw_mysql_init(NULL);
ptr_proto = (MySQLProtocol *)backend->protocol;
client_protocol = (MySQLProtocol *)client_dcb->protocol;
session = DCB_SESSION(client_dcb);
s_data = (MYSQL_session *)session->data;
// this is blocking until auth done
if (gw_mysql_connect("127.0.0.1", 3306, s_data->db, s_data->user, s_data->client_sha1, backend->protocol) == 0) {
fprintf(stderr, "Connected to backend mysql server\n");
backend->fd = ptr_proto->fd;
setnonblocking(backend->fd);
} else {
fprintf(stderr, "<<<< NOT Connected to backend mysql server!!!\n");
backend->fd = -1;
}
// edge triggering flag added
ee.events = EPOLLIN | EPOLLET | EPOLLOUT;
ee.data.ptr = backend;
// if connected, add it to the epoll
if (backend->fd > 0) {
if (epoll_ctl(efd, EPOLL_CTL_ADD, backend->fd, &ee) == -1) {
perror("epoll_ctl: backend sock");
} else {
fprintf(stderr, "--> Backend conn added, bk_fd [%i], scramble [%s], is session with client_fd [%i]\n", ptr_proto->fd, ptr_proto->scramble, client_dcb->fd);
backend->state = DCB_STATE_POLLING;
backend->session = DCB_SESSION(client_dcb);
(backend->func).read = gw_read_backend_event;
(backend->func).write = MySQLWrite;
(backend->func).write_ready = gw_write_backend_event;
(backend->func).error = handle_event_errors_backend;
// assume here one backend only.
// in session.h
// struct dcb *backends;
// instead of a list **backends;
client_dcb->session->backends = backend;
}
return 0;
}
return 1;
}
//////