Changeset 35454 in vbox for trunk/src/VBox/Main/webservice
- Timestamp:
- Jan 10, 2011 12:06:19 PM (14 years ago)
- Location:
- trunk/src/VBox/Main/webservice
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Main/webservice/vboxweb.cpp
r35154 r35454 6 6 * server, to which clients can connect. 7 7 * 8 * Copyright (C) 2006-201 0Oracle Corporation8 * Copyright (C) 2006-2011 Oracle Corporation 9 9 * 10 10 * This file is part of VirtualBox Open Source Edition (OSE), as … … 22 22 // vbox headers 23 23 #include <VBox/com/com.h> 24 #include <VBox/com/array.h> 24 25 #include <VBox/com/ErrorInfo.h> 25 26 #include <VBox/com/errorprint.h> 26 27 #include <VBox/com/EventQueue.h> 28 #include <VBox/com/listeners.h> 27 29 #include <VBox/VBoxAuth.h> 28 30 #include <VBox/version.h> … … 86 88 ****************************************************************************/ 87 89 90 ComPtr<IVirtualBoxClient> g_pVirtualBoxClient = NULL; 88 91 ComPtr<IVirtualBox> g_pVirtualBox = NULL; 89 92 … … 98 101 int g_iWatchdogCheckInterval = 5; 99 102 100 const char *g_pcszBindToHost = NULL; // host; NULL = current machine103 const char *g_pcszBindToHost = NULL; // host; NULL = localhost 101 104 unsigned int g_uBindToPort = 18083; // port 102 105 unsigned int g_uBacklog = 100; // backlog = max queue size for requests … … 125 128 // this mutex protects the auth lib and authentication 126 129 util::WriteLockHandle *g_pAuthLibLockHandle; 130 131 // this mutex protects the global VirtualBox reference 132 util::RWLockHandle *g_pVirtualBoxLockHandle; 127 133 128 134 // this mutex protects all of the below … … 472 478 } 473 479 480 /**************************************************************************** 481 * 482 * VirtualBoxClient event listener 483 * 484 ****************************************************************************/ 485 486 class VirtualBoxClientEventListener 487 { 488 public: 489 VirtualBoxClientEventListener() 490 { 491 } 492 493 virtual ~VirtualBoxClientEventListener() 494 { 495 } 496 497 STDMETHOD(HandleEvent)(VBoxEventType_T aType, IEvent *aEvent) 498 { 499 switch (aType) 500 { 501 case VBoxEventType_OnVBoxSVCAvailabilityChanged: 502 { 503 ComPtr<IVBoxSVCAvailabilityChangedEvent> pVSACEv = aEvent; 504 Assert(pVSACEv); 505 BOOL fAvailable = FALSE; 506 pVSACEv->COMGETTER(Available)(&fAvailable); 507 if (!fAvailable) 508 { 509 WebLog("VBoxSVC became unavailable\n"); 510 { 511 util::AutoWriteLock vlock(g_pVirtualBoxLockHandle COMMA_LOCKVAL_SRC_POS); 512 g_pVirtualBox = NULL; 513 } 514 { 515 // we're messing with sessions, so lock them 516 util::AutoWriteLock lock(g_pSessionsLockHandle COMMA_LOCKVAL_SRC_POS); 517 WEBDEBUG(("SVC unavailable: deleting %d sessions\n", g_mapSessions.size())); 518 519 SessionsMap::iterator it = g_mapSessions.begin(), 520 itEnd = g_mapSessions.end(); 521 while (it != itEnd) 522 { 523 WebServiceSession *pSession = it->second; 524 WEBDEBUG(("SVC unavailable: Session %llX stale, deleting\n", pSession->getID())); 525 delete pSession; 526 it = g_mapSessions.begin(); 527 } 528 } 529 } 530 else 531 { 532 WebLog("VBoxSVC became available\n"); 533 util::AutoWriteLock vlock(g_pVirtualBoxLockHandle COMMA_LOCKVAL_SRC_POS); 534 HRESULT hrc = g_pVirtualBoxClient->COMGETTER(VirtualBox)(g_pVirtualBox.asOutParam()); 535 AssertComRC(hrc); 536 } 537 break; 538 } 539 default: 540 AssertFailed(); 541 } 542 543 return S_OK; 544 } 545 546 private: 547 }; 548 549 typedef ListenerImpl<VirtualBoxClientEventListener> VirtualBoxClientEventListenerImpl; 550 551 VBOX_LISTENER_DECLARE(VirtualBoxClientEventListenerImpl) 552 474 553 /** 475 554 * Implementation for WEBLOG macro defined in vboxweb.h; this prints a message … … 561 640 int m, s; // master and slave sockets 562 641 m = soap_bind(&soap, 563 g_pcszBindToHost , // host: current machine564 g_uBindToPort, 565 g_uBacklog); // backlog = max queue size for requests642 g_pcszBindToHost ? g_pcszBindToHost : "localhost", // safe default host 643 g_uBindToPort, // port 644 g_uBacklog); // backlog = max queue size for requests 566 645 if (m < 0) 567 646 WebLogSoapError(&soap); … … 624 703 * @return 625 704 */ 626 int main(int argc, char *argv[])705 int main(int argc, char *argv[]) 627 706 { 628 707 // initialize runtime … … 648 727 { 649 728 case 'H': 650 g_pcszBindToHost = ValueUnion.psz; 729 if (!ValueUnion.psz || !*ValueUnion.psz) 730 { 731 /* Normalize NULL/empty string to NULL, which will be 732 * interpreted as "localhost" below. */ 733 g_pcszBindToHost = NULL; 734 } 735 else 736 g_pcszBindToHost = ValueUnion.psz; 651 737 break; 652 738 … … 723 809 return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to initialize COM! hrc=%Rhrc\n", hrc); 724 810 725 ComPtr<ISession> session; 726 727 hrc = g_pVirtualBox.createLocalObject(CLSID_VirtualBox); 811 hrc = g_pVirtualBoxClient.createInprocObject(CLSID_VirtualBoxClient); 728 812 if (FAILED(hrc)) 729 RTMsgError("failed to create the VirtualBox object!"); 730 else 731 { 732 hrc = session.createInprocObject(CLSID_Session); 733 if (FAILED(hrc)) 734 RTMsgError("failed to create a session object!"); 735 } 736 737 if (FAILED(hrc)) 738 { 813 { 814 RTMsgError("failed to create the VirtualBoxClient object!"); 739 815 com::ErrorInfo info; 740 816 if (!info.isFullAvailable() && !info.isBasicAvailable()) … … 748 824 } 749 825 826 hrc = g_pVirtualBoxClient->COMGETTER(VirtualBox)(g_pVirtualBox.asOutParam()); 827 if (FAILED(hrc)) 828 { 829 RTMsgError("Failed to get VirtualBox object (rc=%Rhrc)!", hrc); 830 return RTEXITCODE_FAILURE; 831 } 832 833 /* VirtualBoxClient events registration. */ 834 IEventListener *vboxClientListener = NULL; 835 { 836 ComPtr<IEventSource> pES; 837 CHECK_ERROR(g_pVirtualBoxClient, COMGETTER(EventSource)(pES.asOutParam())); 838 vboxClientListener = new VirtualBoxClientEventListenerImpl(); 839 com::SafeArray<VBoxEventType_T> eventTypes; 840 eventTypes.push_back(VBoxEventType_OnVBoxSVCAvailabilityChanged); 841 CHECK_ERROR(pES, RegisterListener(vboxClientListener, ComSafeArrayAsInParam(eventTypes), true)); 842 } 843 750 844 // create the global mutexes 751 845 g_pAuthLibLockHandle = new util::WriteLockHandle(util::LOCKCLASS_WEBSERVICE); 846 g_pVirtualBoxLockHandle = new util::RWLockHandle(util::LOCKCLASS_WEBSERVICE); 752 847 g_pSessionsLockHandle = new util::WriteLockHandle(util::LOCKCLASS_WEBSERVICE); 753 848 g_pThreadsLockHandle = new util::RWLockHandle(util::LOCKCLASS_OBJECTSTATE); … … 788 883 if (RT_FAILURE(rc)) 789 884 RTMsgError("processEventQueue -> %Rrc", rc); 885 } 886 887 /* VirtualBoxClient events unregistration. */ 888 if (vboxClientListener) 889 { 890 ComPtr<IEventSource> pES; 891 CHECK_ERROR(g_pVirtualBoxClient, COMGETTER(EventSource)(pES.asOutParam())); 892 if (!pES.isNull()) 893 CHECK_ERROR(pES, UnregisterListener(vboxClientListener)); 894 vboxClientListener->Release(); 790 895 } 791 896 … … 1124 1229 1125 1230 int WebServiceSession::authenticate(const char *pcszUsername, 1126 const char *pcszPassword) 1231 const char *pcszPassword, 1232 IVirtualBox **ppVirtualBox) 1127 1233 { 1128 1234 int rc = VERR_WEB_NOT_AUTHENTICATED; 1235 ComPtr<IVirtualBox> pVirtualBox; 1236 { 1237 util::AutoReadLock vlock(g_pVirtualBoxLockHandle COMMA_LOCKVAL_SRC_POS); 1238 pVirtualBox = g_pVirtualBox; 1239 } 1240 pVirtualBox.queryInterfaceTo(ppVirtualBox); 1241 if (pVirtualBox.isNull()) 1242 return rc; 1129 1243 1130 1244 util::AutoReadLock lock(g_pAuthLibLockHandle COMMA_LOCKVAL_SRC_POS); … … 1139 1253 // retrieve authentication library from system properties 1140 1254 ComPtr<ISystemProperties> systemProperties; 1141 g_pVirtualBox->COMGETTER(SystemProperties)(systemProperties.asOutParam());1255 pVirtualBox->COMGETTER(SystemProperties)(systemProperties.asOutParam()); 1142 1256 1143 1257 com::Bstr authLibrary; … … 1219 1333 // (and of which IWebsessionManager::getSessionObject returns a managed object reference) 1220 1334 ComPtr<ISession> session; 1221 if (FAILED(rc = session.createInprocObject(CLSID_Session))) 1335 rc = g_pVirtualBoxClient->COMGETTER(Session)(session.asOutParam()); 1336 if (FAILED(rc)) 1222 1337 { 1223 1338 WEBDEBUG(("ERROR: cannot create session object!")); … … 1538 1653 _vbox__IManagedObjectRef_USCOREgetInterfaceNameResponse *resp) 1539 1654 { 1540 HRESULT rc = S OAP_OK;1655 HRESULT rc = S_OK; 1541 1656 WEBDEBUG(("-- entering %s\n", __FUNCTION__)); 1542 1657 … … 1573 1688 _vbox__IManagedObjectRef_USCOREreleaseResponse *resp) 1574 1689 { 1575 HRESULT rc = S OAP_OK;1690 HRESULT rc = S_OK; 1576 1691 WEBDEBUG(("-- entering %s\n", __FUNCTION__)); 1577 1692 … … 1640 1755 */ 1641 1756 int __vbox__IWebsessionManager_USCORElogon( 1642 struct soap *,1757 struct soap *soap, 1643 1758 _vbox__IWebsessionManager_USCORElogon *req, 1644 1759 _vbox__IWebsessionManager_USCORElogonResponse *resp) 1645 1760 { 1646 HRESULT rc = S OAP_OK;1761 HRESULT rc = S_OK; 1647 1762 WEBDEBUG(("-- entering %s\n", __FUNCTION__)); 1648 1763 … … 1655 1770 // in the global map automatically 1656 1771 WebServiceSession *pSession = new WebServiceSession(); 1772 ComPtr<IVirtualBox> pVirtualBox; 1657 1773 1658 1774 // authenticate the user 1659 1775 if (!(pSession->authenticate(req->username.c_str(), 1660 req->password.c_str()))) 1776 req->password.c_str(), 1777 pVirtualBox.asOutParam()))) 1661 1778 { 1662 1779 // in the new session, create a managed object reference (MOR) for the … … 1664 1781 // that it will be implicitly be included in all future requests of this 1665 1782 // webservice client 1666 ComPtr<IUnknown> p2 = g_pVirtualBox; 1783 ComPtr<IUnknown> p2 = pVirtualBox; 1784 if (pVirtualBox.isNull() || p2.isNull()) 1785 { 1786 rc = E_FAIL; 1787 break; 1788 } 1667 1789 ManagedObjectRef *pRef = new ManagedObjectRef(*pSession, 1668 1790 p2, // IUnknown *pobjUnknown 1669 g_pVirtualBox,// void *pobjInterface1791 pVirtualBox, // void *pobjInterface 1670 1792 COM_IIDOF(IVirtualBox), 1671 1793 g_pcszIVirtualBox); … … 1673 1795 WEBDEBUG(("VirtualBox object ref is %s\n", resp->returnval.c_str())); 1674 1796 } 1797 else 1798 rc = E_FAIL; 1675 1799 } while (0); 1676 1800 … … 1690 1814 _vbox__IWebsessionManager_USCOREgetSessionObjectResponse *resp) 1691 1815 { 1692 HRESULT rc = S OAP_OK;1816 HRESULT rc = S_OK; 1693 1817 WEBDEBUG(("-- entering %s\n", __FUNCTION__)); 1694 1818 … … 1723 1847 _vbox__IWebsessionManager_USCORElogoffResponse *resp) 1724 1848 { 1725 HRESULT rc = S OAP_OK;1849 HRESULT rc = S_OK; 1726 1850 WEBDEBUG(("-- entering %s\n", __FUNCTION__)); 1727 1851 -
trunk/src/VBox/Main/webservice/vboxweb.h
r32780 r35454 3 3 * header file for "real" web server code. 4 4 * 5 * Copyright (C) 2006-201 0Oracle Corporation5 * Copyright (C) 2006-2011 Oracle Corporation 6 6 * 7 7 * This file is part of VirtualBox Open Source Edition (OSE), as … … 134 134 135 135 int authenticate(const char *pcszUsername, 136 const char *pcszPassword); 136 const char *pcszPassword, 137 IVirtualBox **ppVirtualBox); 137 138 138 139 ManagedObjectRef* findRefFromPtr(const IUnknown *pObject);
Note:
See TracChangeset
for help on using the changeset viewer.