chat.cpp
Go to the documentation of this file.
00001 #include <dashel/dashel.h>
00002 #include <iostream>
00003 #include <cassert>
00004 
00005 using namespace std;
00006 using namespace Dashel;
00007 
00008 string readLine(Stream* stream)
00009 {
00010         char c;
00011         string line;
00012         do
00013         {
00014                 stream->read(&c, 1);
00015                 line += c;
00016         }
00017         while (c != '\n' && c != '\r');
00018         return line;
00019 }
00020 
00021 void sendString(Stream* stream, const string& line)
00022 {
00023         stream->write(line.c_str(), line.length());
00024         stream->flush();
00025 }
00026 
00027 
00028 class ChatServer: public Hub
00029 {
00030 public:
00031         ChatServer()
00032         {
00033                 listenStream = connect("tcpin:port=8765");
00034         }
00035 
00036 protected:
00037         Stream* listenStream;
00038         map<Stream*, string> nicks;
00039         
00040 protected:
00041         void connectionCreated(Stream *stream)
00042         {
00043                 cout << "+ Incoming connection from " << stream->getTargetName() << " (" << stream << ")" << endl;
00044                 string nick = readLine(stream);
00045                 nick.erase(nick.length() - 1);
00046                 nicks[stream] = nick;
00047                 cout << "+ User " << nick << " is connected." << endl;
00048         }
00049         
00050         void incomingData(Stream *stream)
00051         {
00052                 string line = readLine(stream);
00053                 const string& nick = nicks[stream];
00054                 line = nick + " : " + line;
00055                 cout << "* Message from " << line;
00056                 
00057                 for (StreamsSet::iterator it = dataStreams.begin(); it != dataStreams.end(); ++it)
00058                         sendString((*it), line);
00059         }
00060         
00061         void connectionClosed(Stream *stream, bool abnormal)
00062         {
00063                 cout << "- Connection closed to " << stream->getTargetName() << " (" << stream << ")";
00064                 if (abnormal)
00065                         cout << " : " << stream->getFailReason();
00066                 cout << endl;
00067                 string nick = nicks[stream];
00068                 nicks.erase(stream);
00069                 cout << "- User " << nick << " is disconnected." << endl;
00070         }
00071 };
00072 
00073 class ChatClient: public Hub
00074 {
00075 public:
00076         ChatClient(string remoteTarget, const string& nick) :
00077                 nick(nick), inputStream(0)
00078         {
00079                 remoteTarget += ";port=8765";
00080                 inputStream = connect("stdin:");
00081                 remoteStream = connect(remoteTarget);
00082                 this->nick += '\n';
00083                 sendString(remoteStream, this->nick);
00084         }
00085         
00086 protected:
00087         Stream* inputStream;
00088         Stream* remoteStream;
00089         string nick;
00090         
00091         void connectionCreated(Stream *stream)
00092         {
00093                 cout << "Incoming connection " << stream->getTargetName() << " (" << stream << ")" << endl;
00094         }
00095         
00096         void incomingData(Stream *stream)
00097         {
00098                 assert(inputStream);
00099                 assert(remoteStream);
00100                 
00101                 if (stream == inputStream)
00102                 {
00103                         string line = readLine(inputStream);
00104                         sendString(remoteStream, line);
00105                 }
00106                 else
00107                 {
00108                         string line = readLine(remoteStream);
00109                         cout << line;
00110                         cout.flush();
00111                 }
00112         }
00113         
00114         void connectionClosed(Stream *stream, bool abnormal)
00115         {
00116                 cout << "Connection closed to " << stream->getTargetName() << " (" << stream << ")";
00117                 if (abnormal)
00118                         cout << " : " << stream->getFailReason();
00119                 cout << endl;
00120                 stop();
00121         }
00122 };
00123 
00124 int main(int argc, char* argv[])
00125 {
00126         try
00127         {
00128                 if (argc > 2)
00129                 {
00130                         ChatClient client(argv[1], argv[2]);
00131                         client.run();
00132                 }
00133                 else
00134                 {
00135                         ChatServer server;
00136                         server.run();
00137                 }
00138         }
00139         catch(DashelException e)
00140         {
00141                 std::cerr << e.what() << std::endl;
00142         }
00143         
00144         return 0;
00145 }


dashel
Author(s): Stéphane Magnenat
autogenerated on Sun Oct 5 2014 23:46:32