astyle --recursive --indent=spaces=2 --style=kr --indent-col1-comments --break-blocks --convert-tabs --max-code-length=200
This is done for better merging branch/LG_PRE_RELEASE_0.3 into trunk (same process is applied on this branch) git-svn-id: http://svn.eurecom.fr/openair4G/trunk@7073 818b1a75-f10b-46b9-bf7c-635c3b92a50f
This commit is contained in:
+100
-77
@@ -44,9 +44,9 @@
|
||||
#include <signal.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
#include <netinet/in.h>
|
||||
#include <net/if.h>
|
||||
#include <signal.h>
|
||||
@@ -55,7 +55,7 @@
|
||||
#include "cli.h"
|
||||
#include "UTIL/LOG/log.h"
|
||||
|
||||
#define MAX_NUM_CLIENTS 15
|
||||
#define MAX_NUM_CLIENTS 15
|
||||
#define MAX_LINE 100
|
||||
|
||||
struct sockaddr_in saddr;
|
||||
@@ -68,136 +68,159 @@ cli_handler_t cli_server_handler= NULL;
|
||||
|
||||
cli_config *cli_cfg;
|
||||
|
||||
int cli_server_init(cli_handler_t handler) {
|
||||
int cli_server_init(cli_handler_t handler)
|
||||
{
|
||||
|
||||
int one = 1;
|
||||
cli_init();
|
||||
int one = 1;
|
||||
cli_init();
|
||||
|
||||
cli_server_handler = handler;
|
||||
if ((cli_cfg->sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
perror("socket");
|
||||
return -1;
|
||||
}
|
||||
if (setsockopt(cli_cfg->sfd, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
|
||||
sizeof(one)) < 0) {
|
||||
perror("setsockopt");
|
||||
return -1;
|
||||
}
|
||||
LOG_I(CLI,"socket created %d\n", cli_cfg->sfd);
|
||||
memset(&saddr, 0, sizeof(saddr));
|
||||
saddr.sin_family = AF_INET;
|
||||
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
saddr.sin_port = htons(server_port);
|
||||
|
||||
if (bind(cli_cfg->sfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) {
|
||||
perror("bind");
|
||||
return -1;
|
||||
}
|
||||
/* create telnet listener thread */
|
||||
if (pthread_create(&cli_server_listener, NULL, cli_server_listen, NULL)) {
|
||||
perror("thread");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
cli_server_handler = handler;
|
||||
|
||||
if ((cli_cfg->sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
perror("socket");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (setsockopt(cli_cfg->sfd, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
|
||||
sizeof(one)) < 0) {
|
||||
perror("setsockopt");
|
||||
return -1;
|
||||
}
|
||||
|
||||
LOG_I(CLI,"socket created %d\n", cli_cfg->sfd);
|
||||
memset(&saddr, 0, sizeof(saddr));
|
||||
saddr.sin_family = AF_INET;
|
||||
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
saddr.sin_port = htons(server_port);
|
||||
|
||||
if (bind(cli_cfg->sfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) {
|
||||
perror("bind");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* create telnet listener thread */
|
||||
if (pthread_create(&cli_server_listener, NULL, cli_server_listen, NULL)) {
|
||||
perror("thread");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void * cli_server_listen(void *arg) {
|
||||
|
||||
static void * cli_server_listen(void *arg)
|
||||
{
|
||||
|
||||
struct sockaddr caddr;
|
||||
socklen_t clen;
|
||||
|
||||
|
||||
clen = sizeof(caddr);
|
||||
|
||||
|
||||
printf("[CLI] server thread started\n");
|
||||
|
||||
// to be implemented
|
||||
while (1){
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
|
||||
if (listen(cli_cfg->sfd, MAX_NUM_CLIENTS) < 0) {
|
||||
perror("listen");
|
||||
exit(1);
|
||||
}
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
|
||||
printf("[CLI] server Started listening on %d\n", cli_cfg->sfd);
|
||||
|
||||
if ((cli_cfg->cfd = accept(cli_cfg->sfd, &caddr, &clen)) <0){
|
||||
while (1) {
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
|
||||
|
||||
if (listen(cli_cfg->sfd, MAX_NUM_CLIENTS) < 0) {
|
||||
perror("listen");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
|
||||
printf("[CLI] server Started listening on %d\n", cli_cfg->sfd);
|
||||
|
||||
if ((cli_cfg->cfd = accept(cli_cfg->sfd, &caddr, &clen)) <0) {
|
||||
if (cli_cfg->cfd == EINTR )
|
||||
continue;
|
||||
continue;
|
||||
else {
|
||||
perror("error on accept\n");
|
||||
exit(1);
|
||||
perror("error on accept\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
printf("[CLI] server accepted the connection %d from a new client\n", cli_cfg->cfd);
|
||||
cli_cfg->exit = 0;
|
||||
cli_start(); // init
|
||||
cli_cfg->exit = 0;
|
||||
cli_start(); // init
|
||||
|
||||
do {
|
||||
clen = recv(cli_cfg->cfd, buffer, MAX_SOCK_BUFFER_SIZE, 0);
|
||||
|
||||
if (cli_server_handler)
|
||||
cli_server_handler(buffer, clen);
|
||||
|
||||
}while (cli_cfg->exit == 0);
|
||||
cli_server_handler(buffer, clen);
|
||||
|
||||
} while (cli_cfg->exit == 0);
|
||||
|
||||
cli_finish();
|
||||
}
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
void cli_server_recv(const void * data, socklen_t len) {
|
||||
|
||||
void cli_server_recv(const void * data, socklen_t len)
|
||||
{
|
||||
|
||||
char * msg = (char*) data;
|
||||
//reomve the newlines
|
||||
//reomve the newlines
|
||||
msg[len]=0;
|
||||
msg[len-1]=0;
|
||||
msg[len-2]=0;
|
||||
//Multiplex to the right handler if needed
|
||||
//Multiplex to the right handler if needed
|
||||
cli_loop(msg);
|
||||
//sprintf(buffer, "%s_ACK", msg);
|
||||
//len = send(fd, buffer, strlen(buffer), 0);
|
||||
}
|
||||
|
||||
|
||||
void cli_server_cleanup(void){
|
||||
/* if (unlink(telnet_server_socket) < 0) {
|
||||
perror("");
|
||||
}
|
||||
void cli_server_cleanup(void)
|
||||
{
|
||||
/* if (unlink(telnet_server_socket) < 0) {
|
||||
perror("");
|
||||
}
|
||||
*/
|
||||
printf("[CLI] server pthread exit ! \n");
|
||||
if (cli_cfg->exit) close(cli_cfg->cfd);// close the client first
|
||||
pthread_exit(NULL);
|
||||
|
||||
if (cli_cfg->exit) close(cli_cfg->cfd);// close the client first
|
||||
|
||||
pthread_exit(NULL);
|
||||
pthread_cancel(cli_server_listener);
|
||||
close(cli_cfg->sfd);
|
||||
}
|
||||
|
||||
void cli_init (void) {
|
||||
void cli_init (void)
|
||||
{
|
||||
|
||||
cli_cfg = calloc(1, sizeof(cli_config));
|
||||
memset(cli_cfg, 0, sizeof(cli_config));
|
||||
cli_cfg->exit = 0;
|
||||
cli_cfg->exit = 0;
|
||||
cli_cfg->port = 1352;
|
||||
cli_cfg->promptchar='\0';
|
||||
cli_cfg->prompt[0]='\0';
|
||||
snprintf(cli_cfg->prompt,200,"%s","oaicli");
|
||||
|
||||
cli_cfg->prompt[0]='\0';
|
||||
snprintf(cli_cfg->prompt,200,"%s","oaicli");
|
||||
|
||||
}
|
||||
#ifdef STANDALONE
|
||||
int main() {
|
||||
int main()
|
||||
{
|
||||
|
||||
printf("start \n");
|
||||
cli_init();
|
||||
|
||||
//Main process of telnet server
|
||||
//Main process of telnet server
|
||||
if (cli_server_init(cli_server_recv) < 0) {
|
||||
printf("server init failed \n");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
while (cli_cfg->exit ==0) {
|
||||
}
|
||||
|
||||
printf("server init done 1 \n");
|
||||
cli_server_cleanup();
|
||||
//pthread_join(telnet_server_listener, NULL);
|
||||
printf("server init done \n");
|
||||
|
||||
|
||||
return 0;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user