home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / labs / c11 / lab04 / ex02 / gpsrc / gpsrcdoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  3.5 KB  |  147 lines

  1. // gpsrcDoc.cpp : implementation of the CGpsrcDoc class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "gpsrc.h"
  6.  
  7. #include "gpsrcDoc.h"
  8. #include "MainFrm.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CGpsrcDoc
  18.  
  19. IMPLEMENT_DYNCREATE(CGpsrcDoc, CDocument)
  20.  
  21. BEGIN_MESSAGE_MAP(CGpsrcDoc, CDocument)
  22.     //{{AFX_MSG_MAP(CGpsrcDoc)
  23.         // NOTE - the ClassWizard will add and remove mapping macros here.
  24.         //    DO NOT EDIT what you see in these blocks of generated code!
  25.     //}}AFX_MSG_MAP
  26. END_MESSAGE_MAP()
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CGpsrcDoc construction/destruction
  30.  
  31. CGpsrcDoc::CGpsrcDoc()
  32. {
  33.     // TODO: add one-time construction code here
  34.  
  35.     m_pSock= NULL;
  36. }
  37.  
  38. CGpsrcDoc::~CGpsrcDoc()
  39. {
  40. }
  41.  
  42.  
  43. BOOL CGpsrcDoc::OnNewDocument()
  44. {
  45.     if (!CDocument::OnNewDocument())
  46.         return FALSE;
  47.  
  48.     // TODO: add reinitialization code here
  49.     // (SDI documents will reuse this document)
  50.  
  51.  
  52.     // Create some Longitude/Latitude test points.
  53.     m_LonLat.Add("122W:08:35 47N:52:36");
  54.     m_LonLat.Add("122W:15:18 47N:55:34");
  55.     m_LonLat.Add("122W:23:39 47N:54:22");
  56.     m_LonLat.Add("122W:27:05 47N:50:44");
  57.     m_LonLat.Add("122W:22:10 47N:45:47");
  58.     m_LonLat.Add("122W:17:45 47N:44:21");
  59.     m_LonLat.Add("122W:10:33 47N:42:48");
  60.     m_LonLat.Add("122W:06:57 47N:38:11");
  61.     m_LonLat.Add("122W:12:41 47N:36:39");
  62.     m_LonLat.Add("122W:22:10 47N:37:05");
  63.     m_LonLat.Add("122W:27:34 47N:42:22");
  64.     m_LonLat.Add("122W:30:50 47N:46:00");
  65.     m_LonLat.Add("122W:31:10 47N:48:32");
  66.     m_LonLat.Add("122W:27:14 47N:54:55");
  67.     m_LonLat.Add("122W:19:33 47N:54:08");
  68.     m_LonLat.Add("122W:19:04 47N:48:51");
  69.     m_LonLat.Add("122W:19:23 47N:42:29");
  70.     m_LonLat.Add("122W:19:23 47N:39:43");
  71.     m_LonLat.Add("122W:19:13 47N:35:26");
  72.     m_LonLat.Add("122W:19:39 47N:35:00");
  73.     m_LonLat.Add("122W:15:18 47N:41:09");
  74.     m_LonLat.Add("122W:15:18 47N:43:41");
  75.     m_LonLat.Add("122W:15:28 47N:51:17");
  76.     m_LonLat.Add("122W:14:09 47N:57:00");
  77.     m_LonLat.Add("122W:08:45 47N:56:39");
  78.     m_curLonLat= 0;
  79.  
  80.     // Initialize GPS Socket.
  81.     m_pSock= new CAsyncSocket;
  82.     m_pSock->Create(0, SOCK_DGRAM);
  83.  
  84.  
  85.     return TRUE;
  86. }
  87. void CGpsrcDoc::DeleteContents()
  88. {
  89.     m_strings.RemoveAll();
  90.     UpdateAllViews(NULL);    // refresh our screen.
  91. }
  92.  
  93. /////////////////////////////////////////////////////////////////////////////
  94. // CGpsrcDoc serialization
  95.  
  96. void CGpsrcDoc::Serialize(CArchive& ar)
  97. {
  98.     if (ar.IsStoring())
  99.     {
  100.         // TODO: add storing code here
  101.     }
  102.     else
  103.     {
  104.         // TODO: add loading code here
  105.     }
  106. }
  107.  
  108. /////////////////////////////////////////////////////////////////////////////
  109. // CGpsrcDoc diagnostics
  110.  
  111. #ifdef _DEBUG
  112. void CGpsrcDoc::AssertValid() const
  113. {
  114.     CDocument::AssertValid();
  115. }
  116.  
  117. void CGpsrcDoc::Dump(CDumpContext& dc) const
  118. {
  119.     CDocument::Dump(dc);
  120. }
  121. #endif //_DEBUG
  122.  
  123. /////////////////////////////////////////////////////////////////////////////
  124. // CGpsrcDoc commands
  125.  
  126. void CGpsrcDoc::SendPosition()
  127. {
  128.     if (m_pSock) 
  129.     {
  130.         CString lonlat= m_LonLat.GetAt(m_curLonLat);
  131.         m_strings.Add(lonlat);
  132.         UpdateAllViews(NULL);    // refresh our screen.
  133.  
  134.         // Recycle points if we need to.
  135.         if (++m_curLonLat>= m_LonLat.GetSize())
  136.             m_curLonLat= 0;
  137.  
  138.         // Send out datagram assuming the meter (server) resides 
  139.         // on the same computer as this app.
  140.         char server[512];
  141.         gethostname(server,511);
  142.         m_pSock->SendTo(lonlat, lonlat.GetLength(), 1088, server);
  143.  
  144.     }
  145. }
  146.  
  147.