/* Maximus project {{{ * * Copyright (C) 2012 MStar Semiconductor * * <<>> * * }}} */ /** * \file maximus/sci/utest/server/src/fake_station.cpp * \ingroup maximus_sci_utest_server * */ #include "maximus/interface/station/inc/Station.h" #include "maximus/utils/inc/Error.h" #include "maximus/sci/inc/SciServer.h" #include #include #include #include #include #include #include #include #include #include #include #include Station::Station ( Maximus &ref1, const std::string & station_executable, const Network_Clock_Tick current_tick_value, uint32_t seed): interface (ref1), fd_sta2max (-1), fd_max2sta (-1), fd_log (-1), mStationStatus (MAXIMUS_STATION_STATUS_IDLE), mStationIdleCounter (0) { int fd_socket[PIPE_WAY_NB] = {-1, -1}; int fd_pipe[PIPE_WAY_NB][PIPE_NB] = {{-1, -1}, {-1, -1}}; bool is_socket; /* station_executable is equal to "pipe" or "socket". */ is_socket = (0 != station_executable.compare ("pipe") ? true: false); if (is_socket) socketpair (AF_LOCAL, SOCK_STREAM, 0, fd_socket); else { pipe (fd_pipe[PIPE_STA2MAX]); pipe (fd_pipe[PIPE_MAX2STA]); } mPid = fork (); if (mPid == -1) { Error e (__PRETTY_FUNCTION__, "fork failed", errno); e.display (); exit (EXIT_FAILURE); } else if (mPid == 0) { /* Child process. */ if (is_socket) { close (fd_socket[SOCKET_MAXIMUS]); /* socket become stdout and stdin. */ dup2 (fd_socket[SOCKET_STA], 0); dup2 (fd_socket[SOCKET_STA], 1); close (fd_socket[SOCKET_STA]); } else { /* Child read on its stdin(0). */ close (fd_pipe[PIPE_MAX2STA][PIPE_WRITE]); dup2 (fd_pipe[PIPE_MAX2STA][PIPE_READ], 0); close (fd_pipe[PIPE_MAX2STA][PIPE_READ]); /* Child write on its stdout(1). */ close (fd_pipe[PIPE_STA2MAX][PIPE_READ]); dup2 (fd_pipe[PIPE_STA2MAX][PIPE_WRITE], 1); close (fd_pipe[PIPE_STA2MAX][PIPE_WRITE]); } fd_max2sta = 0; fd_sta2max = 1; try { const int header_size = sizeof (struct Sci_Msg_Header); char alloc[SCI_MSG_MAX_SIZE]; struct Sci_Msg_Header *header = (Sci_Msg_Header *) alloc; int msg_size; int ret; size_t nb_message; for (nb_message = 0; nb_message < seed; nb_message++) { ret = read (fd_max2sta, alloc, header_size); assert (ret == header_size); msg_size = ntohl (header->length); if (msg_size != 0) { ret = read (fd_max2sta, alloc + header_size, msg_size); assert (ret == msg_size); } ret = write (fd_sta2max, alloc, msg_size + header_size); assert (ret == msg_size + header_size); } } catch (Error &e) { e.display (); exit (EXIT_FAILURE); } /* Wait the SIGKILL. */ pause (); exit (EXIT_FAILURE); } else { /* Parent process. */ if (is_socket) { close (fd_socket[SOCKET_STA]); fd_sta2max = fd_socket[SOCKET_MAXIMUS]; fd_max2sta = fd_socket[SOCKET_MAXIMUS]; } else { close (fd_pipe[PIPE_MAX2STA][PIPE_READ]); close (fd_pipe[PIPE_STA2MAX][PIPE_WRITE]); fd_sta2max = fd_pipe[PIPE_STA2MAX][PIPE_READ]; fd_max2sta = fd_pipe[PIPE_MAX2STA][PIPE_WRITE]; } } } Station::~Station () { kill (mPid, SIGKILL); } void Station::decrementStationIdleCounter () { } bool Station::hideStation ( Sci_Msg_Station_Id station_id, bool hidden) { return true; }