summaryrefslogtreecommitdiff
path: root/comIvy/Bus.cpp
blob: 66f2f2c0e76d39bbb6c3ae625a2acf65f3d14b73 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Bus.cpp : implémentation de CBus

#include "stdafx.h"
#include "Bus.h"
#include "Ivy.h"
#include "IvyApplication.h"
#include "Expression.h"


char * ConvBSTR( BSTR str )
{
	char* convstr;
	int len = SysStringLen( str );
	convstr = new char[len+1] ;
	int bytes = WideCharToMultiByte(CP_ACP, 0, str, len, convstr,  len, NULL,NULL);
	convstr[bytes] = '\0';
	if (!bytes) ATLTRACE( "ConvBSR error %d", GetLastError());
	return convstr;
}
BSTR ConvCSTR( const char *str )
{
	BSTR convstr;
	int len = (int)strlen( str )+1;
	convstr = SysAllocStringLen( NULL, len);
	int bytes = MultiByteToWideChar(CP_ACP, 0, str, len, convstr,  len);
	if (!bytes) ATLTRACE( "ConvCSTR error %d", GetLastError());
	return convstr;
}

// CBus
CBus::CBus()
: bus(NULL)
{
	ATLTRACE("CBus created\n");
}
CBus::~CBus()
{
	delete bus;
	bus = NULL;
	ATLTRACE("CBus destroyed\n");
}

STDMETHODIMP CBus::Start(BSTR domain)
{
	char *strdomain = NULL;
	if ( !bus ) return E_FAIL;
	if ( SysStringLen( domain ) )
		strdomain= ConvBSTR(domain);
	if ( bus ) bus->start(strdomain);
	delete strdomain;
	return S_OK;
}

STDMETHODIMP CBus::Stop(void)
{
	if ( !bus ) return E_FAIL;
	bus->stop();
	return S_OK;
}

STDMETHODIMP CBus::Create(BSTR appName, BSTR readyMsg)
{
	char *strname= ConvBSTR(appName);
	char *strready= ConvBSTR(readyMsg);
	bus = new Ivy(strname,strready,this);
	delete strname;
	delete strready;
	return S_OK;
}

STDMETHODIMP CBus::Delete(void)
{
	if ( !bus ) return E_FAIL;
	bus->stop();
	delete bus;
	bus = NULL;
	return S_OK;
}

STDMETHODIMP CBus::Send(BSTR message, SHORT* count)
{
	if ( !bus ) return E_FAIL;
	char *strmessage= ConvBSTR(message);
	*count = bus->SendMsg( strmessage );
	delete strmessage;
	return S_OK;
}

STDMETHODIMP CBus::Bind(BSTR regexp, IExpression** binding)
{
	if ( !bus ) return E_FAIL;
	char* regexpstr = ConvBSTR(regexp);
	*binding = NULL;
	// Note that at this point the ref count for the object is 0.
	HRESULT hRes = CExpression::CreateInstance(binding);
	if ( hRes != S_OK ) return hRes;
	CExpression* bind = (CExpression*)*binding;
	bind->Bind( regexpstr, this );
	delete regexpstr;
	return hRes;
}

void CBus::OnApplicationConnected(IvyApplication * app)
{
	BSTR appname = ConvCSTR( app->GetName() );
	ApplicationConnected(appname);	
}

void CBus::OnApplicationDisconnected(IvyApplication * app)
{
	BSTR appname = ConvCSTR( app->GetName() );
	ApplicationDisconnected(appname);	
}

STDMETHODIMP CBus::GetDomain(BSTR* domain)
{
	*domain = ConvCSTR( bus->GetDomain(NULL) );

	return S_OK;
}