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

Annotation of /trunk/h8server/h8serverDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 97 - (hide annotations) (download)
Tue May 22 06:55:23 2007 UTC (17 years ago) by kevin
File size: 7744 byte(s)
sendcommand fixed
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     CString senddata, termID, ssum;
206     unsigned char Output[30];
207     unsigned int price, isum, cash, bytte;
208     int status;
209     unsigned char reqID;
210     log l;
211     Equipment eq;
212    
213     status = worksock.Receive(buffer,30);
214     if (status != SOCKET_ERROR) {
215    
216     //getvareinfo og sendvareinfo
217     if(status < 18 && status > 10)
218     {
219     reqID = buffer[0];
220     unsigned char antal = buffer[1];
221     unsigned char length = buffer[2];
222     int test;
223     if(length == 'A')
224     {
225     test = 10;
226     }
227    
228     l.logID = "5";
229    
230     for (int i=3; i < (3+test); i++){
231     barcode[i-3] = buffer[i];
232     }
233     barcode[test] = '\0';
234     eq = CommonStorage::Instance()->getDBLayer()->GetEquipment(barcode,0,0);
235     price = (atoi(eq.price)*100);
236     Output[0] = 1;
237     Output[1] = 1;
238     Output[2] = price >> 24;
239     Output[3] = price >> 16;
240     Output[4] = price >> 8;
241     Output[5] = price;
242     Output[6] = eq.description.GetLength();
243     int outputlength;
244     outputlength = Output[6];
245     for (int i = 7; i < (7+outputlength); i++){
246     Output[i] = eq.description[i-7];
247     }
248     Output[outputlength] = '\0';
249    
250 kevin 97 worksock.Send(Output,7);
251 kevin 87
252     //LOG SKAL LAVES NUUUUUUUUUUUUUUUUUUUUUUU
253 kevin 97 if(atoi(l.logID) < 0)
254 kevin 87 {
255     CString logantal;
256     logantal.Format("%d",antal);
257     CommonStorage::Instance()->getDBLayer()->TransaktionsEnhed(l.logID, eq.price, barcode,logantal);
258     }
259     }
260    
261     switch(buffer[0])
262     {
263    
264     //Hello request
265     case '0':
266     //LOG SKAL LAVES NUUUUUUUUUUUUUUUUUUUUUUU
267     reqID = buffer[0];
268     termID = buffer[1];
269    
270     senddata.Format("%d", ack);
271     worksock.Send(senddata, senddata.GetLength(),0);
272     CommonStorage::Instance()->getDBLayer()->Transaktionslog(termID);
273     break;
274    
275     case '2':
276     //return sum
277     isum = atoi(l.sum);
278     isum = (isum*100);
279     Output[0] = ack;
280     Output[1] = 2;
281     Output[2] = isum >> 24;
282     Output[3] = isum >> 16;
283     Output[4] = isum >> 8;
284     Output[5] = isum;
285     ssum = Output;
286     worksock.Send(ssum, ssum.GetLength(),0);
287     break;
288     case '3':
289     //byttepenge
290     cash = buffer[1] << 24 | buffer[2] << 16 | buffer[3] << 8 | buffer[4];
291     isum = atoi(l.sum);
292     isum = (isum*100);
293     bytte = isum - cash;
294     Output[0] = ack;
295     Output[1] = 3;
296     Output[2] = bytte >> 24;
297     Output[3] = bytte >> 16;
298     Output[4] = bytte >> 8;
299     Output[5] = bytte;
300     ssum = Output;
301     worksock.Send(ssum, ssum.GetLength(),0);
302     l.logID = "0";
303     break;
304     case '4':
305     //annulér sidste scanning
306     CommonStorage::Instance()->getDBLayer()->Deletelastentry(l.logID, l.barcode);
307     Output[0] = ack;
308     senddata = Output;
309     worksock.Send(senddata,senddata.GetLength(),0);
310     break;
311    
312     case '5':
313     //annulér hele sessionen
314     CommonStorage::Instance()->getDBLayer()->Deletesession(l.logID);
315     Output[0] = ack;
316     senddata = Output;
317     worksock.Send(senddata,senddata.GetLength(),0);
318     break;
319     case '6':
320     //Farvel
321     Output[0] = ack;
322     senddata = Output;
323     worksock.Send(senddata,senddata.GetLength(),0);
324     worksock.Close();
325     break;
326    
327     }
328    
329     }
330    
331     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