summaryrefslogtreecommitdiff
path: root/2005/i/robert/src/proto/test_proto.cc
diff options
context:
space:
mode:
authorschodet2005-04-10 00:29:15 +0000
committerschodet2005-04-10 00:29:15 +0000
commit2092461fd47d0cbab749529ba18ee894e1fe6e50 (patch)
tree12b15364419306915940287c78e377ec2c584eef /2005/i/robert/src/proto/test_proto.cc
parente46b1d36099f8792f88b77ee42730de2ecf93c26 (diff)
test_proto version pas light.
Add: Proto::wait.
Diffstat (limited to '2005/i/robert/src/proto/test_proto.cc')
-rw-r--r--2005/i/robert/src/proto/test_proto.cc95
1 files changed, 82 insertions, 13 deletions
diff --git a/2005/i/robert/src/proto/test_proto.cc b/2005/i/robert/src/proto/test_proto.cc
index c5bcd0d..faec030 100644
--- a/2005/i/robert/src/proto/test_proto.cc
+++ b/2005/i/robert/src/proto/test_proto.cc
@@ -23,37 +23,106 @@
//
// }}}
#include "proto.hh"
+#include "timer/timer.hh"
#include <iostream>
-#include <exception>
+#include <stdexcept>
+#include <algorithm>
+#include <iterator>
-class chier : public Proto::Receiver
+/// Classe de test pour proto.
+class TestProto : public Proto::Receiver
{
- void receive(char command, const Proto::Frame &frame)
+ void receive (char command, const Proto::Frame &frame)
{
- std::cout << "J'ai reçu en commande:" << command << std::endl;
- std::cout << "J'ai reçu en arg:" << std::endl;
- for(std::vector<uint8_t>::const_iterator it = frame.args.begin(); it != frame.args.end(); it++)
- std::cout << static_cast<int>(*it) << std::endl;
+ std::cout << command << ' ';
+ std::copy (frame.args.begin (), frame.args.end (),
+ std::ostream_iterator<int> (std::cout, " "));
+ std::cout << std::endl;
}
};
+/// Affiche un memo de suntaxe.
+void
+syntax (void)
+{
+ std::cout << "test_proto - test la classe de protocol série.\n"
+ "Syntaxe : test_proto <tty> <...>\n"
+ " s <cmd> <args...> envois une commande\n"
+ " w <ms> attend pendant un nombre de millisecondes\n"
+ " ? affiche cet écran d'aide\n"
+ << std::endl;
+}
+
int
main (int argc, char **argv)
{
try
{
- std::cout << "chier partout!!" << std::endl;
+ int i;
+ if (argc < 2)
+ {
+ syntax ();
+ return 1;
+ }
+ TestProto testProto;
+ Proto proto (testProto);
+ proto.open (argv[1]);
+ i = 2;
+ while (i < argc)
+ {
+ bool reliable = true;
+ switch (argv[i][0])
+ {
+ case 'S':
+ reliable = false;
+ // no break;
+ case 's':
+ {
+ if (i + 2 >= argc)
+ throw std::runtime_error ("syntax error");
+ unsigned a;
+ int arg[4];
+ char c = argv[++i][0];
+ const char *format = argv[++i];
+ if (i + static_cast<int> (strlen (format)) >= argc)
+ throw std::runtime_error ("syntax error");
+ for (a = 0; a < 4 && a < strlen (format); a++)
+ arg[a] = atoi (argv[++i]);
+ proto.send (c, format, arg[0], arg[1], arg[2], arg[3],
+ reliable);
+ }
+ break;
+ case 'w':
+ {
+ int stop, t;
+ if (i + 1 >= argc)
+ throw std::runtime_error ("syntax error");
+ stop = atoi (argv[++i]) + Timer::getProgramTime ();
+ t = Timer::getProgramTime ();
+ while (t < stop)
+ {
+ std::cout << std::endl;
+ proto.wait (stop - t);
+ t = Timer::getProgramTime ();
+ }
+ break;
+ }
+ case '?':
+ proto.close ();
+ syntax ();
+ return 0;
+ default:
+ throw std::runtime_error ("syntax error");
+ }
+ i++;
+ }
}
catch (const std::exception &e)
{
std::cerr << e.what () << std::endl;
+ syntax ();
return 1;
}
-
- chier ch;
- Proto chieri(ch);
- chieri.open("-");
- chieri.send('a', "bb", 3, 4);
return 0;
}