00001
00002
00003
00004
00005
00006
00007
00008 #include "Player.h"
00009
00010
00011
00012
00013
00014
00015
00016 Player::Player(const string &newName,
00017 const TCPStream &newStream,
00018 Thread *newThread) :
00019 TCPStream(newStream), name(newName), ConnectionHandlerThread(newThread) {}
00020
00021
00022 Player::~Player(void) {}
00023
00024
00025 string Player::getName(void) const {return name;}
00026
00027
00028
00029
00030
00031 void Player::say(const string &message) {
00032 try {
00033 *tcp() << message;
00034 TCPStream::sync();
00035 }
00036 catch (iostream::failure *stream) {
00037 cout << "<Player msg> Exception Raised during IOSTREAM write." << endl;
00038 }
00039 }
00040
00041
00042
00043
00044
00045 void Player::say(const double number) {
00046 try {
00047 *tcp() << number;
00048 TCPStream::sync();
00049 }
00050 catch (iostream::failure *stream) {
00051 cout << "<Player msg> Exception Raised during IOSTREAM write." << endl;
00052 }
00053 }
00054
00055
00056
00057
00058
00059 void Player::say(const int number) {
00060 say((double) number);
00061 }
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 string Player::hear(const timeout_t timeout) {
00074 char *message;
00075 message = new char[MESSAGE_MAX_LENGTH];
00076
00077 if( isPending(Socket::pendingInput, timeout) ) {
00078 try {
00079
00080
00081 tcp()->getline(message, MESSAGE_MAX_LENGTH);
00082 message[strlen(message)-1] = '\0';
00083 TCPStream::sync();
00084 }
00085 catch (iostream::failure *stream) {
00086 cout << "<Player msg> Exception Raised during IOSTREAM read." << endl;
00087 }
00088 }
00089
00090 return (string (message));
00091 }
00092
00093
00094
00095
00096 void Player::suspendConnHandler(void) {
00097 try {
00098 ConnectionHandlerThread->suspend();
00099 }
00100 catch(Thread *t) {
00101 cout << "<Player msg> Exception Raised during ConnHeandler Suspending."
00102 << endl;
00103 }
00104 }
00105
00106
00107
00108
00109 void Player::resumeConnHandler(void) {
00110 try {
00111 ConnectionHandlerThread->resume();
00112 }
00113 catch(Thread *t) {
00114 cout << "<Player msg> Exception Raised during ConnHeandler Resuming."
00115 << endl;
00116 }
00117 }