microterm.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 #define LOCAL_ECHO
00009 
00010 class MicroTerm: public Hub
00011 {
00012 public:
00013         MicroTerm(const char* t0, const char* t1)
00014         {
00015                 s0 = connect(t0);
00016                 s1 = connect(t1);
00017         }
00018         
00019 protected:
00020         Stream* s0;
00021         Stream* s1;
00022         
00023         void connectionCreated(Stream *stream)
00024         {
00025                 cout << "Incoming connection " << stream->getTargetName() << " (" << stream << ")" << endl;
00026         }
00027         
00028         void incomingData(Stream *stream)
00029         {
00030                 assert(s0);
00031                 assert(s1);
00032                 
00033                 char c;
00034                 stream->read(&c, 1);
00035                 if (stream == s0)
00036                 {
00037                         #ifdef LOCAL_ECHO
00038                         if(c == 4)
00039                                 stop();
00040                         #ifdef WIN32
00041                         if(c == '\r')
00042                                 cout << std::endl;
00043                         else
00044                         #endif
00045                                 cout << c;
00046                         #endif
00047                         s1->write(&c, 1);
00048                 }
00049                 else
00050                 {
00051                         #ifdef WIN32
00052                         if(c == '\r')
00053                                 cout << std::endl;
00054                         else
00055                         #endif
00056                                 cout << c;
00057                         cout.flush();
00058                 }
00059         }
00060         
00061         void connectionClosed(Stream *stream, bool abnormal)
00062         {
00063                 cout << "Closed connection " << stream->getTargetName() << " (" << stream << ")";
00064                 if (abnormal)
00065                         cout << " : " << stream->getFailReason();
00066                 cout << endl;
00067                 stop();
00068         }
00069 };
00070 
00071 int main(int argc, char* argv[])
00072 {
00073         if (argc != 3)
00074         {
00075                 cerr << "Usage: " << argv[0] << " inputTarget destTarget" << endl;
00076                 return 1;
00077         }
00078         
00079         try
00080         {
00081                 MicroTerm microTerm(argv[1], argv[2]);
00082                 
00083                 microTerm.run();
00084         }
00085         catch (DashelException e)
00086         {
00087                 std::cerr << e.what() << std::endl;
00088         }
00089         
00090         return 0;
00091 }


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