home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2500 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  5.0 KB  |  138 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import win32serviceutil
  5. import win32service
  6. import pywintypes
  7. import win32con
  8. import winerror
  9. from win32event import *
  10. from win32file import *
  11. from win32pipe import *
  12. from win32api import *
  13. from ntsecuritycon import *
  14. import servicemanager
  15. import traceback
  16. import thread
  17.  
  18. def ApplyIgnoreError(fn, args):
  19.     
  20.     try:
  21.         return apply(fn, args)
  22.     except error:
  23.         return None
  24.  
  25.  
  26.  
  27. class TestPipeService(win32serviceutil.ServiceFramework):
  28.     _svc_name_ = 'PyPipeTestService'
  29.     _svc_display_name_ = 'Python Pipe Test Service'
  30.     _svc_description_ = 'Tests Python service framework by receiving and echoing messages over a named pipe'
  31.     
  32.     def __init__(self, args):
  33.         win32serviceutil.ServiceFramework.__init__(self, args)
  34.         self.hWaitStop = CreateEvent(None, 0, 0, None)
  35.         self.overlapped = pywintypes.OVERLAPPED()
  36.         self.overlapped.hEvent = CreateEvent(None, 0, 0, None)
  37.         self.thread_handles = []
  38.  
  39.     
  40.     def CreatePipeSecurityObject(self):
  41.         sa = pywintypes.SECURITY_ATTRIBUTES()
  42.         sidEveryone = pywintypes.SID()
  43.         sidEveryone.Initialize(SECURITY_WORLD_SID_AUTHORITY, 1)
  44.         sidEveryone.SetSubAuthority(0, SECURITY_WORLD_RID)
  45.         sidCreator = pywintypes.SID()
  46.         sidCreator.Initialize(SECURITY_CREATOR_SID_AUTHORITY, 1)
  47.         sidCreator.SetSubAuthority(0, SECURITY_CREATOR_OWNER_RID)
  48.         acl = pywintypes.ACL()
  49.         acl.AddAccessAllowedAce(FILE_GENERIC_READ | FILE_GENERIC_WRITE, sidEveryone)
  50.         acl.AddAccessAllowedAce(FILE_ALL_ACCESS, sidCreator)
  51.         sa.SetSecurityDescriptorDacl(1, acl, 0)
  52.         return sa
  53.  
  54.     
  55.     def DoProcessClient(self, pipeHandle, tid):
  56.         
  57.         try:
  58.             d = ''
  59.             hr = winerror.ERROR_MORE_DATA
  60.             while hr == winerror.ERROR_MORE_DATA:
  61.                 (hr, thisd) = ReadFile(pipeHandle, 256)
  62.                 d = d + thisd
  63.             print 'Read', d
  64.             ok = 1
  65.         except error:
  66.             ok = 0
  67.         
  68.  
  69.         if ok:
  70.             WriteFile(pipeHandle, '%s (on thread %d) sent me %s' % (GetNamedPipeHandleState(pipeHandle)[4], tid, d))
  71.         ApplyIgnoreError(DisconnectNamedPipe, (pipeHandle,))
  72.         ApplyIgnoreError(CloseHandle, (pipeHandle,))
  73.  
  74.     
  75.     def ProcessClient(self, pipeHandle):
  76.         
  77.         try:
  78.             procHandle = GetCurrentProcess()
  79.             th = DuplicateHandle(procHandle, GetCurrentThread(), procHandle, 0, 0, win32con.DUPLICATE_SAME_ACCESS)
  80.             
  81.             try:
  82.                 self.thread_handles.append(th)
  83.                 
  84.                 try:
  85.                     return self.DoProcessClient(pipeHandle, th)
  86.                 except:
  87.                     traceback.print_exc()
  88.  
  89.             finally:
  90.                 self.thread_handles.remove(th)
  91.  
  92.         except:
  93.             traceback.print_exc()
  94.  
  95.  
  96.     
  97.     def SvcStop(self):
  98.         self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
  99.         SetEvent(self.hWaitStop)
  100.  
  101.     
  102.     def SvcDoRun(self):
  103.         servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STARTED, (self._svc_name_, ''))
  104.         num_connections = 0
  105.         while None:
  106.             pipeHandle = CreateNamedPipe('\\\\.\\pipe\\PyPipeTest', PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, PIPE_TYPE_MESSAGE | PIPE_READMODE_BYTE, PIPE_UNLIMITED_INSTANCES, 0, 0, 6000, self.CreatePipeSecurityObject())
  107.             
  108.             try:
  109.                 hr = ConnectNamedPipe(pipeHandle, self.overlapped)
  110.             except error:
  111.                 details = None
  112.                 print 'Error connecting pipe!', details
  113.                 CloseHandle(pipeHandle)
  114.                 break
  115.  
  116.             if hr == winerror.ERROR_PIPE_CONNECTED:
  117.                 SetEvent(self.overlapped.hEvent)
  118.             
  119.             rc = WaitForMultipleObjects((self.hWaitStop, self.overlapped.hEvent), 0, INFINITE)
  120.             if rc == WAIT_OBJECT_0:
  121.                 break
  122.                 continue
  123.             thread.start_new_thread(self.ProcessClient, (pipeHandle,))
  124.             num_connections = num_connections + 1
  125.             continue
  126.             Sleep(500)
  127.             while self.thread_handles:
  128.                 self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING, 5000)
  129.                 print 'Waiting for %d threads to finish...' % len(self.thread_handles)
  130.                 WaitForMultipleObjects(self.thread_handles, 1, 3000)
  131.             servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STOPPED, (self._svc_name_, ' after processing %d connections' % (num_connections,)))
  132.             return None
  133.  
  134.  
  135. if __name__ == '__main__':
  136.     win32serviceutil.HandleCommandLine(TestPipeService)
  137.  
  138.