|
1 /* |
|
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 // myclientDlg.cpp : implementation file |
|
22 // |
|
23 |
|
24 #include "stdafx.h" |
|
25 #include "myclient.h" |
|
26 #include "myclientDlg.h" |
|
27 |
|
28 #ifdef _DEBUG |
|
29 #define new DEBUG_NEW |
|
30 #undef THIS_FILE |
|
31 static char THIS_FILE[] = __FILE__; |
|
32 #endif |
|
33 |
|
34 ///////////////////////////////////////////////////////////////////////////// |
|
35 // CAboutDlg dialog used for App About |
|
36 |
|
37 class CAboutDlg : public CDialog |
|
38 { |
|
39 public: |
|
40 CAboutDlg(); |
|
41 |
|
42 // Dialog Data |
|
43 //{{AFX_DATA(CAboutDlg) |
|
44 enum { IDD = IDD_ABOUTBOX }; |
|
45 //}}AFX_DATA |
|
46 |
|
47 // ClassWizard generated virtual function overrides |
|
48 //{{AFX_VIRTUAL(CAboutDlg) |
|
49 protected: |
|
50 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support |
|
51 //}}AFX_VIRTUAL |
|
52 |
|
53 // Implementation |
|
54 protected: |
|
55 //{{AFX_MSG(CAboutDlg) |
|
56 //}}AFX_MSG |
|
57 DECLARE_MESSAGE_MAP() |
|
58 }; |
|
59 |
|
60 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) |
|
61 { |
|
62 //{{AFX_DATA_INIT(CAboutDlg) |
|
63 //}}AFX_DATA_INIT |
|
64 } |
|
65 |
|
66 void CAboutDlg::DoDataExchange(CDataExchange* pDX) |
|
67 { |
|
68 CDialog::DoDataExchange(pDX); |
|
69 //{{AFX_DATA_MAP(CAboutDlg) |
|
70 //}}AFX_DATA_MAP |
|
71 } |
|
72 |
|
73 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) |
|
74 //{{AFX_MSG_MAP(CAboutDlg) |
|
75 // No message handlers |
|
76 //}}AFX_MSG_MAP |
|
77 END_MESSAGE_MAP() |
|
78 |
|
79 ///////////////////////////////////////////////////////////////////////////// |
|
80 // CMyclientDlg dialog |
|
81 |
|
82 CMyclientDlg::CMyclientDlg(CWnd* pParent /*=NULL*/) |
|
83 : CDialog(CMyclientDlg::IDD, pParent) |
|
84 { |
|
85 //{{AFX_DATA_INIT(CMyclientDlg) |
|
86 // NOTE: the ClassWizard will add member initialization here |
|
87 //}}AFX_DATA_INIT |
|
88 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); |
|
89 } |
|
90 |
|
91 void CMyclientDlg::DoDataExchange(CDataExchange* pDX) |
|
92 { |
|
93 CDialog::DoDataExchange(pDX); |
|
94 //{{AFX_DATA_MAP(CMyclientDlg) |
|
95 DDX_Control(pDX, IDC_CHKLISTEN, m_ChkListen); |
|
96 DDX_Control(pDX, IDC_PORT, m_Port); |
|
97 DDX_Control(pDX, IDC_MACHINE, m_Machine); |
|
98 DDX_Control(pDX, IDC_CONNECT, m_Connect); |
|
99 DDX_Control(pDX, IDC_SEND, m_Send); |
|
100 DDX_Control(pDX, IDC_SENDMSG, m_SendMsg); |
|
101 DDX_Control(pDX, IDC_RECVLIST, m_RecvList); |
|
102 //}}AFX_DATA_MAP |
|
103 } |
|
104 |
|
105 BEGIN_MESSAGE_MAP(CMyclientDlg, CDialog) |
|
106 //{{AFX_MSG_MAP(CMyclientDlg) |
|
107 ON_WM_SYSCOMMAND() |
|
108 ON_WM_PAINT() |
|
109 ON_WM_QUERYDRAGICON() |
|
110 ON_BN_CLICKED(IDC_SEND, OnSend) |
|
111 ON_BN_CLICKED(IDC_CONNECT, OnConnect) |
|
112 //}}AFX_MSG_MAP |
|
113 END_MESSAGE_MAP() |
|
114 |
|
115 ///////////////////////////////////////////////////////////////////////////// |
|
116 // CMyclientDlg message handlers |
|
117 |
|
118 BOOL CMyclientDlg::OnInitDialog() |
|
119 { |
|
120 CDialog::OnInitDialog(); |
|
121 |
|
122 if (!AfxSocketInit()) |
|
123 { |
|
124 AfxMessageBox(IDP_SOCKETS_INIT_FAILED); |
|
125 exit(0); |
|
126 } |
|
127 |
|
128 // Add "About..." menu item to system menu. |
|
129 |
|
130 // IDM_ABOUTBOX must be in the system command range. |
|
131 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); |
|
132 ASSERT(IDM_ABOUTBOX < 0xF000); |
|
133 |
|
134 CMenu* pSysMenu = GetSystemMenu(FALSE); |
|
135 if (pSysMenu != NULL) |
|
136 { |
|
137 CString strAboutMenu; |
|
138 strAboutMenu.LoadString(IDS_ABOUTBOX); |
|
139 if (!strAboutMenu.IsEmpty()) |
|
140 { |
|
141 pSysMenu->AppendMenu(MF_SEPARATOR); |
|
142 pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); |
|
143 } |
|
144 } |
|
145 |
|
146 SetIcon(m_hIcon, TRUE); // Set big icon |
|
147 SetIcon(m_hIcon, FALSE); // Set small icon |
|
148 |
|
149 m_SendMsg.EnableWindow(FALSE); |
|
150 m_Send.EnableWindow(FALSE); |
|
151 |
|
152 m_Machine.SetWindowText("LON-PHILH01"); |
|
153 m_Port.SetWindowText("701"); |
|
154 |
|
155 hThreadHandle = 0; |
|
156 |
|
157 return TRUE; // return TRUE unless you set the focus to a control |
|
158 } |
|
159 |
|
160 void CMyclientDlg::OnSysCommand(UINT nID, LPARAM lParam) |
|
161 { |
|
162 if ((nID & 0xFFF0) == IDM_ABOUTBOX) |
|
163 { |
|
164 CAboutDlg dlgAbout; |
|
165 dlgAbout.DoModal(); |
|
166 } |
|
167 else |
|
168 { |
|
169 CDialog::OnSysCommand(nID, lParam); |
|
170 } |
|
171 } |
|
172 |
|
173 // If you add a minimize button to your dialog, you will need the code below |
|
174 // to draw the icon. For MFC applications using the document/view model, |
|
175 // this is automatically done for you by the framework. |
|
176 |
|
177 void CMyclientDlg::OnPaint() |
|
178 { |
|
179 if (IsIconic()) |
|
180 { |
|
181 CPaintDC dc(this); // device context for painting |
|
182 |
|
183 SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); |
|
184 |
|
185 // Center icon in client rectangle |
|
186 int cxIcon = GetSystemMetrics(SM_CXICON); |
|
187 int cyIcon = GetSystemMetrics(SM_CYICON); |
|
188 CRect rect; |
|
189 GetClientRect(&rect); |
|
190 int x = (rect.Width() - cxIcon + 1) / 2; |
|
191 int y = (rect.Height() - cyIcon + 1) / 2; |
|
192 |
|
193 // Draw the icon |
|
194 dc.DrawIcon(x, y, m_hIcon); |
|
195 } |
|
196 else |
|
197 { |
|
198 CDialog::OnPaint(); |
|
199 } |
|
200 } |
|
201 |
|
202 HCURSOR CMyclientDlg::OnQueryDragIcon() |
|
203 { |
|
204 return (HCURSOR) m_hIcon; |
|
205 } |
|
206 |
|
207 void CMyclientDlg::OnSend() |
|
208 { |
|
209 if (pSocket) |
|
210 { |
|
211 char szData[256]; |
|
212 m_SendMsg.GetWindowText(szData, 256); |
|
213 if (*szData) |
|
214 { |
|
215 if (pSocket->Send('X', szData, strlen(szData)) != ITS_OK) |
|
216 m_RecvList.AddString("Could not send."); |
|
217 } |
|
218 } |
|
219 } |
|
220 |
|
221 void CMyclientDlg::OnConnect() |
|
222 { |
|
223 bool IsConnected = false; |
|
224 |
|
225 pSocket = new CSTATSocket; |
|
226 if (pSocket) |
|
227 { |
|
228 CWaitCursor oWait; |
|
229 |
|
230 if (!m_ChkListen.GetCheck()) |
|
231 { |
|
232 // get machine name and port number |
|
233 char szMachine[256]; |
|
234 m_Machine.GetWindowText(szMachine, 251); |
|
235 strcat(szMachine, ":"); |
|
236 m_Port.GetWindowText(szMachine + strlen(szMachine), 4); |
|
237 |
|
238 if (pSocket->Connect(szMachine) == ITS_OK) |
|
239 { |
|
240 IsConnected = true; |
|
241 m_RecvList.AddString("Connected."); |
|
242 } |
|
243 else |
|
244 m_RecvList.AddString("Could not connect."); |
|
245 } |
|
246 else |
|
247 { |
|
248 int ret = 0; |
|
249 char szPort[4]; |
|
250 m_Port.GetWindowText(szPort, 4); |
|
251 if ((ret = pSocket->Listen(szPort)) == ITS_OK) |
|
252 { |
|
253 m_RecvList.AddString("Listening for connection..."); |
|
254 IsConnected = true; |
|
255 } |
|
256 else |
|
257 m_RecvList.AddString("Could not connect."); |
|
258 } |
|
259 |
|
260 if (IsConnected) |
|
261 { |
|
262 m_SendMsg.EnableWindow(TRUE); |
|
263 m_Send.EnableWindow(TRUE); |
|
264 m_Connect.EnableWindow(FALSE); |
|
265 m_ChkListen.EnableWindow(FALSE); |
|
266 m_Machine.EnableWindow(FALSE); |
|
267 m_Port.EnableWindow(FALSE); |
|
268 GetMessages(); |
|
269 } |
|
270 } |
|
271 else |
|
272 m_RecvList.AddString("Memory error."); |
|
273 } |
|
274 |
|
275 // thread entry point |
|
276 DWORD WINAPI ThreadProc(LPVOID lpParameter) |
|
277 { |
|
278 // lpParameter can be a pointer to a class to allow method calls |
|
279 CMyclientDlg *pDlg = (CMyclientDlg *)lpParameter; |
|
280 |
|
281 char ID = 0; |
|
282 unsigned long Length = 0; |
|
283 char *pData = NULL; |
|
284 char szBuffer[1024]; |
|
285 |
|
286 while(1) |
|
287 { |
|
288 if (pDlg->pSocket->Receive(&ID, &pData, &Length) == ITS_OK) |
|
289 { |
|
290 memset(szBuffer, 0, 1024); |
|
291 sprintf(szBuffer, "%c %ld %s", ID, Length, pData); |
|
292 |
|
293 pDlg->m_RecvList.AddString(szBuffer); |
|
294 pDlg->UpdateWindow(); |
|
295 } |
|
296 |
|
297 Sleep(100); |
|
298 } |
|
299 |
|
300 return 1; |
|
301 } |
|
302 |
|
303 void CMyclientDlg::GetMessages() |
|
304 { |
|
305 DWORD dwThreadID; |
|
306 if (!(hThreadHandle = CreateThread( NULL, // security attributes |
|
307 0, // stack size |
|
308 ThreadProc, // proc to call |
|
309 this, // proc parameter |
|
310 0, // creation flags |
|
311 &dwThreadID))) // thread identifier |
|
312 { |
|
313 AfxMessageBox("Could not create thread"); |
|
314 } |
|
315 } |
|
316 |
|
317 void CMyclientDlg::OnOK() |
|
318 { |
|
319 if (hThreadHandle) |
|
320 TerminateThread(hThreadHandle, -1); |
|
321 |
|
322 CDialog::OnOK(); |
|
323 } |