current version
[opsoft_test.git] / gclib2 / unit_tests / connection2.cxx
blob5d1b92fb44ecac4d6f9222b5d7b92aeacc6a0ade
1 /*
2 * (c) Oleg Puchinin 2008
3 * graycardinalster@gmail.com
5 */
7 #include <gclib2.h>
9 int main (int argc, char ** argv)
11 Connection * c;
12 Connection * m_remote;
13 Connection * m_stdin;
15 Buf * b;
16 Poll * m_pull;
17 char m_buf[4096];
18 int count;
21 if (argc != 3) {
22 printf ("usage: connection <IP> <PORT>\n");
23 return EXIT_SUCCESS;
26 c = new Connection;
27 c->init ();
28 c->setName ("remote");
29 m_remote = c;
31 m_stdin = new Connection;
32 m_stdin->setSocket (fileno (stdin));
33 m_stdin->setName ("local");
35 if (c->connect (argv[1], atoi (argv[2])) < 0) {
36 perror ("connect");
37 return EXIT_FAILURE;
40 m_pull = new Poll;
41 m_pull->add (c);
42 m_pull->add (m_stdin);
44 while (true) {
45 count = m_pull->poll (1000);
46 if (count < 0) {
47 perror ("poll");
48 break;
50 if (count == 0)
51 continue;
53 while ((c = m_pull->scan ()) && c) {
54 if (c->ioNRead () == 0)
55 return EXIT_SUCCESS;
57 memset (m_buf, 0, 4096);
58 if (EQ (c->name (), "remote")) {
59 b = c->recv ();
60 if (b)
61 write (1, b->data (), b->len ());
62 } else if (EQ (c->name (), "local")) {
63 c->read (m_buf, c->ioNRead ());
64 chomp (m_buf);
65 strcat (m_buf, "\n");
66 m_remote->send (m_buf, strlen (m_buf));
72 return EXIT_SUCCESS;