// 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() { if ( bus ) { bus->stop(); 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); 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; }