📄 instance_connectivity.cpp
字号:
/*
Copyright (c) 2008, Intel Corporation.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
* Neither the name of Intel Corporation nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*///==============================================================================// Instance_Connectivity.cpp//// Implement the connectivity APIs////// //==============================================================================//#include "stdafx.h"//#include <Iphlpapi.h>#include <algorithm>#include "Instance_Connectivity.h"#include "mediaconnect.h"#include "inc/network/network_NetworkAdapterClassObject.h"#include "inc/network/network_NetworkAdapterInstanceObject.h"#include "Connectivity_core_utils.h"#include "getroute.h"// error codes #define CCONNECTIVITY_SUCCESS 0 #define CCONNECTIVITY_FAIL -1 #define CCONNECTIVITY_TO_START_NETWORK -2//==============================================================================//// ConnectivityInstanceObject implementation////==============================================================================bool ConnectivityInstanceObject::Initialize( const IntelMobileString& szKey ){//#ifdef _DEBUG _Log(IntelMobileText("ConnectivityInstanceObject::Initialize(%ls)"), szKey.c_str());//#endif m_sKey = szKey; m_pLayerManger = GetLayer(); // TODO return true;}//==============================================================================//==============================================================================//// Connectivity Methods////==============================================================================//==============================================================================bool ConnectivityInstanceObject::IsReachable( const IntelMobileString& UriDestination ){ _Log(IntelMobileText("ConnectivityInstanceObject::IsReachable(%ls)"), UriDestination.c_str());// USES_CONVERSION; bool bRet; bool bIsReachable; unsigned int TotalTime; char buf[1000];#ifdef _UNICODE wcstombs(buf, UriDestination.c_str(), 1000);#else strncpy(buf, UriDestination.c_str(), 1000);#endif bRet = ::IsReachable(&bIsReachable, &TotalTime, buf ); if ( !bRet || !bIsReachable ) bIsReachable = false; else bIsReachable = true; return bRet;}//bool ConnectivityInstanceObject::IsReachableEx( const IntelMobileString& UriDestination,const IntelMobileString& ProxyURL,IntelMobileString& ProxyUsername, IntelMobileString& ProxyPassword,IntelMobileUInt TimeOut,IntelMobileUInt RetryCount,IntelMobileString NetworkAdaptorKey){// USES_CONVERSION; bool bRet; bool bIsReachable; unsigned int TotalTime; Lock(); bRet = ::IsReachableEx(&bIsReachable,&TotalTime,(byte*)(UriDestination.c_str()),(byte*)(ProxyURL.c_str()),ProxyUsername,ProxyPassword, &TimeOut, &RetryCount, NetworkAdaptorKey ); if ( !bRet || !bIsReachable ) bIsReachable = false; else bIsReachable = true; Unlock(); return bIsReachable;}bool ConnectivityInstanceObject::GetNetworkAdapterKey( const IntelMobileString& UriDestination, IntelMobileString* pNetworkAdapter ){ _Log(IntelMobileText("ConnectivityInstanceObject::GetNetworkAdapterKey(%ls, %ls)"), UriDestination.c_str(), pNetworkAdapter->c_str()); try {// USES_CONVERSION; if(IsReachable(UriDestination)) { ServerNetworkAdapterInstanceObject *pNetworkAdapterInstance; IPAddr ipAddress = NULL; unsigned long iRet = 0; unsigned long IndexofInterface = 0; //struct hostent *pHostIP; URI_INFO UriInfo; IntelMobileString pBestLocalIP; char buf[256];#ifdef _UNICODE wcstombs(buf, UriDestination.c_str(), 256);#else strncpy(buf, UriDestination.c_str(), 256);#endif //iRet =ParseURI((byte*)T2CA(UriDestination.c_str()),&UriInfo); iRet =ParseURI(buf,&UriInfo); if(iRet < 0) { return 0; } //GetBestLocalIP(&UriInfo, pBestLocalIP); /* pHostIP = gethostbyname(UriInfo.DestPath.c_str()); if(pHostIP) { unsigned long SizeofIP = pHostIP->h_length; memcpy(&ipAddress,pHostIP->h_addr_list,SizeofIP); }else { URI_INFO tmpUriInfo;*/ // ParseURI((byte*)T2A(UriDestination.c_str()),&tmpUriInfo); //ipAddress=UriInfo.IPAddr; //} //find the interface which the dest ip routes from /* iRet = GetBestInterface(ipAddress, &IndexofInterface); if (iRet < 0) { return 0; } */ /* PIP_INTERFACE_INFO pInfo; pInfo = (IP_INTERFACE_INFO *) malloc( sizeof(IP_INTERFACE_INFO) ); unsigned long ipInfoSize = 0; if ( (iRet = GetInterfaceInfo(pInfo, &ipInfoSize)) == ERROR_INSUFFICIENT_BUFFER) { free(pInfo); pInfo = (IP_INTERFACE_INFO *) malloc (ipInfoSize); } iRet = GetInterfaceInfo(pInfo,&ipInfoSize); if (iRet !=NO_ERROR) return 0; WCHAR pBestInterfaceName[128]; for (long dwIfArray =0; dwIfArray<pInfo->NumAdapters;dwIfArray++) { if (pInfo->Adapter[dwIfArray].Index == IndexofInterface) { memcpy(&pBestInterfaceName,&pInfo->Adapter[dwIfArray].Name,128); break; } } free(pInfo); */ char dstIP[128]; IntelMobileChar bestIf[128]; memset(dstIP, 0, sizeof(dstIP)); memset(bestIf, 0, sizeof(bestIf)); inet_ntop(AF_INET, &UriInfo.IPAddr, dstIP, sizeof(dstIP)); FindBestInterface(dstIP, bestIf); long iChangedIndex = 0; // to act like client int iEnumIndex = 0; ServerInstanceCollection* pNetworkAdapterInstanceCollection = NULL; if (pNetworkAdapterInstanceCollection = ((ServerNetworkAdapterClassObject&)(m_pLayerManger->GetLayerClassObject(IntelMobileText("NetworkAdapter")))).GetInstances()) { pNetworkAdapterInstanceCollection->Reset(&iChangedIndex); bool bHasNext; if ( bHasNext = pNetworkAdapterInstanceCollection->HasNext(iEnumIndex, iChangedIndex )) { while ( bHasNext ) { if ( !pNetworkAdapterInstanceCollection->Next( (ServerInstanceObject**)&pNetworkAdapterInstance, &iEnumIndex, iChangedIndex ) ) break; else { IntelMobileString sInterfaceName = bestIf; //transform(sInterfaceName.begin(),sInterfaceName.end(),sInterfaceName.begin(),tolower); IntelMobileString sNetworkAdapterDeviceId = pNetworkAdapterInstance->DeviceId.GetValue(); //transform(sNetworkAdapterDeviceId .begin(),sNetworkAdapterDeviceId .end(),sNetworkAdapterDeviceId .begin(),tolower); //cout << IntelMobileText("TEST:: ") << sNetworkAdapterDeviceId << endl; if (sInterfaceName.find(sNetworkAdapterDeviceId)!= -1) { *pNetworkAdapter=(const_cast<IntelMobileString &>(pNetworkAdapterInstance->GetKey())); return 1; } else { if (!(bHasNext = pNetworkAdapterInstanceCollection->HasNext( iEnumIndex, iChangedIndex ))) break; } } } } } } else pNetworkAdapter =NULL; return false; } catch(ServerException se) { if (se.IfServerBaseObjectException()) { ServerException nse(IntelMobileText("8004320D")); nse.SetParent( &se ); throw nse; } throw se; } catch(...) { THROWIMEXCEPTION("8004320D"); }}bool ConnectivityInstanceObject::GetNetworkProtocolKey( const IntelMobileString& UriDestination, IntelMobileString* pNetworkProtocol ){ return GetNetworkAdapterKey( UriDestination, pNetworkProtocol); }bool ConnectivityInstanceObject::GetLatency( const IntelMobileString& UriDestination, unsigned int* pLatency ){ _Log(IntelMobileText("ConnectivityInstanceObject::GetLatency(%ls)"), UriDestination.c_str());// USES_CONVERSION; bool bRet; bool bIsReachable; unsigned int TotalTime; char buf[1024];#ifdef _UNICODE wcstombs(buf, UriDestination.c_str(), 1024);#else strncpy(buf, UriDestination.c_str(), 1024);#endif bRet = ::IsReachable(&bIsReachable, &TotalTime, buf); if ( !bRet || !bIsReachable ) bIsReachable = false; else bIsReachable = true; if ( pLatency ) *pLatency = (bIsReachable ? (TotalTime): TIME_OUT); else bRet = false; return bRet;}//bool ConnectivityInstanceObject::GetLatencyEx( const IntelMobileString& UriDestination,IntelMobileUInt* pLatency, const IntelMobileString& ProxyURL,IntelMobileString& ProxyUsername, IntelMobileString& ProxyPassword,IntelMobileUInt TimeOut,IntelMobileUInt RetryCount,IntelMobileString NetworkAdaptorKey){// USES_CONVERSION; bool bRet; bool bIsReachable; unsigned int TotalTime; Lock(); bRet = ::IsReachableEx(&bIsReachable,&TotalTime,(byte*)(UriDestination.c_str()),(byte*)(ProxyURL.c_str()),ProxyUsername,ProxyPassword, &TimeOut, &RetryCount, NetworkAdaptorKey ); if ( !bRet || !bIsReachable ) bIsReachable = false; else bIsReachable = true; if ( pLatency ) *pLatency = (bIsReachable ? (IntelMobileUInt)(TotalTime/1000): TIME_OUT); else bRet = false; Unlock(); return bRet;}bool ConnectivityInstanceObject::GetReliability( const IntelMobileString& NetworkAdapterKey, unsigned int* pReliability ){ // TODO return false;}bool ConnectivityInstanceObject::SetRoute( const IntelMobileString& UriDestination, const IntelMobileString& NetworkAdapterKey ){ // TODO return false;}//==============================================================================//==============================================================================//// Connectivity Properties////==============================================================================//==============================================================================void ConnectivityConnectedClass::GetValueImpl(){//#ifdef _DEBUG _Log(IntelMobileText("ConnectivityConnectedClass::GetValueImpl()"));//#endif int iRet; CMediaConnect cMediaConnect; bool IsNetOn; IsNetOn = false; iRet = CCONNECTIVITY_FAIL; try { iRet = cMediaConnect.IsNetOn(&IsNetOn); } catch(ServerException se) { if (se.IfServerBaseObjectException()) { ServerException nse(IntelMobileText("80043208")); nse.SetParent( &se ); throw nse; } throw se; } catch(...) { THROWIMEXCEPTION("80043208"); } if ( !iRet ) this->SetValueImpl(IsNetOn); else this->SetNull( true );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -