// ShapeDemoView.cpp : CShapeDemoView 类的实现
//
#include "stdafx.h"
#include "ShapeDemo.h"
#include "ShapeDemoDoc.h"
#include "ShapeDemoView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
//#include "dds/DdsDcpsInfrastructureC.h"
DDS::DomainId_t g_domainId = 255;
const char * g_topicname = "Sharp";
int g_shap_size = 15;
// CShapeDemoView
IMPLEMENT_DYNCREATE(CShapeDemoView, CFormView)
BEGIN_MESSAGE_MAP(CShapeDemoView, CFormView)
ON_COMMAND(ID_PUB, &CShapeDemoView::OnPub)
ON_COMMAND(ID_SUB, &CShapeDemoView::OnSub)
ON_WM_TIMER()
ON_WM_SIZE()
// ON_WM_CLOSE()
ON_WM_DESTROY()
END_MESSAGE_MAP()
// CShapeDemoView 构造/析构
CShapeDemoView::CShapeDemoView()
: CFormView(CShapeDemoView::IDD)
{
// TODO: 在此处添加构造代码
m_pdpf = NULL;
m_pdp = NULL;
m_pservant = NULL;
m_pub_shape = NULL;
m_sub_shape = NULL;
m_timer = NULL;
m_xspeed = 6;
m_yspeed = 6;
m_max_with = 0;
m_max_height = 0;
}
CShapeDemoView::~CShapeDemoView()
{
ShutDown();
}
void CShapeDemoView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
}
BOOL CShapeDemoView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此处通过修改
// CREATESTRUCT cs 来修改窗口类或样式
return CFormView::PreCreateWindow(cs);
}
void CShapeDemoView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
}
void CShapeDemoView::OnRButtonUp(UINT nFlags, CPoint point)
{
ClientToScreen(&point);
OnContextMenu(this, point);
}
void CShapeDemoView::OnContextMenu(CWnd* pWnd, CPoint point)
{
}
// CShapeDemoView 诊断
#ifdef _DEBUG
void CShapeDemoView::AssertValid() const
{
CFormView::AssertValid();
}
void CShapeDemoView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CShapeDemoDoc* CShapeDemoView::GetDocument() const // 非调试版本是内联的
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CShapeDemoDoc)));
return (CShapeDemoDoc*)m_pDocument;
}
#endif //_DEBUG
bool CShapeDemoView::init()
{
if ((!CORBA::is_nil(m_pdpf)) || (!CORBA::is_nil(m_pdp)))
{
return false;
}
ACE::init();
if (CORBA::is_nil(m_pdpf))
{
int argcValue = 3;
char*argvValue[3] = {NULL};
argvValue[0] = "ShapeDemo.exe";
argvValue[1] = "-DCPSConfigFile";
argvValue[2] = "dds_rtps_dis_conf.ini";
m_pdpf = TheParticipantFactoryWithArgs(argcValue, argvValue);
if (CORBA::is_nil(m_pdpf))
{
return false;
}
if (CORBA::is_nil(m_pdp))
{
m_pdp = m_pdpf->create_participant(g_domainId,
PARTICIPANT_QOS_DEFAULT,
DDS::DomainParticipantListener::_nil(),
::OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (CORBA::is_nil(m_pdp))
{
return false;
}
}
if (CORBA::is_nil(m_pservant))
{
m_pservant = new ShapeTypeSupportImpl();
if (DDS::RETCODE_OK != m_pservant->register_type(m_pdp, "Sample"))
{
return false;
}
}
}
return true;
}
bool CShapeDemoView::CreatePublisher()
{
if ((CORBA::is_nil(m_pdp))||(CORBA::is_nil(m_pservant)))
{
return false;
}
if (m_pub_shape)
{
return false;
}
m_pub_shape = new Shape;
m_pub_shape->xpos = m_max_with/2;
m_pub_shape->ypos = m_max_height/2;
CORBA::String_var type_name = m_pservant->get_type_name();
DDS::Topic_var topic = m_pdp->create_topic (g_topicname,
type_name.in(),
TOPIC_QOS_DEFAULT,
DDS::TopicListener::_nil(),
::OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (CORBA::is_nil (topic.in ()))
{
return false;
}
DDS::Publisher_var pub =
m_pdp->create_publisher(PUBLISHER_QOS_DEFAULT,
DDS::PublisherListener::_nil(),
::OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (CORBA::is_nil (pub.in()))
{
return false;
}
DDS::DataWriter_var dw = pub->create_datawriter(topic.in(),
DATAWRITER_QOS_DEFAULT,
DDS::DataWriterListener::_nil(),
::OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (CORBA::is_nil (dw.in ())) {
return false;
}
m_pwrite = ShapeDataWriter::_narrow(dw.in());
if (CORBA::is_nil(m_pwrite))
{
return false;
}
return true;
}
bool CShapeDemoView::CreateSubscriber()
{
if ((CORBA::is_nil(m_pdp))||(CORBA::is_nil(m_pservant)))
{
return false;
}
if (m_sub_shape)
{
return false;
}
CORBA::String_var type_name = m_pservant->get_type_name();
DDS::Topic_var topic = m_pdp->create_topic (g_topicname,
type_name.in(),
TOPIC_QOS_DEFAULT,
DDS::TopicListener::_nil(),
::OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (CORBA::is_nil (topic.in ())) {
return false;
}
DDS::Subscriber_var sub =
m_pdp->create_subscriber(SUBSCRIBER_QOS_DEFAULT,
DDS::SubscriberListener::_nil(),
::OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (CORBA::is_nil (sub.in()))
{
return false;
}
DDS::DataReaderListener_var plistener (new DataReaderListenerImpl);
if (CORBA::is_nil(plistener))
{
return false;
}
DDS::DataReader_var dr =
sub->create_datareader(topic.in(),
DATAREADER_QOS_DEFAULT,
plistener.in(),
::OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (CORBA::is_nil (dr.in()))
{
return false;
}
DataReaderListenerImpl* plistener_servant =
dynamic_cast<DataReaderListenerImpl*>(plistener.in());
m_sub_shape = &(plistener_servant->m_pShape);
return true;
}
bool CShapeDemoView::ShutDown()
{
if (!CORBA::is_nil(m_pdpf))
{
if (!CORBA::is_nil(m_pdp))
{
m_pdp->delete_contained_entities();
m_pdpf->delete_participant(m_pdp);
}
TheServiceParticipant->shutdown();
}
ACE::fini();
return true;
}
// CShapeDemoView 消息处理程序
void CShapeDemoView::OnPub()
{
// TODO: 在此添加命令处理程序代码
if (NULL == m_timer)
{
m_timer = SetTimer(100, 100, NULL);
init();
}
CreatePublisher();
}
void CShapeDemoView::OnSub()
{
// TODO: 在此添加命令处理程序代码
if (NULL == m_timer)
{
m_timer = SetTimer(100, 100, NULL);
init();
}
CreateSubscriber();
}
void CShapeDemoView::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if(m_pub_shape)
{
int deta = 3 * g_shap_size;
int left = m_pub_shape->xpos - deta;
int top = m_pub_shape->ypos - deta;
int right = m_pub_shape->xpos + deta;
int bottom = m_pub_shape->ypos + deta;
if (left<0)
{
left = 0;
}
if (right>m_max_with)
{
right = m_max_with;
}
if (top<0)
{
top = 0;
}
if (bottom>m_max_height)
{
bottom = m_max_height;
}
CRect rect(left, top, right, bottom);
InvalidateRect(&rect);
m_pwrite->write(*m_pub_shape, NULL);
m_pub_shape->xpos += m_xspeed;
if ((m_pub_shape->xpos >= m_max_with - g_shap_size)||(m_pub_shape->xpos <= g_shap_size))
{
m_xspeed = -m_xspeed;
}
m_pub_shape->ypos += m_yspeed;
if ((m_pub_shape->ypos >= m_max_height - g_shap_size)||(m_pub_shape->ypos <= g_shap_size))
{
m_yspeed = -m_yspeed;
}
}
CFormView::OnTimer(nIDEvent);
}
void CShapeDemoView::OnSize(UINT nType, int cx, int cy)
{
CFormView::OnSize(nType, cx, cy);
m_max_with = cx;
m_max_height = cy;
// TODO: 在此处添加消息处理程序代码
}
void CShapeDemoView::OnDraw(CDC* pDC)
{
// TODO: 在此添加专用代码和/或调用基类
if (m_pub_shape)
{
pDC->Rectangle(m_pub_shape->xpos - g_shap_size, m_pub_shape->ypos - g_shap_size,
m_pub_shape->xpos + g_shap_size, m_pub_shape->ypos + g_shap_size);
}
if (m_sub_shape)
{
pDC->Rectangle(m_sub_shape->xpos - g_shap_size, m_sub_shape->ypos - g_shap_size,
m_sub_shape->xpos + g_shap_size, m_sub_shape->ypos + g_shap_size);
}
}
void CShapeDemoView::OnDestroy()
{
CFormView::OnDestroy();
if (m_timer)
{
KillTimer(m_timer);
m_timer = NULL;
}
// TODO: 在此处添加消息处理程序代码
}
- 1
- 2
- 3
前往页