本文整理汇总了C++中MsgDistributor类的典型用法代码示例。如果您正苦于以下问题:C++ MsgDistributor类的具体用法?C++ MsgDistributor怎么用?C++ MsgDistributor使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MsgDistributor类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: while
/******************************************************************************
Description.: this is the mflisten thread
it loops forever, listen on the src_GUID
Input Value.:
Return Value:
******************************************************************************/
void *mflisten_thread(void *arg)
{
if (debug) printf("\nin listen thread\n");
while (!global_stop) {
MsgD.listen();
}
return NULL;
}
示例2: errorSocket
/******************************************************************************
Description.: print out the error message and exit
Input Value.:
Return Value:
******************************************************************************/
void errorSocket(const char *msg, int sock)
{
errno = EBADF;
perror(msg);
if (!orbit)
{
close(sock);
}
else
{
MsgD.close(sock, 1);
}
printf("[server] Connection closed. --- error\n\n");
pthread_exit(NULL); //terminate calling thread!
}
示例3: if
/******************************************************************************
Description.: There is a separate instance of this function
for each connection. It handles all communication
once a connnection has been established.
Input Value.:
Return Value: -
******************************************************************************/
void *serverThread (void * inputsock)
{
int sock = *((int *)inputsock);
int n;
char buffer[100];
string userID;
char *threadType;
char fail[] = "failed";
// Receive the header
bzero(buffer, sizeof(buffer));
if (!orbit)
{
n = read(sock, buffer, sizeof(buffer));
if (n < 0)
{
errorSocket("ERROR reading from socket", sock);
}
}
// below is orbit mode, using MFAPI
else
{
MsgD.recv(sock, buffer, sizeof(buffer));
}
printf("[server] header content: %s\n\n",buffer);
threadType = strtok(buffer, ",");
userID = strtok(NULL, ",");
// grap the lock
pthread_mutex_lock(&user_map_lock);
// confirm that this user does not log in
if (user_map.find(userID) == user_map.end())
{
// put the new user into user map
user_map[userID] = 1;
}
else
{
if (user_map[userID] == 1)
{
// increase user thread count
user_map[userID] = 2;
}
else
{
// remember to unlock!
pthread_mutex_unlock(&user_map_lock);
// reponse to the client
if (!orbit)
{
if (write(sock, "failed", sizeof("failed")) < 0)
{
errorSocket("ERROR writting to socket", sock);
}
close(sock);
}
else
{
MsgD.send(sock, fail, sizeof(fail));
}
printf("[server] User exist. Connection closed.\n\n");
return 0;
}
}
pthread_mutex_unlock(&user_map_lock);
if (strcmp(threadType, "transmit") == 0)
{
server_transmit(sock, userID);
}
else if (strcmp(threadType, "result") == 0)
{
server_result(sock, userID);
}
else
{
if (!orbit)
{
close(sock);
}
printf("[server] Command Unknown. Connection closed.\n\n");
}
return 0;
}
示例4: server_transmit
/******************************************************************************
Description: function for transmitting the frames
Input Value.:
Return Value:
******************************************************************************/
void server_transmit (int sock, string userID)
{
// printf("transmitting part\n");
int n;
char response[] = "ok";
char file_name_temp[60];
char *file_name;
int write_length = 0;
int length = 0;
queue<string> *imgQueue = new queue<string>(); // queue storing the file names
// grap the lock
pthread_mutex_lock(&queue_map_lock);
queue_map[userID] = imgQueue; // put the address of queue into map
pthread_mutex_unlock(&queue_map_lock);
pthread_mutex_t queueLock; // mutex lock for queue operation
sem_t *sem_match = 0;
// init the mutex lock
if (pthread_mutex_init(&queueLock, NULL) != 0)
{
errorSocket("ERROR mutex init failed", sock);
}
if (!orbit)
{
char buffer[BUFFER_SIZE];
char *file_size_char;
int file_size;
int received_size = 0;
// reponse to the client
n = write(sock, response, sizeof(response));
if (n < 0)
{
pthread_mutex_destroy(&queueLock);
errorSocket("ERROR writting to socket", sock);
}
while (!global_stop)
{
received_size = 0;
// receive the file info
bzero(buffer, sizeof(buffer));
n = read(sock,buffer, sizeof(buffer));
if (n <= 0)
{
pthread_mutex_destroy(&queueLock);
// signal the result thread to terminate
sem_post(sem_match);
errorSocket("ERROR reading from socket", sock);
}
// store the file name and the block count
file_name = strtok(buffer, ",");
strcpy(file_name_temp, file_name);
if (debug) printf("\n[server] file name: [%s]\n", file_name);
file_size_char = strtok(NULL, ",");
file_size = strtol(file_size_char, NULL, 10);
if (debug) printf("file size: %d\n", file_size);
// calculate the time consumption here
struct timeval tpstart,tpend;
double timeuse;
gettimeofday(&tpstart,NULL);
// reponse to the client
n = write(sock, response, sizeof(response));
if (n <= 0)
{
pthread_mutex_destroy(&queueLock);
// signal the result thread to terminate
sem_post(sem_match);
errorSocket("ERROR writting to socket", sock);
}
if (!storm)
{
FILE *fp = fopen(file_name, "w");
if (fp == NULL)
{
printf("File:\t%s Can Not Open To Write!\n", file_name);
break;
}
int done = 0;
// receive the data from client and store them into buffer
bzero(buffer, sizeof(buffer));
while((length = recv(sock, buffer, sizeof(buffer), 0)))
{
if (length < 0)
{
//.........这里部分代码省略.........
示例5: server_result
/******************************************************************************
Description: function for sending back the result
Input Value.:
Return Value:
******************************************************************************/
void server_result (int sock, string userID)
{
if (debug) printf("result thread\n\n");
int n, fd;
char response[] = "ok";
sem_t *sem_match = new sem_t(); // create a new semaphore in heap
queue<string> *imgQueue = 0; // queue storing the file names
// Init semaphore and put the address of semaphore into map
if (sem_init(sem_match, 0, 0) != 0)
{
errorSocket("ERROR semaphore init failed", sock);
}
// grap the lock
pthread_mutex_lock(&sem_map_lock);
sem_map[userID] = sem_match;
pthread_mutex_unlock(&sem_map_lock);
// reponse to the client
if (!orbit)
{
n = write(sock, response, sizeof(response));
if (n < 0)
{
error("ERROR writting to socket");
}
}
else
{
MsgD.send(sock, response, sizeof(response));
}
struct sockaddr_in myaddr;
int ret;
char buf[1024];
int serverPort = 9879;
if (storm)
{
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
printf("socket create failed\n");
if (debug) printf("socket created\n");
/* bind it to all local addresses and pick any port number */
memset((char *)&myaddr, 0, sizeof(myaddr));
myaddr.sin_family = AF_INET;
myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
myaddr.sin_port = htons(serverPort);
if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
perror("bind failed");
goto stop;
}
if (debug) printf("socket binded\n");
}
while(!global_stop)
{
sem_wait(sem_match);
// get the address of image queue
if (imgQueue == 0)
{
imgQueue = queue_map[userID];
}
// check if the queue is empty
if (imgQueue->empty())
{
sem_map.erase(userID);
queue_map.erase(userID);
user_map.erase(userID);
delete(sem_match);
delete(imgQueue);
sem_destroy(sem_match);
// if (orbit)
// {
// MsgD.close(sock, 0);
// }
printf("[server] client disconnected --- result\n");
// pthread_exit(NULL); //terminate calling thread!
return;
}
if (!storm)
{
if (debug) printf("\n----------- start matching -------------\n");
string file_name = imgQueue->front();
if (debug) printf("file name: [%s]\n", file_name.c_str());
imgQueue->pop();
// create a new thread to do the image processing
//.........这里部分代码省略.........
示例6: errorSocket
/******************************************************************************
Description.: this is the transmit child thread
it is responsible to send out one frame
Input Value.:
Return Value:
******************************************************************************/
void *result_child(void *arg)
{
if (debug) printf("result child thread\n");
struct arg_result *args = (struct arg_result *)arg;
int sock = args->sock;
char *file_name = args->file_name;
int matchedIndex;
char defMsg[] = "none";
char sendInfo[200];
vector<float> coord;
ImgMatch imgM;
// start matching the image
imgM.matchImg(file_name);
matchedIndex = imgM.getMatchedImgIndex();
if (matchedIndex == 0)
{
// write none to client
if (!orbit) {
if (write(sock, defMsg, sizeof(defMsg)) < 0)
{
errorSocket("ERROR writting to socket", sock);
}
printf("Not match.\n\n");
}
else
{
MsgD.send(sock, defMsg, sizeof(defMsg));
printf("Not match.\n\n");
}
// if (debug) printf("not match\n");
}
else
{
// send result to client
coord = imgM.calLocation();
string info = imgM.getInfo();
sprintf(sendInfo, "%s,%f,%f,%f,%f,%f,%f,%f,%f", info.c_str(), coord.at(0), coord.at(1), coord.at(2), coord.at(3), coord.at(4), coord.at(5), coord.at(6), coord.at(7));
printf("Matched Index: %d\n\n", matchedIndex);
if (debug) printf("sendInfo: %s\n", sendInfo);
if (!orbit)
{
if (write(sock, sendInfo, sizeof(sendInfo)) < 0)
{
errorSocket("ERROR writting to socket", sock);
}
}
else
{
MsgD.send(sock, sendInfo, sizeof(sendInfo));
}
// if (debug) printf("matched image index: %d\n", matchedIndex);
}
if (debug) printf("------------- end matching -------------\n");
return NULL;
}
示例7: main
int main(int argc, char *argv[])
{
int src_GUID = -1, dst_GUID = -1;
/* parameter parsing */
while(1) {
int option_index = 0, c = 0;
static struct option long_options[] =
{
{"h", no_argument, 0, 0},
{"help", no_argument, 0, 0},
{"v", no_argument, 0, 0},
{"version", no_argument, 0, 0},
{"orbit", no_argument, 0, 0},
{"d", no_argument, 0, 0},
{"m", required_argument, 0, 0},
{"o", required_argument, 0, 0},
{"storm", no_argument, 0, 0},
{"train", no_argument, 0, 0},
{0, 0, 0, 0}
};
c = getopt_long_only(argc, argv, "", long_options, &option_index);
/* no more options to parse */
if(c == -1) break;
/* unrecognized option */
if(c == '?')
{
help();
return 0;
}
switch(option_index)
{
/* h, help */
case 0:
case 1:
help();
return 0;
break;
/* v, version */
case 2:
case 3:
printf("Real-Time CPS Server Version: 0.1\n" \
"Compilation Date.....: unknown\n" \
"Compilation Time.....: unknown\n");
return 0;
break;
/* orbit, run in orbit mode */
case 4:
orbit = 1;
break;
/* debug mode */
case 5:
debug = 1;
break;
/* mine GUID */
case 6:
src_GUID = strtol(optarg, NULL, 10);
break;
/* other's GUID */
case 7:
dst_GUID = strtol(optarg, NULL, 10);
break;
/* storm mode */
case 8:
storm = 1;
break;
/* train mode */
case 9:
train = 1;
break;
default:
help();
return 0;
}
}
if (!orbit)
{
/* register signal handler for <CTRL>+C in order to clean up */
if(signal(SIGINT, signal_handler) == SIG_ERR)
{
printf("could not register signal handler\n");
exit(EXIT_FAILURE);
}
}
// init the mutex lock
if (pthread_mutex_init(&user_map_lock, NULL) != 0
//.........这里部分代码省略.........
示例8: server_main
/******************************************************************************
Description.: calling this function creates and starts the server threads
Input Value.: -
Return Value: -
******************************************************************************/
void server_main()
{
printf("\n[server] start supporting service\n");
if (!orbit)
{
// init part
int sockfd, newsockfd, portno;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
clilen = sizeof(cli_addr);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
error("ERROR opening socket");
}
else
if (debug) printf ("[server] obtain socket descriptor successfully.\n");
bzero((char *) &serv_addr, sizeof(serv_addr));
// set up the port number
portno = PORT_NO;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
error("ERROR on binding");
}
else
if (debug) printf("[server] bind tcp port %d sucessfully.\n",portno);
if(listen(sockfd,5))
{
error("ERROR listening");
}
else
if (debug) printf ("[server] listening the port %d sucessfully.\n\n", portno);
// init finished, now wait for a client
while (!global_stop) {
pthread_t thread_id;
//Block here. Until server accpets a new connection.
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
{
// error("ERROR on accept");
fprintf(stderr,"Accept error!\n");
continue; //ignore current socket ,continue while loop.
}
else
printf ("[server] server has got connect from %s, socket id: %d.\n", (char *)inet_ntoa(cli_addr.sin_addr), newsockfd);
/* create thread and pass context to thread function */
if (pthread_create(&thread_id, 0, serverThread, (void *)&(newsockfd)) == -1)
{
fprintf(stderr,"pthread_create error!\n");
break; //break while loop
}
pthread_detach(thread_id);
usleep(1000 * 5); // sleep 5ms to avoid clients gain same sock
} /* end of while */
close(sockfd);
}
// below is orbit mode
else
{
int newsockfd;
// init finished, now wait for a client
while (!global_stop) {
pthread_t thread_id;
//Block here. Until server accpets a new connection.
newsockfd = MsgD.accept();
if (newsockfd < 0)
{
// error("ERROR on accept");
fprintf(stderr,"Accept error!\n");
continue; //ignore current socket ,continue while loop.
}
else
printf ("[server] server has got new connection, socket id: %d.\n", newsockfd);
/* create thread and pass context to thread function */
if (pthread_create(&thread_id, 0, serverThread, (void *)&(newsockfd)) == -1)
{
fprintf(stderr,"pthread_create error!\n");
break; //break while loop
}
pthread_detach(thread_id);
usleep(1000 * 5); // sleep 5ms to avoid clients gain same sock
} /* end of while */
if (debug) printf("main end\n");
//.........这里部分代码省略.........
示例9: server_transmit
/******************************************************************************
Description: function for transmitting the frames
Input Value.:
Return Value:
******************************************************************************/
void server_transmit (int sock, string userID)
{
// printf("transmitting part\n");
int n;
char buffer[BUFFER_SIZE];
char response[] = "ok";
char file_name_temp[60];
char *file_name;
int write_length = 0;
int length = 0;
char *block_count_char;
int block_count;
int count = 0;
queue<string> *imgQueue = new queue<string>(); // queue storing the file names
// grap the lock
pthread_mutex_lock(&queue_map_lock);
queue_map[userID] = imgQueue; // put the address of queue into map
pthread_mutex_unlock(&queue_map_lock);
pthread_mutex_t queueLock; // mutex lock for queue operation
sem_t *sem_match = 0;
// init the mutex lock
if (pthread_mutex_init(&queueLock, NULL) != 0)
{
errorSocket("ERROR mutex init failed", sock);
}
if (!orbit)
{
// reponse to the client
n = write(sock, response, sizeof(response));
if (n < 0)
{
pthread_mutex_destroy(&queueLock);
errorSocket("ERROR writting to socket", sock);
}
while (!global_stop)
{
// receive the file info
bzero(buffer,BUFFER_SIZE);
n = read(sock,buffer, sizeof(buffer));
if (n <= 0)
{
pthread_mutex_destroy(&queueLock);
// signal the result thread to terminate
sem_post(sem_match);
errorSocket("ERROR reading from socket", sock);
}
// store the file name and the block count
file_name = strtok(buffer, ",");
strcpy(file_name_temp, file_name);
printf("\n[server] file name: %s\n", file_name);
block_count_char = strtok(NULL, ",");
block_count = strtol(block_count_char, NULL, 10);
// printf("block count: %d\n", block_count);
// reponse to the client
n = write(sock, response, sizeof(response));
if (n <= 0)
{
pthread_mutex_destroy(&queueLock);
// signal the result thread to terminate
sem_post(sem_match);
errorSocket("ERROR writting to socket", sock);
}
FILE *fp = fopen(file_name, "w");
if (fp == NULL)
{
printf("File:\t%s Can Not Open To Write!\n", file_name);
break;
}
// receive the data from server and store them into buffer
bzero(buffer, sizeof(buffer));
count = 0;
while((length = recv(sock, buffer, BUFFER_SIZE, 0)))
{
if (length < 0)
{
printf("Recieve Data From Client Failed!\n");
break;
}
write_length = fwrite(buffer, sizeof(char), length, fp);
if (write_length < length)
{
printf("File:\t Write Failed!\n");
break;
}
//.........这里部分代码省略.........
示例10: server_result
/******************************************************************************
Description: function for sending back the result
Input Value.:
Return Value:
******************************************************************************/
void server_result (int sock, string userID)
{
// printf("result thread\n\n");
int n;
char response[BUFFER_SIZE] = "ok";
char defMsg[BUFFER_SIZE] = "none";
int matchedIndex;
char sendInfo[BUFFER_SIZE];
vector<float> coord;
sem_t *sem_match = new sem_t(); // create a new semaphore in heap
queue<string> *imgQueue = 0; // queue storing the file names
// Init semaphore and put the address of semaphore into map
if (sem_init(sem_match, 0, 0) != 0)
{
errorSocket("ERROR semaphore init failed", sock);
}
// grap the lock
pthread_mutex_lock(&sem_map_lock);
sem_map[userID] = sem_match;
pthread_mutex_unlock(&sem_map_lock);
// reponse to the client
if (!orbit)
{
n = write(sock, response, sizeof(response));
if (n < 0)
{
error("ERROR writting to socket");
}
}
else
{
MsgD.send(sock, response, BUFFER_SIZE);
}
while(!global_stop)
{
sem_wait(sem_match);
// get the address of image queue
if (imgQueue == 0)
{
imgQueue = queue_map[userID];
}
// check if the queue is empty
if (imgQueue->empty())
{
sem_map.erase(userID);
queue_map.erase(userID);
user_map.erase(userID);
delete(sem_match);
delete(imgQueue);
sem_destroy(sem_match);
MsgD.close(sock, 0);
printf("[server] client disconnectted\n");
pthread_exit(NULL); //terminate calling thread!
}
if (debug) printf("\n----------- start matching -------------\n");
string file_name = imgQueue->front();
if (debug) printf("file name: %s\n", file_name.c_str());
imgQueue->pop();
// start matching the image
imgM.matchImg(file_name);
matchedIndex = imgM.getMatchedImgIndex();
if (matchedIndex == 0)
{
// write none to client
if (!orbit) {
if (write(sock, defMsg, sizeof(defMsg)) < 0)
{
errorSocket("ERROR writting to socket", sock);
}
}
else
{
MsgD.send(sock, defMsg, BUFFER_SIZE);
}
if (debug) printf("not match\n");
}
else
{
// write index to client
coord = imgM.calLocation();
sprintf(sendInfo, "%d,%f,%f,%f,%f,%f,%f,%f,%f", matchedIndex, coord.at(0), coord.at(1), coord.at(2), coord.at(3), coord.at(4), coord.at(5), coord.at(6), coord.at(7));
if (debug) printf("sendInfo: %s\n", sendInfo);
if (!orbit)
{
if (write(sock, sendInfo, sizeof(sendInfo)) < 0)
{
//.........这里部分代码省略.........