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

Annotation of /trunk/h8server/h8serverDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 104 - (hide annotations) (download)
Sun May 27 15:03:22 2007 UTC (17 years ago) by kevin
File size: 7795 byte(s)
ved ikke hvor langt jeg er lige pt.
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 kevin 104 int status, intlogid;
209 kevin 87 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     switch(buffer[0])
218     {
219    
220     //Hello request
221     case '0':
222     //LOG SKAL LAVES NUUUUUUUUUUUUUUUUUUUUUUU
223     reqID = buffer[0];
224     termID = buffer[1];
225    
226     senddata.Format("%d", ack);
227     worksock.Send(senddata, senddata.GetLength(),0);
228 kevin 103 tl = CommonStorage::Instance()->getDBLayer()->Transaktionslog(termID,0);
229     intlogid = (atoi(tl.logID));
230 kevin 87 break;
231    
232     case '2':
233     //return sum
234     isum = atoi(l.sum);
235     isum = (isum*100);
236     Output[0] = ack;
237     Output[1] = 2;
238     Output[2] = isum >> 24;
239     Output[3] = isum >> 16;
240     Output[4] = isum >> 8;
241     Output[5] = isum;
242     ssum = Output;
243     worksock.Send(ssum, ssum.GetLength(),0);
244     break;
245     case '3':
246     //byttepenge
247     cash = buffer[1] << 24 | buffer[2] << 16 | buffer[3] << 8 | buffer[4];
248     isum = atoi(l.sum);
249     isum = (isum*100);
250     bytte = isum - cash;
251     Output[0] = ack;
252     Output[1] = 3;
253     Output[2] = bytte >> 24;
254     Output[3] = bytte >> 16;
255     Output[4] = bytte >> 8;
256     Output[5] = bytte;
257     ssum = Output;
258     worksock.Send(ssum, ssum.GetLength(),0);
259     l.logID = "0";
260     break;
261     case '4':
262     //annulér sidste scanning
263     CommonStorage::Instance()->getDBLayer()->Deletelastentry(l.logID, l.barcode);
264     Output[0] = ack;
265     senddata = Output;
266     worksock.Send(senddata,senddata.GetLength(),0);
267     break;
268    
269     case '5':
270     //annulér hele sessionen
271     CommonStorage::Instance()->getDBLayer()->Deletesession(l.logID);
272     Output[0] = ack;
273     senddata = Output;
274     worksock.Send(senddata,senddata.GetLength(),0);
275     break;
276     case '6':
277     //Farvel
278     Output[0] = ack;
279     senddata = Output;
280     worksock.Send(senddata,senddata.GetLength(),0);
281     worksock.Close();
282     break;
283    
284     }
285    
286 kevin 103 //getvareinfo og sendvareinfo
287     if(status < 18 && status > 10)
288     {
289     reqID = buffer[0];
290     unsigned char antal = buffer[1];
291     unsigned char length = buffer[2];
292     int test;
293     if(length == 'A')
294     {
295     test = 10;
296     }
297    
298    
299     for (int i=3; i < (3+test); i++){
300     barcode[i-3] = buffer[i];
301     }
302     barcode[test] = '\0';
303     eq = CommonStorage::Instance()->getDBLayer()->GetEquipment(barcode,0,0);
304     price = (atoi(eq.price)*100);
305     Output[0] = 1;
306 kevin 104 Output[1] = price >> 24;
307     Output[2] = price >> 16;
308     Output[3] = price >> 8;
309     Output[4] = price;
310     Output[5] = eq.description.GetLength();
311 kevin 103 int outputlength;
312 kevin 104 outputlength = Output[5];
313     for (int i = 6; i < (6+outputlength); i++){
314     Output[i] = eq.description[i-6];
315 kevin 103 }
316     Output[outputlength] = '\0';
317    
318 kevin 104 worksock.Send(Output,6);
319 kevin 103
320     //LOG SKAL LAVES NUUUUUUUUUUUUUUUUUUUUUUU
321     if(intlogid > 0)
322     {
323     CString logantal;
324     logantal.Format("%d",antal-48);
325     CommonStorage::Instance()->getDBLayer()->TransaktionsEnhed(tl.logID, eq.price, barcode,logantal);
326     }
327 kevin 87 }
328    
329 kevin 103 }
330    
331 kevin 87 CSocket::OnReceive(nErrorCode);
332     }
333    
334    
335     void mysocket::OnAccept(int nErrorCode)
336     {
337     // TODO: Add your specialized code here and/or call the base class
338     sock.Accept(worksock);
339     CSocket::OnAccept(nErrorCode);
340     }

  ViewVC Help
Powered by ViewVC 1.1.20