/[H8]/trunk/h8server/h8serverDlg.cpp
ViewVC logotype

Annotation of /trunk/h8server/h8serverDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 100 - (hide annotations) (download)
Fri May 25 08:34:08 2007 UTC (17 years ago) by kevin
File size: 7792 byte(s)
added test program and fixed some log problems
1 kevin 87 // h8serverDlg.cpp : implementation file
2     //
3    
4     #include "stdafx.h"
5     #include "h8server.h"
6     #include "h8serverDlg.h"
7     #include "ConfigFile.h"
8     #include "MySQLLayer.h"
9     #include "Containers.h"
10     #include <vector>
11    
12     #ifdef _DEBUG
13     #define new DEBUG_NEW
14     #endif
15    
16    
17     // CAboutDlg dialog used for App About
18    
19     class CAboutDlg : public CDialog
20     {
21     public:
22     CAboutDlg();
23    
24     // Dialog Data
25     enum { IDD = IDD_ABOUTBOX };
26    
27     protected:
28     virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
29    
30     // Implementation
31     protected:
32     DECLARE_MESSAGE_MAP()
33     };
34    
35     class mysocket : public CSocket
36     {
37     public:
38     virtual void OnReceive(int nErrorCode);
39     virtual void OnAccept(int nErrorCode);
40     };
41     mysocket sock;
42     mysocket worksock;
43     CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
44     {
45     }
46    
47     void CAboutDlg::DoDataExchange(CDataExchange* pDX)
48     {
49     CDialog::DoDataExchange(pDX);
50     }
51    
52     BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
53     END_MESSAGE_MAP()
54    
55    
56     // Ch8serverDlg dialog
57    
58    
59    
60     Ch8serverDlg::Ch8serverDlg(CWnd* pParent /*=NULL*/)
61     : CDialog(Ch8serverDlg::IDD, pParent)
62     {
63     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
64     }
65    
66     void Ch8serverDlg::DoDataExchange(CDataExchange* pDX)
67     {
68     CDialog::DoDataExchange(pDX);
69     }
70    
71     BEGIN_MESSAGE_MAP(Ch8serverDlg, CDialog)
72     ON_WM_SYSCOMMAND()
73     ON_WM_PAINT()
74     ON_WM_QUERYDRAGICON()
75     //}}AFX_MSG_MAP
76     END_MESSAGE_MAP()
77    
78    
79     // Ch8serverDlg message handlers
80    
81     BOOL Ch8serverDlg::OnInitDialog()
82     {
83     CDialog::OnInitDialog();
84    
85     // Add "About..." menu item to system menu.
86    
87     // IDM_ABOUTBOX must be in the system command range.
88     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
89     ASSERT(IDM_ABOUTBOX < 0xF000);
90    
91     CMenu* pSysMenu = GetSystemMenu(FALSE);
92     if (pSysMenu != NULL)
93     {
94     CString strAboutMenu;
95     strAboutMenu.LoadString(IDS_ABOUTBOX);
96     if (!strAboutMenu.IsEmpty())
97     {
98     pSysMenu->AppendMenu(MF_SEPARATOR);
99     pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
100     }
101     }
102    
103     // Set the icon for this dialog. The framework does this automatically
104     // when the application's main window is not a dialog
105     SetIcon(m_hIcon, TRUE); // Set big icon
106     SetIcon(m_hIcon, FALSE); // Set small icon
107    
108     // TODO: Add extra initialization here
109    
110     int listenPort = 4000;
111    
112     sock.Create(listenPort);
113     sock.Listen();
114    
115     ConfigFile config;
116     try {
117     config.Read();
118     } catch(...) {
119     MessageBox("Could not open config file");
120     OnOK();
121     return true;
122     }
123    
124     try {
125     DatabaseLayer *dbl = 0;
126     if (config.driver.MakeLower() == "mysql")
127     dbl = new MySQLLayer(config);
128     else {
129     CString tmp;
130     tmp.Format("Unsupported driver: %s", config.driver);
131     MessageBox(tmp);
132     OnOK();
133     }
134     CommonStorage::Instance()->setDBLayer(dbl);
135     } catch (CDBException *ex) {
136     MessageBox(CString("Could not establish connection to the database server\r\n") +
137     "Please contact your network administrator\r\n\r\n" +
138     "ODBC Returned the following message:\r\n" +
139     ex->m_strError);
140    
141     OnOK();
142     } catch (...) {
143     MessageBox("Unknown DB exception");
144     OnOK();
145     }
146     return TRUE; // return TRUE unless you set the focus to a control
147     }
148    
149     void Ch8serverDlg::OnSysCommand(UINT nID, LPARAM lParam)
150     {
151     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
152     {
153     CAboutDlg dlgAbout;
154     dlgAbout.DoModal();
155     }
156     else
157     {
158     CDialog::OnSysCommand(nID, lParam);
159     }
160     }
161    
162     // If you add a minimize button to your dialog, you will need the code below
163     // to draw the icon. For MFC applications using the document/view model,
164     // this is automatically done for you by the framework.
165    
166     void Ch8serverDlg::OnPaint()
167     {
168     if (IsIconic())
169     {
170     CPaintDC dc(this); // device context for painting
171    
172     SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
173    
174     // Center icon in client rectangle
175     int cxIcon = GetSystemMetrics(SM_CXICON);
176     int cyIcon = GetSystemMetrics(SM_CYICON);
177     CRect rect;
178     GetClientRect(&rect);
179     int x = (rect.Width() - cxIcon + 1) / 2;
180     int y = (rect.Height() - cyIcon + 1) / 2;
181    
182     // Draw the icon
183     dc.DrawIcon(x, y, m_hIcon);
184     }
185     else
186     {
187     CDialog::OnPaint();
188     }
189     }
190    
191     // The system calls this function to obtain the cursor to display while the user drags
192     // the minimized window.
193     HCURSOR Ch8serverDlg::OnQueryDragIcon()
194     {
195     return static_cast<HCURSOR>(m_hIcon);
196     }
197    
198     void mysocket::OnReceive(int nErrorCode)
199     {
200     // TODO: Add your specialized code here and/or call the base class
201     Ch8serverDlg* parent = (Ch8serverDlg*) AfxGetMainWnd();
202     char buffer[30];
203     char barcode[15];
204     int ack = 1;
205 kevin 100 CString senddata, termID, ssum, hellodata1, hellodata2;
206 kevin 87 unsigned char Output[30];
207     unsigned int price, isum, cash, bytte;
208     int status;
209     unsigned char reqID;
210     log l;
211 kevin 100 translog tl;
212 kevin 87 Equipment eq;
213    
214     status = worksock.Receive(buffer,30);
215     if (status != SOCKET_ERROR) {
216    
217     //getvareinfo og sendvareinfo
218     if(status < 18 && status > 10)
219     {
220     reqID = buffer[0];
221     unsigned char antal = buffer[1];
222     unsigned char length = buffer[2];
223     int test;
224     if(length == 'A')
225     {
226     test = 10;
227     }
228    
229 kevin 100 //l.logID = "5";
230 kevin 87
231     for (int i=3; i < (3+test); i++){
232     barcode[i-3] = buffer[i];
233     }
234     barcode[test] = '\0';
235     eq = CommonStorage::Instance()->getDBLayer()->GetEquipment(barcode,0,0);
236     price = (atoi(eq.price)*100);
237     Output[0] = 1;
238     Output[1] = 1;
239     Output[2] = price >> 24;
240     Output[3] = price >> 16;
241     Output[4] = price >> 8;
242     Output[5] = price;
243     Output[6] = eq.description.GetLength();
244     int outputlength;
245     outputlength = Output[6];
246     for (int i = 7; i < (7+outputlength); i++){
247     Output[i] = eq.description[i-7];
248     }
249     Output[outputlength] = '\0';
250    
251 kevin 97 worksock.Send(Output,7);
252 kevin 87
253     //LOG SKAL LAVES NUUUUUUUUUUUUUUUUUUUUUUU
254 kevin 100 if(atoi(tl.logID) > 0)
255 kevin 87 {
256     CString logantal;
257 kevin 100 logantal.Format("%d",antal-48);
258     CommonStorage::Instance()->getDBLayer()->TransaktionsEnhed(tl.logID, eq.price, barcode,logantal);
259 kevin 87 }
260     }
261    
262     switch(buffer[0])
263     {
264    
265     //Hello request
266     case '0':
267     //LOG SKAL LAVES NUUUUUUUUUUUUUUUUUUUUUUU
268     reqID = buffer[0];
269     termID = buffer[1];
270    
271     senddata.Format("%d", ack);
272     worksock.Send(senddata, senddata.GetLength(),0);
273 kevin 100 CommonStorage::Instance()->getDBLayer()->Transaktionslog(termID,0);
274 kevin 87 break;
275    
276     case '2':
277     //return sum
278     isum = atoi(l.sum);
279     isum = (isum*100);
280     Output[0] = ack;
281     Output[1] = 2;
282     Output[2] = isum >> 24;
283     Output[3] = isum >> 16;
284     Output[4] = isum >> 8;
285     Output[5] = isum;
286     ssum = Output;
287     worksock.Send(ssum, ssum.GetLength(),0);
288     break;
289     case '3':
290     //byttepenge
291     cash = buffer[1] << 24 | buffer[2] << 16 | buffer[3] << 8 | buffer[4];
292     isum = atoi(l.sum);
293     isum = (isum*100);
294     bytte = isum - cash;
295     Output[0] = ack;
296     Output[1] = 3;
297     Output[2] = bytte >> 24;
298     Output[3] = bytte >> 16;
299     Output[4] = bytte >> 8;
300     Output[5] = bytte;
301     ssum = Output;
302     worksock.Send(ssum, ssum.GetLength(),0);
303     l.logID = "0";
304     break;
305     case '4':
306     //annulér sidste scanning
307     CommonStorage::Instance()->getDBLayer()->Deletelastentry(l.logID, l.barcode);
308     Output[0] = ack;
309     senddata = Output;
310     worksock.Send(senddata,senddata.GetLength(),0);
311     break;
312    
313     case '5':
314     //annulér hele sessionen
315     CommonStorage::Instance()->getDBLayer()->Deletesession(l.logID);
316     Output[0] = ack;
317     senddata = Output;
318     worksock.Send(senddata,senddata.GetLength(),0);
319     break;
320     case '6':
321     //Farvel
322     Output[0] = ack;
323     senddata = Output;
324     worksock.Send(senddata,senddata.GetLength(),0);
325     worksock.Close();
326     break;
327    
328     }
329    
330     }
331    
332     CSocket::OnReceive(nErrorCode);
333     }
334    
335    
336     void mysocket::OnAccept(int nErrorCode)
337     {
338     // TODO: Add your specialized code here and/or call the base class
339     sock.Accept(worksock);
340     CSocket::OnAccept(nErrorCode);
341     }

  ViewVC Help
Powered by ViewVC 1.1.20