// clientserverView.cpp : implementation of the CClientserverView class // #include "stdafx.h" #include "clientserver.h" #include "clientserverDoc.h" #include "clientserverView.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #include "EnterCheckNumberDlg.h" #include "EnterDepositNumberDlg.h" #include "DisplayCheckInfoDlg.h" #include "DisplayDepositInfoDlg.h" #include "GetServerDlg.h" ///////////////////////////////////////////////////////////////////////////// // CClientserverView IMPLEMENT_DYNCREATE(CClientserverView, CView) BEGIN_MESSAGE_MAP(CClientserverView, CView) //{{AFX_MSG_MAP(CClientserverView) ON_COMMAND(ID_BANK_STARTSERVER, OnBankStartserver) ON_COMMAND(ID_BANK_CHECK, OnBankCheck) ON_COMMAND(ID_BANK_DEPOSIT, OnBankDeposit) ON_COMMAND(ID_BANK_DISCONNECT, OnBankDisconnect) //}}AFX_MSG_MAP // Standard printing commands ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CClientserverView construction/destruction CClientserverView::CClientserverView() { // TODO: add construction code here } CClientserverView::~CClientserverView() { } BOOL CClientserverView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CClientserverView drawing void CClientserverView::OnDraw(CDC* pDC) { CClientserverDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here } ///////////////////////////////////////////////////////////////////////////// // CClientserverView printing BOOL CClientserverView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CClientserverView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CClientserverView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CClientserverView diagnostics #ifdef _DEBUG void CClientserverView::AssertValid() const { CView::AssertValid(); } void CClientserverView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CClientserverDoc* CClientserverView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CClientserverDoc))); return (CClientserverDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CClientserverView message handlers void CClientserverView::BankStartserver() { CClientserverDoc* pDoc; pDoc=GetDocument(); // assume that the constructor for the document class // has properly initialized all document member variables // and then // force the document class to serialize/read the file // CString MyFileNameVar="myfilename.dat"; // default file name CFileDialog myOpenFileDlg(TRUE,NULL,MyFileNameVar); // TRUE yields a FileOpen myOpenFileDlg.DoModal(); // show dialog box MyFileNameVar=myOpenFileDlg.GetPathName(); // get whole file name pDoc->OnOpenDocument(MyFileNameVar); // serialize/read CSocket sockSrvr; // construct a socket sockSrvr.Create(77); // create the SOCKET sockSrvr.Listen( ); // start listening CSocket sockRecv; // construct a new, empty socket sockSrvr.Accept( sockRecv ); // accept connection CSocketFile file(&sockRecv); // construct file object // construct two archives CArchive arIn(&file, CArchive::load); CArchive arOut(&file, CArchive::store); int TransactionType=0; // temporary input variables int tCheckNumber=0; CString tCheckDate=""; CString tPayTo=""; double tCheckAmount=0.0; int tDepositNumber=0; CString tDepositDate=""; double tDepositAmount=0.0; CString tDepositNote=""; // read transaction type // note: 1=check // 2=deposit // 3=exit arIn >> TransactionType; while(TransactionType != 3) { switch(TransactionType){ case 1: // get check number (max 999) arIn >> tCheckNumber; // send check data to client arOut << pDoc->Checks[tCheckNumber-1].GetCheckDate(); arOut << pDoc->Checks[tCheckNumber-1].GetCheckPayTo(); arOut << pDoc->Checks[tCheckNumber-1].GetCheckAmount(); arOut.Flush(); // get data back from client arIn >> tCheckDate; arIn >> tPayTo; arIn >> tCheckAmount; // store back in array pDoc->Checks[tCheckNumber-1].SetCheckDate(tCheckDate); pDoc->Checks[tCheckNumber-1].SetCheckPayTo(tPayTo); pDoc->Checks[tCheckNumber-1].SetCheckAmount(tCheckAmount); break; case 2: // get deposit number (max 999) arIn >> tDepositNumber; // send check data to client arOut << pDoc->Deposits[tDepositNumber-1].GetDepositDate(); arOut << pDoc->Deposits[tDepositNumber-1].GetDepositNote(); arOut << pDoc->Deposits[tDepositNumber-1].GetDepositAmount(); arOut.Flush(); // get data back from client arIn >> tDepositDate; arIn >> tDepositNote; arIn >> tDepositAmount; // store back in array pDoc->Deposits[tDepositNumber-1].SetDepositDate(tDepositDate); pDoc->Deposits[tDepositNumber-1].SetDepositNote(tDepositNote); pDoc->Deposits[tDepositNumber-1].SetDepositNote(tDepositAmount); break; case 3: break; } // get next transaction type arIn >> TransactionType; } // // force the document class to serialize/save the file // CFileDialog mySaveFileDlg(FALSE,NULL,MyFileNameVar); // FALSE yields a FileSaveAs mySaveFileDlg.DoModal(); // show dialog box MyFileNameVar=mySaveFileDlg.GetPathName(); // get whole file name pDoc->OnSaveDocument(MyFileNameVar); // serialize/save } void CClientserverView::OnBankCheck() { CClientserverDoc* pDoc; pDoc=GetDocument(); if(!pDoc->Connected){ // Connected is a BOOL var CGetServerDlg getserver; getserver.m_serveraddress=""; getserver.DoModal(); pDoc->sockClient.Connect(getserver.m_serveraddress, 77); // seek a connection pDoc->Connected=TRUE; } CSocketFile file(&sockClient); // construct file object // construct two archives CArchive arIn(&file, CArchive::load); CArchive arOut(&file, CArchive::store); CEnterCheckNumberDlg onechecknumdlg; CDisplayCheckInfoDlg onecheckinfodlg; int TranType=1; // check transaction type CString tCheckDate=""; CString tCheckPayTo=""; double tCheckAmount=0.0; onechecknumdlg.m_CheckNumber=1; if(onechecknumdlg.DoModal()==IDOK) { // send transaction type 1 (check) to server arOut << TranType; arOut.Flush(); // send check # to server arOut << onechecknumdlg.m_CheckNumber; arOut.Flush(); // receive check data from server arIn >> tCheckDate; onecheckinfodlg.m_CheckDate=tCheckDate; arIn >> tCheckPayTo; onecheckinfodlg.m_CheckPayTo=tCheckPayTo; arIn >> tCheckAmount; onecheckinfodlg.m_CheckAmount=tCheckAmount; if(onecheckinfodlg.DoModal()==IDOK) { // send updated check information to server arOut << onecheckinfodlg.m_CheckDate; arOut << onecheckinfodlg.m_CheckPayTo; arOut << onecheckinfodlg.m_CheckAmount; arOut.Flush(); } else { // send unchanged data back to server // REMEMBER that server is waiting to // have a check sent back to it one // way or another... arOut << tCheckDate; arOut << tCheckPayTo; arOut << tCheckAmount; arOut.Flush(); } } } void CClientserverView::OnBankDeposit() { CClientserverDoc* pDoc; pDoc=GetDocument(); if(!pDoc->Connected){ // Connected is a BOOL var CGetServerDlg getserver; getserver.m_serveraddress=""; getserver.DoModal(); pDoc->sockClient.Connect(getserver.m_serveraddress, 77); // seek a connection pDoc->Connected=TRUE; } CSocketFile file(&sockClient); // construct file object // construct two archives CArchive arIn(&file, CArchive::load); CArchive arOut(&file, CArchive::store); CEnterDepositNumberDlg onedepositnumdlg; CDisplayDepositInfoDlg onedepositinfodlg; int TranType=2; // deposit transaction type CString tDepositDate=""; CString tDepositNote=""; double tDepositAmount=0.0; onedepositnumdlg.m_DepositNumber=1; if(onedepositnumdlg.DoModal()==IDOK) { // send transaction type 2 (deposit) to server arOut << TranType; arOut.Flush(); // send deposit # to server arOut << onedepositnumdlg.m_DepositNumber; arOut.Flush(); // receive deposit data from server arIn >> tDepositDate; onedepositinfodlg.m_DepositDate=tDepositDate; arIn >> tDepositNote; onedepositinfodlg.m_DepositNote=tDepositNote; arIn >> tDepositAmount; onedepositinfodlg.m_DepositAmount=tDepositAmount; if(onedepositinfodlg.DoModal()==IDOK) { // send updated deposit information to server arOut << onedepositinfodlg.m_DepositDate; arOut << onedepositinfodlg.m_DepositNote; arOut << onedepositinfodlg.m_DepositAmount; arOut.Flush(); } else { // send unchanged data back to server // REMEMBER that server is waiting to // have a deposit sent back to it one // way or another... arOut << tDepositDate; arOut << tDepositNote; arOut << tDepositAmount; arOut.Flush(); } } } void CClientserverView::OnBankDisconnect() { CClientserverDoc* pDoc; pDoc=GetDocument(); if(!pDoc->Connected){ // Connected is a BOOL var CGetServerDlg getserver; getserver.m_serveraddress=""; getserver.DoModal(); pDoc->sockClient.Connect(getserver.m_serveraddress, 77); // seek a connection pDoc->Connected=TRUE; } CSocketFile file(&sockClient); // construct file object // construct an archives CArchive arOut(&file, CArchive::store); int TranType=3; // disconnect transaction type arOut << TranType; // send disconnect code }