summaryrefslogtreecommitdiff
path: root/test/functional/Test1/TestApp.cpp
blob: 0dcaac5fe40b25f784f5f559fe3f314c8462da52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

// STD
#include <cstdio>

// local
#include "TestApp.h"
#include "TestAppIntro.h"

using namespace std;

DBus::BusDispatcher dispatcher;
TestAppIntro *g_testComIntro;
DBus::Pipe *mTestToDBusPipe;
bool testResult = false;
std::list <std::string> testList;

pthread_mutex_t clientMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t clientCondition = PTHREAD_COND_INITIALIZER;

TestApp::TestApp()
{
  testList.push_back("test1");
  testList.push_back("testByte");

  cout << "initialize DBus..." << endl;
  initDBus();
}

void TestApp::initDBus()
{
  DBus::_init_threading();

  DBus::default_dispatcher = &dispatcher;

  new DBus::DefaultTimeout(100, false, &dispatcher);

  DBus::Connection conn = DBus::Connection::SessionBus();

  TestAppIntro testComIntro(conn, clientCondition, testResult);
  g_testComIntro = &testComIntro;

  cout << "Start server..." << endl;
  TestAppIntroProvider testComProviderIntro(conn, &testComIntro);
  conn.request_name("DBusCpp.Test.Com.Intro");

  mTestToDBusPipe = dispatcher.add_pipe(TestApp::testHandler, NULL);

  cout << "Start client thread..." << endl;
  pthread_create(&testThread, NULL, TestApp::testThreadRunner, &conn);

  dispatcher.enter();

  pthread_join(testThread, NULL);

  cout << "Testresult = " << string(testResult ? "OK" : "NOK") << endl;
}

void *TestApp::testThreadRunner(void *arg)
{
  for (std::list <std::string>::const_iterator tl_it = testList.begin();
       tl_it != testList.end();
       ++tl_it)
  {
    const string &testString = *tl_it;

    cout << "write to pipe" << endl;
    mTestToDBusPipe->write(testString.c_str(), testString.length() + 1);

    struct timespec abstime;

    clock_gettime(CLOCK_REALTIME, &abstime);
    abstime.tv_sec += 1;

    pthread_mutex_lock(&clientMutex);
    if (pthread_cond_timedwait(&clientCondition, &clientMutex, &abstime) == ETIMEDOUT)
    {
      cout << "client timeout!" << endl;
      testResult = false;
    }
    pthread_mutex_unlock(&clientMutex);
  }

  cout << "leave!" << endl;
  dispatcher.leave();

  return NULL;
}

void TestApp::testHandler(const void *data, void *buffer, unsigned int nbyte)
{
  char *str = (char *) buffer;
  cout << "buffer1: " << str << ", size: " << nbyte << endl;

  cout << "run it!" << endl;
  if (string(str) == "test1")
  {
    g_testComIntro->test1();
  }
  else if (string(str) == "testByte")
  {
    g_testComIntro->testByte(4);
  }
}