summaryrefslogtreecommitdiff
path: root/IvyTest/ivytest.cpp
blob: 370901e2d3355bc05e06573e32cfc587a7ce2f38 (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
// ivytest.cpp : Defines the entry point for the console application.
//
#include <iostream>

#include <stdlib.h>
using namespace std;

#include "Ivy.h"
#include "IvyApplication.h"

static bool TheGrassIsGreenAndTheWindBlows = true;

class cIvyTranslater : public IvyApplicationCallback 
{   
public:
    cIvyTranslater(void);
	~cIvyTranslater();
protected:
	void OnApplicationConnected   ( IvyApplication *app );
	void OnApplicationDisconnected( IvyApplication *app );
	void HelloCallback  ( IvyApplication *app, int argc, const char **argv );
	void ByeCallback    ( IvyApplication *app, int argc, const char **argv );
	Ivy *bus;
};



cIvyTranslater::cIvyTranslater(void) 
{
    // initialization
	bus = new Ivy( "cIvyTranslater","cIvyTranslater READY",this,FALSE);

    int count;
    count = bus->BindMsg( "^Hello(.*)", BUS_CALLBACK_OF(cIvyTranslater, HelloCallback ));
    count = bus->BindMsg( "^Bye$",      BUS_CALLBACK_OF(cIvyTranslater, ByeCallback ));
    
    bus->start(NULL);
}
cIvyTranslater::~cIvyTranslater(void) 
{
 	bus->stop(); 
	delete bus;
}

void cIvyTranslater::HelloCallback(IvyApplication *app, int argc, const char **argv)
{
    const char* arg = (argc < 1) ? "" : argv[0];
	cout << "cIvyTranslater received msg: Hello'" << arg << "'" << endl;
    bus->SendMsg( "Bonjour%s!", arg );
}

void cIvyTranslater::ByeCallback(IvyApplication *app, int argc, const char **argv)
{
	cout << "cIvyTranslater stops bus" << endl;
    if (bus) {
        TheGrassIsGreenAndTheWindBlows = false;
		// This is causing the fatal bug
//        bus->stop(); 
//        delete bus; // This statement is never reached! Don't know why!
    }
	
}

void cIvyTranslater::OnApplicationConnected(IvyApplication *app)
{
	cout << "cIvyTranslater is ready to accept messages from " << app->GetName() << endl;
}

void cIvyTranslater::OnApplicationDisconnected(IvyApplication *app)
{
	cout << "cIvyTranslater good buy '" << app->GetName() << "'" << endl;
}


void main(int argc, char* argv[]) 
{
    cIvyTranslater aIvyTL;

    while (TheGrassIsGreenAndTheWindBlows) {
        Sleep(2000);
        cout << "new cycle..." << endl;
    }
	cout << "Good buy, world\n";
}