socket_client.cpp
Go to the documentation of this file.
00001 
00008 /*****************************************************************************
00009 ** Platform Check
00010 *****************************************************************************/
00011 
00012 #include <ecl/config/ecl.hpp>
00013 
00014 #ifdef ECL_IS_POSIX
00015 #ifndef ECL_IS_MAC
00016 
00017 /*****************************************************************************
00018 ** Includes
00019 *****************************************************************************/
00020 
00021 #include <iostream>
00022 #include <string>
00023 #include <ecl/command_line.hpp>
00024 #include <ecl/formatters.hpp>
00025 #include <ecl/threads/thread.hpp>
00026 #include <ecl/time/sleep.hpp>
00027 #include <ecl/time/timestamp.hpp>
00028 #include <ecl/devices/socket.hpp>
00029 
00030 /*****************************************************************************
00031 ** Namespaces
00032 *****************************************************************************/
00033 
00034 namespace ecl {
00035 namespace demos {
00036 
00037 /*****************************************************************************
00038 ** Using
00039 *****************************************************************************/
00040 
00041 using std::string;
00042 using ecl::CmdLine;
00043 using ecl::ValueArg;
00044 using ecl::SwitchArg;
00045 using ecl::ArgException;
00046 using ecl::ConnectionHungUp;
00047 using ecl::SocketClient;
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 /*****************************************************************************
00057 ** Classes
00058 *****************************************************************************/
00059 
00060 class Reader {
00061 public:
00062         Reader(SocketClient &socket_server, bool timestamps_reqd, bool hex_format_reqd = false) :
00063                 socket(socket_server),
00064                 timestamps(timestamps_reqd),
00065                 hex(hex_format_reqd),
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 << "Server Hung Up." << std::endl;
00105                     break;
00106                 }
00107             } else {
00108                 // nothing read
00109             }
00110             sleep(15);
00111         }
00112 
00113         }
00114 
00115 private:
00116         SocketClient &socket;
00117         TimeStamp timestamp;
00118         bool timestamps;
00119         bool hex;
00120         bool new_line;
00121 };
00122 
00123 class Writer {
00124 public:
00125         Writer(SocketClient &socket_server, bool timestamps_reqd) : socket(socket_server), timestamps(timestamps_reqd) {}
00126 
00127         void loop() {
00128         MilliSleep sleep;
00129         char buffer[256];
00130         memset(buffer,0,256);
00131         char *s_ptr;
00132         while( 1 ) {
00133             if ( fgets(buffer,80,stdin) == NULL ) {
00134                 break;
00135             }
00136             // fgets always terminates with a null character, even if it manages
00137             // to read n-1 chars
00138             s_ptr = buffer;
00139             while ( *s_ptr != '\0') { ++s_ptr; }
00140             if ( timestamps ) { timestamp.stamp(); }
00141             socket.write(buffer,s_ptr - buffer);
00142             if ( timestamps ) {
00143                 std::cout << "[" << timestamp << "] : ";
00144             }
00145             std::cout << buffer; // already terminated by an endl;
00146             std::cout.flush();
00147             sleep(500);
00148         }
00149 
00150         }
00151 
00152 private:
00153         SocketClient &socket;
00154         TimeStamp timestamp;
00155         bool timestamps;
00156 };
00157 
00158 } // namespace demos
00159 } // namespace ecl
00160 
00161 /*****************************************************************************
00162 ** Using
00163 *****************************************************************************/
00164 
00165 using namespace ecl::demos;
00166 
00167 /*****************************************************************************
00168 ** Main
00169 *****************************************************************************/
00170 
00171 int main(int argc, char** argv) {
00172 
00173     std::cout << std::endl;
00174     std::cout << "***********************************************************" << std::endl;
00175     std::cout << "                 Parsing Command Line" << std::endl;
00176     std::cout << "***********************************************************" << std::endl;
00177     std::cout << std::endl;
00178 
00179     int port = 0;
00180     string hostname;
00181     bool hex(false);
00182     bool timestamps(false);
00183 
00184     try {
00185         CmdLine cmd("This is a simple interface for making a socket client connection.",' ',"0.1");
00186         ValueArg<string> arg_hostname("n","hostname","Hostname of the server [localhost]",false,"localhost","string");
00187         ValueArg<int> arg_port("p","port","Port number to connect to [1470].",false,1470,"integer");
00188         SwitchArg switch_timestamps("t","timestamp","Timestamp incoming/outgoings.",false);
00189         SwitchArg switch_hex("x","hex","Enable hex output.",false);
00190 
00191         cmd.add(arg_hostname);
00192         cmd.add(arg_port);
00193         cmd.add(switch_hex);
00194         cmd.add(switch_timestamps);
00195         cmd.parse(argc,argv);
00196 
00197         hostname = arg_hostname.getValue();
00198         port = arg_port.getValue();
00199         hex = switch_hex.getValue();
00200         timestamps = switch_timestamps.getValue();
00201 
00202     } catch ( ArgException &e ) {
00203         std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl;
00204     }
00205 
00206     std::cout << "Connecting to: " << hostname << " [" << port << "]" << std::endl;
00207 
00208     std::cout << std::endl;
00209     std::cout << "***********************************************************" << std::endl;
00210     std::cout << "                   Read and Write Threads   " << std::endl;
00211     std::cout << "***********************************************************" << std::endl;
00212     std::cout << std::endl;
00213 
00214     SocketClient client(hostname,port);
00215     Writer writer(client,timestamps);
00216     Reader reader(client,timestamps,hex);
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 
00224     std::cout << std::endl;
00225     std::cout << "***********************************************************" << std::endl;
00226     std::cout << "                      Passed" << std::endl;
00227     std::cout << "***********************************************************" << std::endl;
00228     std::cout << std::endl;
00229 
00230         return 0;
00231 }
00232 
00233 #endif /* ECL_IS_POSIX */
00234 #endif  /* !ECL_IS_MAC */
00235 
00236 #if !defined ECL_IS_POSIX || ECL_IS_MAC
00237 
00238 #include <iostream>
00239 
00240 int main(int argc, char **argv) {
00241 
00242         std::cout << "This is a posix (not mac) only app." << std::endl;
00243         return 0;
00244 }
00245 
00246 #endif


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