- Timestamp:
- Aug 1, 2013, 1:56:17 AM (11 years ago)
- Branches:
- master
- Children:
- 8271c78
- Parents:
- b35b2b2
- Location:
- common
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
common/Common.cpp
rb35b2b2 rd05086b 1 1 #include "Common.h" 2 2 3 #include < iostream>3 #include <sstream> 4 4 #include <cmath> 5 5 … … 7 7 #include <Windows.h> 8 8 #elif defined LINUX 9 #include < time.h>9 #include <ctime> 10 10 #endif 11 11 12 12 using namespace std; 13 14 /*15 FLOAT_POSITION POSITION::toFloat() {16 FLOAT_POSITION floatPosition;17 floatPosition.x = x;18 floatPosition.y = y;19 20 return floatPosition;21 }22 */23 13 24 14 void set_nonblock(int sock) … … 51 41 } 52 42 43 string getCurrentDateTimeString() { 44 time_t millis = time(NULL); 45 struct tm *time = localtime(&millis); 46 47 ostringstream timeString; 48 timeString << time->tm_hour << ":" << time->tm_min << ":"<< time->tm_sec << " " << (time->tm_mon+1) << "/" << time->tm_mday << "/" << (time->tm_year+1900); 49 50 return timeString.str(); 51 } 52 53 53 float posDistance(FLOAT_POSITION pos1, FLOAT_POSITION pos2) { 54 54 float xDiff = pos2.x - pos1.x; -
common/Common.h
rb35b2b2 rd05086b 12 12 #endif 13 13 14 #include <string> 15 16 using namespace std; 17 14 18 typedef struct 15 19 { … … 22 26 int x; 23 27 int y; 24 //FLOAT_POSITION toFloat();25 28 FLOAT_POSITION toFloat() { 26 29 FLOAT_POSITION floatPosition; … … 34 37 void set_nonblock(int sock); 35 38 unsigned long long getCurrentMillis(); 39 string getCurrentDateTimeString(); 36 40 float posDistance(FLOAT_POSITION pos1, FLOAT_POSITION pos2); 37 41 -
common/MessageProcessor.cpp
rb35b2b2 rd05086b 2 2 3 3 #include <iostream> 4 #include <fstream> 4 5 5 6 #include "Common.h" … … 12 13 } 13 14 14 int MessageProcessor::sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest ) {15 int MessageProcessor::sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest, ofstream* outputLog) { 15 16 cout << "Sending message of type " << msg->type << endl; 16 17 17 18 msg->id = ++lastUsedId; 18 19 MessageContainer message(*msg, *dest); 19 sentMessages[msg->id][dest->sin_addr.s_addr] = message; 20 21 if (outputLog) 22 (*outputLog) << "Sending message (id " << msg->id << ") of type " << MessageContainer::getMsgTypeString(msg->type) << endl; 20 23 21 24 sentMessages[msg->id][dest->sin_addr.s_addr] = message; … … 26 29 } 27 30 28 int MessageProcessor::receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *source ) {31 int MessageProcessor::receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *source, ofstream* outputLog) { 29 32 socklen_t socklen = sizeof(struct sockaddr_in); 30 33 … … 40 43 sentMessages[msg->id][source->sin_addr.s_addr].setAcked(true); 41 44 sentMessages[msg->id][source->sin_addr.s_addr].setTimeAcked(getCurrentMillis()); 45 if (outputLog) 46 (*outputLog) << "Received ack for message id " << msg->id << endl; 42 47 } 43 48 … … 49 54 isDuplicate = true; 50 55 cout << "Got duplicate of type " << msg->type << endl; 51 }else 56 }else { 52 57 cout << "Got message of type " << msg->type << endl; 58 if (outputLog) 59 (*outputLog) << "Received message (id " << msg->id << ") of type " << MessageContainer::getMsgTypeString(msg->type) << endl; 60 } 53 61 54 62 ackedMessages[msg->id] = MessageContainer(*msg, *source); … … 69 77 } 70 78 71 void MessageProcessor::resendUnackedMessages(int sock ) {79 void MessageProcessor::resendUnackedMessages(int sock, ofstream* outputLog) { 72 80 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it; 73 81 map<unsigned long, MessageContainer>::iterator it2; … … 84 92 } 85 93 86 void MessageProcessor::cleanAckedMessages( ) {94 void MessageProcessor::cleanAckedMessages(ofstream* outputLog) { 87 95 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it = sentMessages.begin(); 88 96 map<unsigned long, MessageContainer>::iterator it2; … … 92 100 while (it2 != it->second.end()) { 93 101 if (it2->second.getAcked()) { 94 if ((getCurrentMillis() - it2->second.getTimeAcked()) > 1000) 102 if ((getCurrentMillis() - it2->second.getTimeAcked()) > 1000) { 103 if (outputLog) 104 (*outputLog) << "Removing id " << it2->second.getMessage()->id << " from the acked record" << endl; 95 105 it->second.erase(it2++); 96 else106 }else 97 107 it2++; 98 108 }else -
common/MessageProcessor.h
rb35b2b2 rd05086b 24 24 ~MessageProcessor(); 25 25 26 int sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest );27 int receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest );28 void resendUnackedMessages(int sock );29 void cleanAckedMessages( );26 int sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest, ofstream* outputLog = NULL); 27 int receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest, ofstream* outputLog = NULL); 28 void resendUnackedMessages(int sock, ofstream* outputLog = NULL); 29 void cleanAckedMessages(ofstream* outputLog = NULL); 30 30 31 31 map<unsigned int, map<unsigned long, MessageContainer> >& getSentMessages();
Note:
See TracChangeset
for help on using the changeset viewer.