Exemple #1
0
int rhoPlainLogData(const char* file, int line, LogSeverity severity, const char* szCategory,
				const void* data, int len ){
		
	rho::LogMessage oMsg(file, line, severity, LOGCONF(), rho::LogCategory(szCategory) );
	oMsg.addRawString( static_cast<const char*>(data),len);	
	return 1;
}
/**
 * Test try receive functionality
 */
TEST_F(TypedSendReceiveSocketTest, TestTryReceive)
{
	VmsTypedSendSocket<TestMsg> *pSender = m_pSocketFactory->CreateTypedSendSocket<TestMsg>(STR_ADDRESS);
	VmsTypedReceiveSocket<TestMsg> *pReceiver = m_pSocketFactory->CreateTypedReceiveSocket<TestMsg>(STR_ADDRESS, new TestMsg::TCreator);

	const int iWaitMsecs = 5;
	VistaTimer oTimer;
	VistaType::microtime tStart= oTimer.GetMicroTime();
	TestMsg *pWhatIHeard = pReceiver->TryReceive(iWaitMsecs);
	VistaType::microtime tEnd= oTimer.GetMicroTime();

	EXPECT_TRUE(pWhatIHeard == NULL);
	EXPECT_GE(1000.0*(tEnd-tStart), static_cast<double>(iWaitMsecs));

	TestMsg oMsg("Hello World!");
	pSender->Send(&oMsg);

	tStart= oTimer.GetMicroTime();
	pWhatIHeard = pReceiver->TryReceive(iWaitMsecs);
	tEnd= oTimer.GetMicroTime();

	EXPECT_TRUE(pWhatIHeard != NULL);
	EXPECT_LE(1000.0*(tEnd-tStart), static_cast<double>(iWaitMsecs));
	if(pWhatIHeard != NULL)
		ASSERT_EQ(oMsg.GetMsgText(), pWhatIHeard->GetMsgText());

	delete pReceiver;
	delete pSender;
}
void
BF_GUI_TopVMenu::KeyDown(const char *bytes, int32 numBytes)
{
	/* handle keys */
	if(numBytes==1 && (bytes[0]==B_RIGHT_ARROW || bytes[0]==B_LEFT_ARROW)){
		BMessage oMsg(BF_MSG_TOPMENU_HCLOSE);
		oMsg.AddInt8("key",(const int8)bytes[0]);
		BMessenger oMessenger(poHMenu);
		oMessenger.SendMessage(&oMsg);
	}else
		BF_GUI_DlgView_VMenu::KeyDown(bytes,numBytes);
}											
/**
 * Test simple ping-pong between the socket pair
 */
TEST_F(TypedSendReceiveSocketTest, TestConnection)
{
	VmsTypedSendSocket<TestMsg> *pSender = m_pSocketFactory->CreateTypedSendSocket<TestMsg>(STR_ADDRESS);
	VmsTypedReceiveSocket<TestMsg> *pReceiver = m_pSocketFactory->CreateTypedReceiveSocket<TestMsg>(STR_ADDRESS, new TestMsg::TCreator);

	TestMsg oMsg("Hello World!");
	pSender->Send(&oMsg);

	TestMsg *pWhatIHeard = pReceiver->Receive();

	EXPECT_TRUE(pWhatIHeard != NULL);
	if(pWhatIHeard != NULL)
		ASSERT_EQ(oMsg.GetMsgText(), pWhatIHeard->GetMsgText());

	delete pReceiver;
	delete pSender;
}
Exemple #5
0
// Uploads the PNG file to Gyazo
BOOL uploadFile(HWND hwnd, LPCTSTR fileName, BOOL isPng)
{
	const int nSize = 256;
	LPCTSTR DEFAULT_UPLOAD_SERVER = _T("upload.gyazo.com");
	LPCTSTR DEFAULT_UPLOAD_PATH   = _T("/upload.cgi");
	LPCTSTR DEFAULT_UPLOAD_TOKEN = _T("");
	//LPCTSTR DEFAULT_USER_AGENT    = _T("User-Agent: Gyazowin/1.0\r\n");
	const int DEFAULT_UPLOAD_SERVER_PORT = INTERNET_DEFAULT_HTTP_PORT;

	TCHAR upload_server[nSize];
	TCHAR upload_path[nSize];
	TCHAR upload_token[nSize];
	//TCHAR ua[nSize];
	lstrcpy(upload_server, DEFAULT_UPLOAD_SERVER);
	lstrcpy(upload_path, DEFAULT_UPLOAD_PATH);
	lstrcpy(upload_token, DEFAULT_UPLOAD_TOKEN);
	//lstrcpy(ua, DEFAULT_USER_AGENT);
	int upload_server_port = DEFAULT_UPLOAD_SERVER_PORT;

	TCHAR runtime_path[MAX_PATH+1];
	TCHAR runtime_dirname[MAX_PATH+1];
	TCHAR config_file[MAX_PATH+1];
	if (0 != ::GetModuleFileName(NULL, runtime_path, MAX_PATH)) {
		TCHAR tmp[MAX_PATH+1];
		_tsplitpath_s(runtime_path, tmp, runtime_dirname, tmp, tmp);
	}
	lstrcpy(config_file, runtime_dirname);
	lstrcat(config_file, _T("\\gyazo.ini"));
	if (PathFileExists(config_file)) {
		LPCTSTR SECTION_NAME = _T("gyazo");
		GetPrivateProfileString(SECTION_NAME, _T("server"), DEFAULT_UPLOAD_SERVER, upload_server, sizeof(upload_server), config_file);
		GetPrivateProfileString(SECTION_NAME, _T("path"), DEFAULT_UPLOAD_PATH, upload_path, sizeof(upload_path), config_file);
		GetPrivateProfileString(SECTION_NAME, _T("token"), DEFAULT_UPLOAD_TOKEN, upload_token, sizeof(upload_token), config_file);
		//GetPrivateProfileString(SECTION_NAME, _T("user_agent"), DEFAULT_USER_AGENT, ua, sizeof(ua), config_file);
		upload_server_port = GetPrivateProfileInt(SECTION_NAME, _T("port"), DEFAULT_UPLOAD_SERVER_PORT, config_file);
	}

	const char*  sBoundary = "----BOUNDARYBOUNDARY----";		// boundary
	const char   sCrLf[]   = { 0xd, 0xa, 0x0 };					// 改行(CR+LF)
	TCHAR szHeader[200];

	StringCchPrintf(szHeader, 200, TEXT("Auth-Token: %s\r\nContent-type: multipart/form-data; boundary=----BOUNDARYBOUNDARY----"), upload_token);

	std::ostringstream	buf;	// 送信メッセージ

	wchar_t fname[_MAX_FNAME];
	wchar_t ext[_MAX_EXT];
	_wsplitpath(fileName, NULL, NULL, fname, ext );
	std::string data = (isPng) ? "imagedata" : "data";
	LPCTSTR file = (isPng) ? _T("gyazo") : wcsncat(fname, ext, _MAX_FNAME);
	size_t size = wcstombs(NULL, file, 0);
	char* CharStr = new char[size + 1];
	wcstombs(CharStr, file, size + 1);

	// -- "imagedata" part
	buf << "--";
	buf << sBoundary;
	buf << sCrLf;
	buf << "content-disposition: form-data; name=\"";
	buf << data;
	buf << "\"; filename=\"";
	buf << CharStr;
	buf << "\"";
	buf << sCrLf;
	//buf << "Content-type: image/png";	// 一応
	//buf << sCrLf;
	buf << sCrLf;

	// 本文: PNG ファイルを読み込む
	std::ifstream png;
	png.open(fileName, std::ios::binary);
	if (png.fail()) {
		MessageBox(hwnd, _T("PNG open failed"), szTitle, MB_ICONERROR | MB_OK);
		png.close();
		return FALSE;
	}
	buf << png.rdbuf();		// read all & append to buffer
	png.close();

	// 最後
	buf << sCrLf;
	buf << "--";
	buf << sBoundary;
	buf << "--";
	buf << sCrLf;

	// メッセージ完成
	std::string oMsg(buf.str());

	// WinInet を準備 (proxy は 規定の設定を利用)
	HINTERNET hSession    = InternetOpen(_T("Gyazowin/1.0"), 
		INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
	if(NULL == hSession) {
		LastErrorMessageBox(hwnd, _T("Cannot configure wininet."));
		return FALSE;
	}
	
	// 接続先
	HINTERNET hConnection = InternetConnect(hSession, 
		upload_server, upload_server_port,
		NULL, NULL, INTERNET_SERVICE_HTTP, 0, NULL);
	if(NULL == hConnection) {
		LastErrorMessageBox(hwnd, _T("Cannot initiate connection."));
		InternetCloseHandle(hSession);
		return FALSE;
	}

	// 要求先の設定
	HINTERNET hRequest    = HttpOpenRequest(hConnection,
		_T("POST"), upload_path, NULL,
		NULL, NULL, INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_RELOAD, NULL);
	if(NULL == hRequest) {
		LastErrorMessageBox(hwnd, _T("Cannot compose post request."));
		InternetCloseHandle(hConnection);
		InternetCloseHandle(hSession);
		return FALSE;
	}
	
	// 要求を送信
	BOOL bSuccess = FALSE;
	if (HttpSendRequest(hRequest,
                    szHeader,
					lstrlen(szHeader),
                    (LPVOID)oMsg.c_str(),
					(DWORD) oMsg.length()))
	{
		// 要求は成功
		
		DWORD resLen = 8;
		TCHAR resCode[8];

		// status code を取得
		if(!HttpQueryInfo(hRequest, HTTP_QUERY_STATUS_CODE, resCode, &resLen, 0))
		{
			LastErrorMessageBox(hwnd, _T("Cannot get status code."));
			InternetCloseHandle(hRequest);
			InternetCloseHandle(hConnection);
			InternetCloseHandle(hSession);
			return FALSE;
		}

		// 結果 (URL) を読取る
		DWORD len;
		char  resbuf[1024];
		std::string result;

		// そんなに長いことはないけどまあ一応
		while (InternetReadFile(hRequest, (LPVOID)resbuf, 1024, &len)
			&& len != 0)
		{
			result.append(resbuf, len);
		}

		// 取得結果は NULL terminate されていないので
		result += '\0';

		if( _ttoi(resCode) != 200 ) {
			// upload 失敗 (status error)
			TCHAR errorBuf[200];
			StringCchPrintf(errorBuf, 200, TEXT("Cannot upload the image. Error %s "),resCode);
			std::wstring stemp = std::wstring(result.begin(), result.end());
			StringCchCat(errorBuf, 200, (LPTSTR)stemp.c_str());
			MessageBox(hwnd, errorBuf, szTitle, MB_ICONERROR | MB_OK);
		} else {
			// upload succeeded

			// クリップボードに URL をコピー
			setClipBoardText(result.c_str());
			
			// URL を起動
			execUrl(result.c_str()); 

			bSuccess = TRUE;
		}
	} else {
		// アップロード失敗...
		LastErrorMessageBox(hwnd, _T("Cannot connect to the server."));
	}

	// ハンドルクローズ
	InternetCloseHandle(hRequest);
	InternetCloseHandle(hConnection);
	InternetCloseHandle(hSession);

	return bSuccess;

}
void VmsSimpleIsoServer::Run()
{
	if(m_strServiceSocketName.empty() || m_strDataFileName.empty())
		return;

	//load data
	vtkDataSetReader *pReader = vtkDataSetReader::New();
	vstr::out() << "Reading data..." << std::endl;
	pReader->SetFileName(m_strDataFileName.c_str());
	pReader->Update();
	vstr::out() << "\tDONE!" << std::endl;

	vtkContourFilter *pFilter = vtkContourFilter::New();
	pFilter->SetInputConnection(pReader->GetOutputPort());

	vstr::out() << "Entering server loop" << std::endl;

	//cache the message tags for easy access
	const std::type_info& TERMINATE_TAG = typeid(VmsSimpleIsoVocabulary::TerminateMsg);
	const std::type_info& UPDATE_ISO_TAG = typeid(VmsSimpleIsoVocabulary::RequestIsosurfaceMsg);
	
	//enter service loop:
	//as long as there are new isovalues ==> we'll answer with the corresponding surface
	while(true)
	{
		IVistaSerializable *pMsg = m_pServiceSocket->ReceiveRequest();
		if(pMsg == NULL)
		{
			vstr::warnp() << "Intercepted invalid or empty message!" << std::endl;
			continue;
		}	
		
		//we have nothing to add -> just ping-pong the message as ack
		m_pServiceSocket->SendAck(pMsg);
		
		const std::type_info& rMsgType = typeid(*pMsg);

		if(rMsgType == TERMINATE_TAG)
		{
			delete pMsg;
			break;
		}
		else if(rMsgType == UPDATE_ISO_TAG)
		{
			double dIsoVal = static_cast<VmsSimpleIsoVocabulary::RequestIsosurfaceMsg*>(pMsg)->GetIsoVal();
			vstr::out() << "Updating for " << dIsoVal << std::endl;
			pFilter->SetValue(0, dIsoVal);
			pFilter->Update();
			vstr::out() << "\tDONE!" << std::endl;

			VmsSimpleIsoVocabulary::UpdatePolyDataMsg oMsg(pFilter->GetOutput());
			m_pDataSocket->Send(&oMsg);
		}
		else
		{
			vstr::errp() << "Undefined message!" << std::endl;
			vstr::errp() << "typeid name is " << typeid(*pMsg).name() << std::endl;
		}
		
		//clean up properly!
		delete pMsg;
	}
	
	vstr::out() << "Left server loop" << std::endl;

	//cleanup
	pFilter->Delete();
	pReader->Delete();
}