#include <unistd.h> // sleep()

#include <iostream>

#include "odai.h"

// callback function for receiving the message "message(i, f, s)"
static void
message(int i, double f, char *s) {
   cout << "MESSAGE: ";
   cout << "string="  << s << ", ";
   cout << "integer=" << i << ", ";
   cout << "float="   << f << endl << endl;
}

int
main(int argc, char **argv) {
   char *t = "abcdefghijklmnopqrstuvwxyz";
   Client *c = NULL;
   double f;
   char *s;
   int i;

   try {
      c = new Client("localhost", 3000);
   } catch (int err) {
      cerr << "client: fatal error " << err << endl;
      exit(1);
   }

   // make function a callback for a message
   c->reg("message(i, f, s)", (void *)message);

   // call every second a function in the sever process
   while (true) {
      sleep(1);
      cout << "calling ret_void() ... "; flush(cout);
      if (!c->fnc("ret_void(s)", t)) {
         cout << "OK" << endl;
      }
      sleep(1);
      cout << "calling ret_int() ... "; flush(cout);
      if (!c->fnc(&i, "ret_int(s)", t)) {
         cout << "OK, got: " << i << endl;
      }
      sleep(1);
      cout << "calling ret_float() ... "; flush(cout);
      if (!c->fnc(&f, "ret_float(s)", t)) {
         cout << "OK, got: " << f << endl;
      }
      sleep(1);
      cout << "calling ret_string() ... "; flush(cout);
      if (!(c->fnc(&s, "ret_string(s)", t))) {
         cout << "OK, got: " << s << endl << endl;
      }
   }

   delete (c);

   return (0);
}