// h8serverDlg.cpp : implementation file // #include "stdafx.h" #include "h8server.h" #include "h8serverDlg.h" #include "ConfigFile.h" #include "MySQLLayer.h" #include "Containers.h" #include #ifdef _DEBUG #define new DEBUG_NEW #endif // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data enum { IDD = IDD_ABOUTBOX }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support // Implementation protected: DECLARE_MESSAGE_MAP() }; class mysocket : public CSocket { public: virtual void OnReceive(int nErrorCode); virtual void OnAccept(int nErrorCode); }; mysocket sock; mysocket worksock; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) END_MESSAGE_MAP() // Ch8serverDlg dialog Ch8serverDlg::Ch8serverDlg(CWnd* pParent /*=NULL*/) : CDialog(Ch8serverDlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void Ch8serverDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(Ch8serverDlg, CDialog) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP END_MESSAGE_MAP() // Ch8serverDlg message handlers BOOL Ch8serverDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here int listenPort = 4000; sock.Create(listenPort); sock.Listen(); ConfigFile config; try { config.Read(); } catch(...) { MessageBox("Could not open config file"); OnOK(); return true; } try { DatabaseLayer *dbl = 0; if (config.driver.MakeLower() == "mysql") dbl = new MySQLLayer(config); else { CString tmp; tmp.Format("Unsupported driver: %s", config.driver); MessageBox(tmp); OnOK(); } CommonStorage::Instance()->setDBLayer(dbl); } catch (CDBException *ex) { MessageBox(CString("Could not establish connection to the database server\r\n") + "Please contact your network administrator\r\n\r\n" + "ODBC Returned the following message:\r\n" + ex->m_strError); OnOK(); } catch (...) { MessageBox("Unknown DB exception"); OnOK(); } return TRUE; // return TRUE unless you set the focus to a control } void Ch8serverDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void Ch8serverDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this function to obtain the cursor to display while the user drags // the minimized window. HCURSOR Ch8serverDlg::OnQueryDragIcon() { return static_cast(m_hIcon); } void mysocket::OnReceive(int nErrorCode) { // TODO: Add your specialized code here and/or call the base class Ch8serverDlg* parent = (Ch8serverDlg*) AfxGetMainWnd(); char buffer[30]; char barcode[15]; int ack = 1; CString senddata, termID, ssum; unsigned char Output[30]; unsigned int price, isum, cash, bytte; int status; unsigned char reqID; log l; Equipment eq; status = worksock.Receive(buffer,30); if (status != SOCKET_ERROR) { //getvareinfo og sendvareinfo if(status < 18 && status > 10) { reqID = buffer[0]; unsigned char antal = buffer[1]; unsigned char length = buffer[2]; int test; if(length == 'A') { test = 10; } l.logID = "5"; for (int i=3; i < (3+test); i++){ barcode[i-3] = buffer[i]; } barcode[test] = '\0'; eq = CommonStorage::Instance()->getDBLayer()->GetEquipment(barcode,0,0); price = (atoi(eq.price)*100); Output[0] = 1; Output[1] = 1; Output[2] = price >> 24; Output[3] = price >> 16; Output[4] = price >> 8; Output[5] = price; Output[6] = eq.description.GetLength(); int outputlength; outputlength = Output[6]; for (int i = 7; i < (7+outputlength); i++){ Output[i] = eq.description[i-7]; } Output[outputlength] = '\0'; worksock.Send(Output,sendlength,0); //LOG SKAL LAVES NUUUUUUUUUUUUUUUUUUUUUUU if(l.logID < 0) { CString logantal; logantal.Format("%d",antal); CommonStorage::Instance()->getDBLayer()->TransaktionsEnhed(l.logID, eq.price, barcode,logantal); } } switch(buffer[0]) { //Hello request case '0': //LOG SKAL LAVES NUUUUUUUUUUUUUUUUUUUUUUU reqID = buffer[0]; termID = buffer[1]; senddata.Format("%d", ack); worksock.Send(senddata, senddata.GetLength(),0); CommonStorage::Instance()->getDBLayer()->Transaktionslog(termID); break; case '2': //return sum isum = atoi(l.sum); isum = (isum*100); Output[0] = ack; Output[1] = 2; Output[2] = isum >> 24; Output[3] = isum >> 16; Output[4] = isum >> 8; Output[5] = isum; ssum = Output; worksock.Send(ssum, ssum.GetLength(),0); break; case '3': //byttepenge cash = buffer[1] << 24 | buffer[2] << 16 | buffer[3] << 8 | buffer[4]; isum = atoi(l.sum); isum = (isum*100); bytte = isum - cash; Output[0] = ack; Output[1] = 3; Output[2] = bytte >> 24; Output[3] = bytte >> 16; Output[4] = bytte >> 8; Output[5] = bytte; ssum = Output; worksock.Send(ssum, ssum.GetLength(),0); l.logID = "0"; break; case '4': //annulér sidste scanning CommonStorage::Instance()->getDBLayer()->Deletelastentry(l.logID, l.barcode); Output[0] = ack; senddata = Output; worksock.Send(senddata,senddata.GetLength(),0); break; case '5': //annulér hele sessionen CommonStorage::Instance()->getDBLayer()->Deletesession(l.logID); Output[0] = ack; senddata = Output; worksock.Send(senddata,senddata.GetLength(),0); break; case '6': //Farvel Output[0] = ack; senddata = Output; worksock.Send(senddata,senddata.GetLength(),0); worksock.Close(); break; } } CSocket::OnReceive(nErrorCode); } void mysocket::OnAccept(int nErrorCode) { // TODO: Add your specialized code here and/or call the base class sock.Accept(worksock); CSocket::OnAccept(nErrorCode); }