-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathWebHaosou.cpp
115 lines (88 loc) · 2.48 KB
/
WebHaosou.cpp
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
#include "StdAfx.h"
#include "WebHaosou.h"
#include <network/HttpImplement.h>
#include <json/json.h>
#include <util/stdstringextend.h>
using namespace network;
CWebHaosou::CWebHaosou(void)
{
m_hSemaphore = CreateSemaphore(NULL, 0, 1000, NULL);
CSubThread::StartThread();
}
CWebHaosou::~CWebHaosou(void)
{
CSubThread::StopThread();
CSubThread::WaitForSubThreadExit();
}
STDMETHODIMP_(CLSID) CWebHaosou::GetClsid()
{
return CLSID_WebHaosou;
}
STDMETHODIMP CWebHaosou::SetNotify(ISWebNotify* pNotify)
{
RASSERT( pNotify , E_INVALIDARG);
m_pSwebNotify = pNotify;
return m_pSwebNotify ? S_OK : E_INVALIDARG;
}
STDMETHODIMP CWebHaosou::GetSuggest(LPCWSTR lpszSuggest)
{
RASSERT( m_pSwebNotify, E_FAIL);
RASSERT( lpszSuggest && lpszSuggest[0], E_INVALIDARG);
m_strText = lpszSuggest;
if ( !ReleaseSemaphore(m_hSemaphore, 1, NULL) )
return E_FAIL;
return S_OK;
}
HRESULT CWebHaosou::Run()
{
HANDLE hHandles[] = { m_hExit, m_hSemaphore};
for ( ; ; )
{
DWORD dwWait = WaitForMultipleObjects(_countof(hHandles), hHandles, FALSE, INFINITE);
switch( dwWait )
{
case WAIT_OBJECT_0://m_hExit
{
GrpMsg(GroupName, MsgLevel_Msg, _T("CWebBaidu Run Exit"));
return S_OK;
}
case WAIT_OBJECT_0 + 1://hExp
{
if ( m_pSwebNotify )
m_pSwebNotify->OnSWebNotify_Msg( SWEB_MSG_BEGIN, NULL);
CUrlParamValueMap valueMap;
valueMap["word"] = m_strText.GetBuffer();
CHttpImplement httpImpl;
DWORD dwRet = httpImpl.GetRequest("sug.so.360.cn", 80, "suggest",valueMap);
if ( dwRet )
{
if ( m_pSwebNotify )
m_pSwebNotify->OnSWebNotify_Msg( SWEB_MSG_BEGIN, NULL);
break;
}
std::string sValue = (char*)httpImpl.GetRequestData();
sValue = sValue.substr(sValue.find('[') + 1);
sValue = sValue.erase(sValue.find(']'));
sValue = strext::replace<char>(sValue.c_str(), "\"", "").c_str();
std::vector<std::string> sVect = strext::split<char>(sValue.c_str(), ",");
UTIL::com_ptr<IProperty2> pProp;
m_pRot->CreateInstance(CLSID_CProperty2, NULL, __uuidof(IProperty2), (void**)&pProp);
if ( !pProp )
{
if ( m_pSwebNotify )
m_pSwebNotify->OnSWebNotify_Msg( SWEB_MSG_BEGIN, NULL);
break;
}
CPropSet propSet(pProp);
for ( int nLoop = 0 ; nLoop < sVect.size() ; nLoop++ )
propSet[nLoop] = sVect[nLoop].c_str();
if ( m_pSwebNotify )
{
m_pSwebNotify->OnSWebNotify_Suggest(this, sVect.size(), pProp);
m_pSwebNotify->OnSWebNotify_Msg( SWEB_MSG_DONE, NULL);
}
break;
}
}
}
}