socket_server.cpp
Go to the documentation of this file.
00001 
00009 /*****************************************************************************
00010 ** Platform Check
00011 *****************************************************************************/
00012 
00013 #include <ecl/config/ecl.hpp>
00014 
00015 #ifdef ECL_IS_POSIX
00016 #ifndef ECL_IS_MAC
00017 
00018 /*****************************************************************************
00019 ** Includes
00020 *****************************************************************************/
00021 
00022 #include <iostream>
00023 #include <ecl/command_line.hpp>
00024 #include <ecl/threads/thread.hpp>
00025 #include <ecl/time/sleep.hpp>
00026 #include <ecl/time/timestamp.hpp>
00027 #include <ecl/devices/socket_connection_status.hpp>
00028 #include <ecl/devices/socket.hpp>
00029 #include <ecl/formatters.hpp>
00030 
00031 /*****************************************************************************
00032 ** Namespaces
00033 *****************************************************************************/
00034 
00035 namespace ecl {
00036 namespace demos {
00037 
00038 /*****************************************************************************
00039 ** Using
00040 *****************************************************************************/
00041 
00042 using ecl::CmdLine;
00043 using ecl::ValueArg;
00044 using ecl::SwitchArg;
00045 using ecl::ArgException;
00046 using ecl::ConnectionHungUp;
00047 using ecl::SocketServer;
00048 using ecl::Format;
00049 using ecl::Hex;
00050 using ecl::NoAlign;
00051 using ecl::Thread;
00052 using ecl::MilliSleep;
00053 using ecl::TimeStamp;
00054 
00055 /*****************************************************************************
00056 ** Classes
00057 *****************************************************************************/
00058 
00059 class Reader {
00060 public:
00061         Reader(SocketServer &socket_server, bool timestamps_reqd, bool hex_format_reqd, const int &port_number) :
00062                 socket(socket_server),
00063                 timestamps(timestamps_reqd),
00064                 hex(hex_format_reqd),
00065                 port(port_number),
00066                 new_line(true)
00067         {}
00068 
00069         void loop() {
00070 
00071         Format<unsigned char> hex_format(-1,NoAlign,Hex);
00072         char c;
00073         long ch_read;
00074         MilliSleep sleep;
00075 
00076         if ( hex ) { std::cout << "Hex format" << std::endl; } else { std::cout << "Ascii format" << std::endl; }
00077 
00078         while (1) {
00079             ch_read = socket.read(c);
00080             if ( ch_read > 0 ) {
00081                 if ( hex ) {
00082                     if ( timestamps ) {
00083                         if ( new_line ) { timestamp.stamp(); }
00084                                         std::cout << "[" << timestamp << "] : ";
00085                     }
00086                     std::cout << hex_format(c) << std::endl;
00087                 } else {
00088                     if ( timestamps && new_line ) {
00089                                         timestamp.stamp();
00090                                         std::cout << "[" << timestamp << "] : ";
00091                     }
00092                     std::cout << c;
00093                 }
00094                 std::cout.flush();
00095                 if ( timestamps ) {
00096                         if ( new_line ) {
00097                                 new_line = false;
00098                         } else if ( c == '\n' ) {
00099                                 new_line = true;
00100                         }
00101                 }
00102             } else if ( ch_read < 0 ) {
00103                 if ( ch_read == ConnectionHungUp ) {
00104                     std::cout << "Client Hung Up." << std::endl;
00105                     exit(EXIT_FAILURE);
00106                 }
00107             } else {
00108                 // nothing read
00109             }
00110             sleep(15);
00111         }
00112 
00113         }
00114 
00115 private:
00116         SocketServer &socket;
00117         TimeStamp timestamp;
00118         bool timestamps;
00119         bool hex;
00120         int port;
00121         bool new_line;
00122 };
00123 
00124 class Writer {
00125 public:
00126         Writer(SocketServer &socket_server, bool timestamps_reqd) :
00127                 socket(socket_server),
00128                 timestamps(timestamps_reqd)
00129         {}
00130 
00131         void loop() {
00132         MilliSleep sleep;
00133         char buffer[256];
00134         memset(buffer,0,256);
00135         char *s_ptr;
00136         while( 1 ) {
00137             if ( fgets(buffer,80,stdin) == NULL ) {
00138                 break;
00139             }
00140             // fgets always terminates with a null character, even if it manages
00141             // to read n-1 chars
00142             s_ptr = buffer;
00143             while ( *s_ptr != '\0') { ++s_ptr; }
00144             if ( timestamps ) { timestamp.stamp(); }
00145             socket.write(buffer,s_ptr - buffer);
00146             if ( timestamps ) {
00147                 std::cout << "[" << timestamp << "] : ";
00148             }
00149             std::cout << buffer; // already terminated by an endl;
00150             std::cout.flush();
00151             sleep(500);
00152         }
00153 
00154         }
00155 
00156 private:
00157         SocketServer &socket;
00158         TimeStamp timestamp;
00159         bool timestamps;
00160 };
00161 
00162 } // namespace demos
00163 } // namespace ecl
00164 
00165 /*****************************************************************************
00166 ** Using
00167 *****************************************************************************/
00168 
00169 using namespace ecl::demos;
00170 
00171 /*****************************************************************************
00172 ** Main
00173 *****************************************************************************/
00174 
00175 int main(int argc, char** argv) {
00176 
00177     std::cout << std::endl;
00178     std::cout << "***********************************************************" << std::endl;
00179     std::cout << "                 Parsing Command Line" << std::endl;
00180     std::cout << "***********************************************************" << std::endl;
00181     std::cout << std::endl;
00182     int port = 0;
00183     bool hex(false);
00184     bool timestamps(false);
00185 
00186     try {
00187         CmdLine cmd("This is a simple interface for making serving from a socket.",' ',"0.1");
00188         ValueArg<int> arg_port("p","port","Port to connect to [1470].",false,1470,"integer");
00189         SwitchArg switch_hex("x","hex","Enable hex output.",false);
00190         SwitchArg switch_timestamps("t","timestamp","Timestamp incoming/outgoings.",false);
00191 
00192         cmd.add(arg_port);
00193         cmd.add(switch_hex);
00194         cmd.add(switch_timestamps);
00195         cmd.parse(argc,argv);
00196 
00197         hex = switch_hex.getValue();
00198         port = arg_port.getValue();
00199         timestamps = switch_timestamps.getValue();
00200 
00201     } catch ( ArgException &e ) {
00202         std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl;
00203     }
00204 
00205     std::cout << "Listening on port: " << port << std::endl;
00206 
00207     std::cout << std::endl;
00208     std::cout << "***********************************************************" << std::endl;
00209     std::cout << "                   Read and Write Threads   " << std::endl;
00210     std::cout << "***********************************************************" << std::endl;
00211     std::cout << std::endl;
00212 
00213     SocketServer server(port);
00214     server.listen();
00215     Writer writer(server,timestamps);
00216     Reader reader(server,timestamps,hex,port);
00217     Thread read_thread(&Reader::loop, reader);
00218     Thread write_thread(&Writer::loop, writer);
00219 
00220     read_thread.join();
00221     write_thread.join();
00222 
00223     std::cout << std::endl;
00224     std::cout << "***********************************************************" << std::endl;
00225     std::cout << "                      Passed" << std::endl;
00226     std::cout << "***********************************************************" << std::endl;
00227     std::cout << std::endl;
00228 
00229         return 0;
00230 }
00231 
00232 #endif /* ECL_IS_POSIX */
00233 #endif  /* !ECL_IS_MAC */
00234 
00235 #if !defined ECL_IS_POSIX || ECL_IS_MAC
00236 
00237 #include <iostream>
00238 
00239 int main(int argc, char **argv) {
00240 
00241         std::cout << "This is a posix (not mac) only app." << std::endl;
00242         return 0;
00243 }
00244 
00245 #endif


ecl_core_apps
Author(s): Daniel Stonier (d.stonier@gmail.com)
autogenerated on Thu Jan 2 2014 11:13:30