1 /* Copyright 2000, International Business Machines Corporation and others.
4 This software has been released under the terms of the IBM Public
5 License. For details, see the LICENSE file in the top-level source
6 directory or online at http://www.openafs.org/dl/license10.html
8 // ProgBarDlg.cpp : implementation file
13 #include "WinAfsLoad.h"
14 #include "ProgBarDlg.h"
16 //#include <process.h>
21 static char THIS_FILE[] = __FILE__;
24 /////////////////////////////////////////////////////////////////////////////
28 CProgBarDlg::CProgBarDlg(CWnd* pParent /*=NULL*/)
29 : CDialog(CProgBarDlg::IDD, pParent)
31 //{{AFX_DATA_INIT(CProgBarDlg)
32 m_sAuthenicateTime = _T("");
33 m_sBackgroundTime = _T("");
34 m_sMountTime = _T("");
39 CProgBarDlg::~CProgBarDlg()
41 // nitwit do not expect any local variables to hang around
44 void CProgBarDlg::DoDataExchange(CDataExchange* pDX)
46 CDialog::DoDataExchange(pDX);
47 //{{AFX_DATA_MAP(CProgBarDlg)
48 DDX_Control(pDX, IDC_CHECKAUTHENICATION, m_cAuthenicate);
49 DDX_Control(pDX, IDC_CHECKMOUNT, m_cMount);
50 DDX_Control(pDX, IDC_CHECKBACKGROUND, m_cBackground);
51 DDX_Control(pDX, IDC_STATUSREGION, m_cStatusRegion);
52 DDX_Control(pDX, IDC_COMPLETE_MOUNT, m_cCompleteMount);
53 DDX_Control(pDX, IDC_COMPLETE_ENABLE, m_cCompleteEnable);
54 DDX_Control(pDX, IDC_COMPLETE_AUTH, m_cCompleteAuth);
55 DDX_Text(pDX, IDC_TIMEAUTHENICATION, m_sAuthenicateTime);
56 DDX_Text(pDX, IDC_TIMEBACKGROUND, m_sBackgroundTime);
57 DDX_Text(pDX, IDC_TIMEMOUNT, m_sMountTime);
62 BEGIN_MESSAGE_MAP(CProgBarDlg, CDialog)
63 //{{AFX_MSG_MAP(CProgBarDlg)
66 ON_MESSAGE( WM_PROGRESSPARM,OnParm)
70 ON_MESSAGE(WSA_EVENT, OnWSAEvent)
73 /////////////////////////////////////////////////////////////////////////////
74 // CProgBarDlg message handlers
76 BOOL CProgBarDlg::OnInitDialog()
78 CDialog::OnInitDialog();
80 // Set the icon for this dialog. The framework does this automatically
81 // when the application's main window is not a dialog
82 SetIcon(m_hIcon, TRUE); // Set big icon
83 SetIcon(m_hIcon, FALSE); // Set small icon
85 // TODO: Add extra initialization here
90 m_Socket = INVALID_SOCKET;
92 m_cBackground.GetWindowText(m_sDefaultBackground.GetBuffer(32),31);
93 m_cAuthenicate.GetWindowText(m_sDefaultAuthenicate.GetBuffer(32),32);
94 m_cMount.GetWindowText(m_sDefaultMount.GetBuffer(32),32);
95 return TRUE; // return TRUE unless you set the focus to a control
98 // If you add a minimize button to your dialog, you will need the code below
99 // to draw the icon. For MFC applications using the document/view model,
100 // this is automatically done for you by the framework.
103 HCURSOR CProgBarDlg::OnQueryDragIcon()
105 return (HCURSOR) m_hIcon;
108 void CProgBarDlg::OnTimer(UINT nIDEvent)
110 // TODO: Add your message handler code here and/or call default
113 m_iOrientation=++m_iOrientation % 2;
115 m_cStatusRegion.GetWindowRect(&s1);
117 InvalidateRect(&s1,TRUE);
120 case WM_PROGRESSTIMER:
122 CTimeSpan elapsed=::CTime::GetCurrentTime()-m_StartTime;
126 m_sBackgroundTime.Format("%02d:%02d",elapsed.GetTotalMinutes()
127 ,elapsed.GetTotalSeconds()-60*elapsed.GetTotalMinutes());
131 m_sAuthenicateTime.Format("%02d:%02d",elapsed.GetTotalMinutes()
132 ,elapsed.GetTotalSeconds()-60*elapsed.GetTotalMinutes());
136 m_sMountTime.Format("%02d:%02d",elapsed.GetTotalMinutes()
137 ,elapsed.GetTotalSeconds()-60*elapsed.GetTotalMinutes());
148 CDialog::OnTimer(nIDEvent);
151 LRESULT CProgBarDlg::OnParm(WPARAM wp, LPARAM lp)
158 char *msg=(char *)lp;
160 m_cBackground.SetWindowText(msg);
163 m_cAuthenicate.SetWindowText(msg);
166 m_cMount.SetWindowText(msg);
172 m_nTimer=SetTimer(WM_PROGRESSTIMER,1000,0);
177 m_cBackground.SetWindowText(m_sDefaultBackground);
178 m_cAuthenicate.SetWindowText(m_sDefaultAuthenicate);
179 m_cMount.SetWindowText(m_sDefaultMount);
184 m_sBackgroundTime="";
185 m_sAuthenicateTime="";
191 m_cBackground.ModifyStyle(WS_DISABLED,0,0);
193 m_cBackground.ModifyStyle(0,WS_DISABLED,0);
196 m_cAuthenicate.ModifyStyle(WS_DISABLED,0,0);
198 m_cAuthenicate.ModifyStyle(0,WS_DISABLED,0);
200 m_cMount.ModifyStyle(WS_DISABLED,0,0);
202 m_cMount.ModifyStyle(0,WS_DISABLED,0);
204 m_nTimer=SetTimer(WM_PROGRESSTIMER,1000,0);
213 if (m_Option & m_Current)
215 CWnd* pWnd=GetDlgItem(IDC_TIMEBACKGROUND);
218 m_StartTime=CTime::GetCurrentTime();
219 OnTimer(WM_PROGRESSTIMER);
224 if (m_Option & m_Current)
226 OnTimer(WM_PROGRESSTIMER);
227 CWnd* pWnd=GetDlgItem(IDC_TIMEAUTHENICATION);
230 m_StartTime=CTime::GetCurrentTime();
235 if (m_Option & m_Current)
237 OnTimer(WM_PROGRESSTIMER);
238 CWnd* pWnd=GetDlgItem(IDC_TIMEMOUNT);
241 m_StartTime=CTime::GetCurrentTime();
253 m_cStatusRegion.GetWindowRect(&s1);
255 InvalidateRect(&s1,TRUE);
259 BOOL CProgBarDlg::Create()
261 return CDialog::Create(IDD, m_pParent);
264 void CProgBarDlg::PostNcDestroy()
266 // TODO: Add your specialized code here and/or call the base class
267 if (m_Socket != INVALID_SOCKET)
268 WSAAsyncSelect(m_Socket, m_hWnd, 0, 0); //cancel out messages
269 if (m_Socket != INVALID_SOCKET)
270 closesocket(m_Socket);
274 void CProgBarDlg::OnDestroy()
278 CDialog::OnDestroy();
279 // TODO: Add your message handler code here
282 VOID CProgBarDlg::DisConnect()
284 if (m_Socket != INVALID_SOCKET)
286 shutdown(m_Socket,3);
287 char cBuffer[AFS_MAX_MSG_LEN];
289 int loop=SOCKETIO/250;
290 while ((uStatus !=SOCKET_ERROR)&&(uStatus!=0) &&(loop-- >0))
293 uStatus = recv(m_Socket, (char FAR *)&cBuffer, AFS_MAX_MSG_LEN, 0 );
295 closesocket(m_Socket);
296 m_Socket = INVALID_SOCKET;
298 WSAAsyncSelect(m_Socket, m_hWnd, 0, 0); //cancel out messages
299 LOG("Socket Disconnect");
302 typedef struct SoCkErR {
307 SOC_ERR socket_error[] = {
308 { WSANOTINITIALISED , "A successful WSAStartup must occur before using this function."},
309 { WSAENETDOWN , "The Windows Sockets implementation has detected that the network "\
310 "subsystem has failed."},
311 { WSAENOTCONN , "The socket is not connected."},
312 { WSAEINTR , "The (blocking) call was canceled using WSACancelBlockingCall."},
313 { WSAEINPROGRESS , "A blocking Windows Sockets operation is in progress."},
314 { WSAENOTSOCK , "The descriptor is not a socket."},
315 { WSAEOPNOTSUPP , "MSG_OOB was specified, but the socket is not of type SOCK_STREAM."},
316 { WSAESHUTDOWN , "The socket has been shut down; it is not possible to recv on a socket "\
317 "after shutdown has been invoked with how set to 0 or 2."},
318 { WSAEWOULDBLOCK , "The socket is marked as nonblocking and the receive operation would block."},
319 { WSAEMSGSIZE , "The datagram was too large to fit into the specified buffer and was truncated."},
320 { WSAEINVAL , "The socket has not been bound with bind."},
321 { WSAECONNABORTED , "The virtual circuit was aborted due to timeout or other failure."},
322 { WSAECONNRESET , "The virtual circuit was reset by the remote side."},
323 { WSAEACCES , "The requested address is a broadcast address, but the appropriate flag was not set."},
324 { WSAEFAULT , "The buf argument is not in a valid part of the user address space."},
325 { WSAENETRESET , "The connection must be reset because the Windows Sockets implementation dropped it."},
326 { WSAENOBUFS , "The Windows Sockets implementation reports a buffer deadlock."},
327 { WSAENOTCONN , "The socket is not connected."},
328 { WSAEWOULDBLOCK , " The socket is marked as nonblocking and the requested operation would block."},
329 { -1 , "Unknown Socket Error"}
332 LPCSTR show_socket_error()
334 INT soc_err=WSAGetLastError();
335 SOC_ERR *lperr = &socket_error[0];
336 while (1 && lperr->soc_err != -1)
338 if (lperr->soc_err == soc_err)
340 return lperr->szSockErr;
344 return "Unknown Socket Error";
348 BOOL CProgBarDlg::Connect(CString &sStatus)
351 UINT dwSize = sizeof(szBuff);
352 SOCKADDR_IN socketAddress;
353 m_Socket = socket( AF_INET, SOCK_DGRAM, 0);
354 if (m_Socket == INVALID_SOCKET) {
355 sStatus="Socket() failed";
358 if (gethostname(szBuff, dwSize)!=0)
360 sStatus.Format("Get Host Name - %s", show_socket_error());
363 socketAddress.sin_family = AF_INET;
364 socketAddress.sin_addr.s_addr = INADDR_ANY;
365 socketAddress.sin_port = htons(AFS_MSG_PORT); /* Convert to network ordering */
366 if (bind(m_Socket, (struct sockaddr FAR *) &socketAddress, sizeof(socketAddress)) == SOCKET_ERROR) {
367 sStatus.Format("Bind :%s", show_socket_error());
370 if ((m_uStatus = WSAAsyncSelect( m_Socket,GetSafeHwnd(), WSA_EVENT, FD_READ | FD_CLOSE )) > 0) {
371 WSAAsyncSelect( m_Socket, GetSafeHwnd(), 0, 0);
372 sStatus.Format("Get Host Name: ", show_socket_error());
376 sStatus="Success Connect";
377 LOG("Socket Connect");
381 LRESULT CProgBarDlg::OnWSAEvent(WPARAM wParam, LPARAM lParam)
384 if (WSAGETSELECTERROR( lParam ) != 0) {
385 // m_sLog.Format("Read Failure status=%d\r\n", WSAGETSELECTERROR(lParam));
386 WSAAsyncSelect( m_Socket,GetSafeHwnd(), 0, 0);
387 m_uStatus=WSAGETSELECTERROR( lParam );
388 LOG("WSA Select error");
391 switch (WSAGETSELECTEVENT(lParam))
394 wParam=Decode(wParam,sLog);
395 LOG("OnWSAEvent FD_READ(%d)",wParam);
396 CWINAFSLOADAPP->WSANotifyFromUI(wParam,(const char *)sLog);
399 LOG("OnWSAEvent FD_CLOSE(%d)",wParam);
402 LOG("OnWSAEvent FD_WRITE(%d)",wParam);
405 LOG("OnWSAEvent Unknown Response");
411 // this routine will set the notfication return (wParam) for WinAfsLoadDlg to receive
412 // if problem with decoding result return AFS_EXITCODE_GENERAL_FAILURE
413 // Other returns are:
414 // AFS_EXITCODE_NORMAL(0) ,AFS_EXITCODE_PANIC(2) ,AFS_EXITCODE_NETWORK_FAILURE(3)
415 UINT CProgBarDlg::Decode(WPARAM wParam,CString &sLog)
417 m_bReadyToSend=FALSE;
418 m_uStatus = recv((SOCKET)wParam, (char FAR *)&m_buffer, AFS_MAX_MSG_LEN, 0 );
420 sLog.Format("Receive Error: %s", show_socket_error());
421 return AFS_EXITCODE_GENERAL_FAILURE;
423 SOCKETTYPE *p=(SOCKETTYPE *)m_buffer;
424 switch (p->header.msgtype)
426 case AFS_MSG_STATUS_CHANGE:
428 _afsMsg_statChange *sp=(_afsMsg_statChange *)p;
429 switch (sp->exitCode)
431 case AFS_EXITCODE_NORMAL:
433 if ((m_uStatus = WSAAsyncSelect( m_Socket,GetSafeHwnd(),WSA_EVENT, FD_READ | FD_CLOSE )) > 0) {
434 WSAAsyncSelect( m_Socket, GetSafeHwnd(), 0, 0);
435 sLog.Format("WSAAsyncSelect: %s", show_socket_error());
436 return AFS_EXITCODE_GENERAL_FAILURE;
438 int len=sp->hdr.length-sizeof(*sp);
441 char *x=new char[len+1];
442 strncpy(x,&(sp->message),len);
444 sLog.Format("AFSD operation normal Message#%s#",x);
447 sLog="AFSD operation normal";
451 case AFS_EXITCODE_PANIC:
452 sLog="AFS Client Console Panic Exit";
454 case AFS_EXITCODE_NETWORK_FAILURE:
455 sLog="AFS Client Network Failure Exit";
458 sLog.Format("Receive Error: Messsage format(exit code)=%0x",p->change.exitCode);
459 p->change.exitCode=AFS_EXITCODE_GENERAL_FAILURE;
462 return (p->change.exitCode);
465 if ((m_uStatus = WSAAsyncSelect( m_Socket, GetSafeHwnd(),WSA_EVENT, FD_READ | FD_CLOSE )) > 0) {
466 WSAAsyncSelect( m_Socket, GetSafeHwnd(), 0, 0);
467 sLog.Format("WSAAsyncSelect: %s", show_socket_error());
468 return AFS_EXITCODE_GENERAL_FAILURE;
470 return AFS_EXITCODE_GENERAL_FAILURE;
472 sLog.Format("Receive Error: Messsage format=%0x",p->header.msgtype);
473 return AFS_EXITCODE_GENERAL_FAILURE;
479 void CProgBarDlg::OnPaint()
481 CPaintDC dc(this); // device context for painting
483 // TODO: Add your message handler code here
484 //m_iCurrent is the Current item (1,2,4)
485 //m_iOption is which are active
487 CBrush brBack,brGreen,brYellow;
488 brBack.CreateSolidBrush(GetSysColor(COLOR_MENU));
489 brGreen.CreateSolidBrush(RGB(0,255,0));
490 brYellow.CreateSolidBrush(RGB(255,255,0));
495 SetShape(shape,m_Current,m_iOrientation);
496 dc.SelectObject(&brYellow);
498 SetShape(shape,m_Option & 2,0);
501 dc.SelectObject(&brBack);
504 SetShape(shape,m_Option & 4,0);
507 dc.SelectObject(&brBack);
512 SetShape(shape,m_Option & 1,0);
515 dc.SelectObject(&brGreen);
518 SetShape(shape,m_Current,m_iOrientation);
521 dc.SelectObject(&brYellow);
524 SetShape(shape,m_Option & 4,0);
527 dc.SelectObject(&brBack);
532 SetShape(shape,m_Option & 1,0);
535 dc.SelectObject(&brGreen);
538 SetShape(shape,m_Option & 2,0);
541 dc.SelectObject(&brGreen);
544 SetShape(shape,m_Current,m_iOrientation);
547 dc.SelectObject(&brYellow);
554 // Do not call CDialog::OnPaint() for painting messages
557 void CProgBarDlg::SetShape(CRect &shape,int option,int orientation)
563 m_cCompleteEnable.GetWindowRect(&s1);
566 m_cCompleteAuth.GetWindowRect(&s1);
569 m_cCompleteMount.GetWindowRect(&s1);
576 int delta=(s1.Width()-s1.Height())/2;
578 s2.bottom=s1.bottom+delta;
579 s2.left=s1.left+delta;
580 s2.right=s1.right-delta;
582 rect.bottom=s2.bottom;