// clockclient.cc include <net/cs/csclient.h> include <net/cs/tcp/client.h> include <net/cs/samlib.h> include <iostream.h> include <unistd.h> include <stdlib.h> include <hush/hush.h> include <hush/session.h> include <hush/event.h> include <widgets/button.h> const char* cmd_disconnect = "Disconnect"; const char* cmd_connect = "Connect"; //------------------------- definition ---------------------------- class beatbutton; class c_misc_handler;
class app: public session { public: app(int argc, char* argv[]); int main(); void unbind(); void disconnect(); void connect(); protected: c_misc_handler* misc_handler_; tcp_client* pclient_;
class button* b_tick_;
class button* b_disconnect_;
class button* b_connect_; };
class beatbutton: public button { public: beatbutton(app* owner, csclient* c); int operator()(); protected: void process_data(); app* owner_; csclient* pclient_; };
class c_misc_handler: public handler { public: c_misc_handler(app* owner); int operator()(); protected: app* owner_; };
//------------------------- implementation ---------------------------- //---- beatbutton ---- beatbutton::beatbutton(app* owner, csclient* c) : button(".beat") { tk -> eval("image create photo .beat_small -file beat_small.gif"); tk -> eval("image create photo .beat_big -file beat_big.gif"); pclient_ = c; owner_ = owner; image(".beat_small"); tk -> bind(pclient_ -> fd(), this); } void beatbutton::process_data() { char buf[128]; int count; pclient_ -> readmsg(buf, sizeof(buf)); if (!pclient_ -> connected()) { cout << "server has closed connection" << endl; owner_ -> unbind(); return; } sscanf(buf, "%d", &count); if (count % 24 == 0) { image(".beat_big"); tk -> update(); } else if (count % 24 == 6) { image(".beat_small"); tk -> update(); } } int beatbutton::operator()() { int fd = _event->fd(); int mask = _event->mask(); cerr << "beatbutton :" << mask << endl; switch(mask) { case kit::writable: // so that you may read if (fd == pclient_ -> fd()) process_data(); else cout << "Not from my fd" << endl; break; case kit::readable: // so that you may write // whatever you want to write to fd; cerr << "beat write" << endl; break; case kit::exception: // oops ... error? cerr << "beat exception" << endl; break; } return OK; } //---- c_misc_handler ---- c_misc_handler::c_misc_handler(app* owner) { owner_ = owner; } int c_misc_handler::operator()() { int i; printf("c_misc_handler::operator()\n"); for (i = 0; i < _event -> argc(); i++) printf(" arg %d = %s\n", i, _event -> arg(i)); if (strcmp(_event -> arg(1), cmd_disconnect) == 0) owner_ -> disconnect(); else if (strcmp(_event -> arg(1), cmd_connect) == 0) owner_ -> connect(); else warn("c_misc_handler::operator() : unknown command string %s", _event -> arg(1)); return OK; } //---- app ---- app::app(int argc, char* argv[]) : session(0, 0,"clock") { int i; //tk -> trace(); for (i = 0; i < argc; i++) cout << "argv[" << i << "] = " << argv[i] << endl; pclient_ = new tcp_client(argv[1], atoi(argv[2])); cout << "Client ready ... " << endl; } int app::main() { misc_handler_ = new c_misc_handler(this); tk->trace(); b_tick_ = new beatbutton(this, pclient_); tk -> pack(b_tick_, "-side left"); b_disconnect_ = new button(".disconnect", "-text disconnect"); b_disconnect_ -> bind(misc_handler_, cmd_disconnect); tk -> pack(b_disconnect_, "-side left"); b_connect_ = new button(".connect", "-text connect"); b_connect_ -> bind(misc_handler_, cmd_connect); tk -> pack(b_connect_, "-side left"); tk -> bind(pclient_ -> fd(), b_tick_); return OK; } void app::unbind() { cout << "app::unbind" << endl; exit(1); } void app::connect() { pclient_ -> csclient::connect(); // stupid compiler will not get it // otherwise tk -> bind(pclient_ -> fd(), b_tick_); } void app::disconnect() { tk -> unbind(pclient_ -> fd()); pclient_ -> disconnect(); } //----------------------------- main ----------------------- int main(int argc, char* argv[]) { app a(argc, argv); a.run(); return 0; }
Hush Online Technology
hush@cs.vu.nl
09/09/98 |
![]() |
![]() |