266 lines
No EOL
8.4 KiB
Text
266 lines
No EOL
8.4 KiB
Text
;********************************
|
|
;*
|
|
;* lweb_file_cache.pbi
|
|
;*
|
|
;* LiHaSo Webserver Modul In Memory File Cache (Obsolete and replaced by lcmf_main.pbi)
|
|
;*
|
|
;* It Could be configured Globaly and by Virtualhost
|
|
;*
|
|
|
|
DeclareModule lweb_FC
|
|
Enumeration Server_Type
|
|
#Type_Main ;Main Server
|
|
#Type_Virtual_Main ;Shared Virtual Server which get Data from Main
|
|
#Type_Virtual ;Standalone Virtual Server which has own Memory Area
|
|
EndEnumeration
|
|
|
|
;{ Error Enumeration
|
|
#Err_Unknown = -1
|
|
#Err_Server_Not_Found = -2
|
|
#Err_Server_File_Not_Found = -3
|
|
;}
|
|
|
|
Declare.s FC_Config_Get(UUID.s)
|
|
Declare.i FC_Config_Set(UUID.s, ConfigXML.s)
|
|
Declare.i FC_Create(UUID.s, Type.i = #Type_Main)
|
|
Declare.i FC_Destroy(UUID.s)
|
|
Declare.s FC_Status_Get(UUID.s)
|
|
Declare.i FC_GetFile(UUID.s, FileName.s)
|
|
Declare.i FC_SetFile(UUID.s, FileName.s, Memory.i, Size.i)
|
|
EndDeclareModule
|
|
|
|
Module lweb_FC
|
|
Structure St_Jobs
|
|
Type.i
|
|
Memory.i
|
|
EndStructure
|
|
|
|
Structure St_File
|
|
lifetime.i
|
|
timer.i
|
|
size.i
|
|
memoryID.i
|
|
ThreadID.i
|
|
Accesed.i
|
|
Mutex.i
|
|
Semaphore.i
|
|
List Job.St_Jobs()
|
|
EndStructure
|
|
|
|
Structure St_FC_Config
|
|
max_life_time.i
|
|
max_total_size.i
|
|
max_file_size.i
|
|
current_total_size.i
|
|
enabled.b
|
|
EndStructure
|
|
|
|
Structure St_Server
|
|
VServer_UUID.s
|
|
Log_UUID.s
|
|
Status.s
|
|
Thread.i
|
|
Semaphore.i
|
|
Config.St_FC_Config
|
|
Map Files.St_File()
|
|
EndStructure
|
|
|
|
Structure St_CacheServers
|
|
Main_UUID.s
|
|
Log_UUID.s
|
|
Status.s
|
|
Thread.i
|
|
Map Virtual.St_Server()
|
|
EndStructure
|
|
|
|
Global Var_FC_Server.St_CacheServers
|
|
|
|
Declare.i Cache_Cleaner(UUID.s)
|
|
Declare.i Cache_Server(UUID.s)
|
|
Declare.i Cache_File(UUID.s)
|
|
|
|
Procedure.s FC_Config_Get(UUID.s)
|
|
Protected XMLString.s, XMLID.i
|
|
If FindMapElement(FC\Virtual(), UUID)
|
|
XMLID = CreateXML(#PB_Any)
|
|
InsertXMLMap(RootXMLNode(XMLID), FC\Virtual\
|
|
Else
|
|
ProcedureReturn Str(#Err_Server_Not_Found)
|
|
EndIf
|
|
|
|
EndProcedure
|
|
|
|
Procedure.i FC_Config_Set(UUID.s, ConfigXML.s)
|
|
;Apply the Config XML to the Map
|
|
Protected XML_ID.i
|
|
XML_ID = CreateXML(#PB_Any)
|
|
If XMLStatus(XML_ID) = #PB_XML_Success
|
|
ParseXML(XML_ID, ConfigXML)
|
|
If XMLStatus(XML_ID) = #PB_XML_Success
|
|
ExtractXMLMap(MainXMLNode(XML_ID), @FC\Virtual(UUID), #PB_XML_NoCase)
|
|
FreeXML(XML_ID)
|
|
ProcedureReturn #True
|
|
Else
|
|
FreeXML(XML_ID)
|
|
ProcedureReturn #False
|
|
EndIf
|
|
Else
|
|
If XML_ID <> 0
|
|
FreeXML(XML_ID)
|
|
EndIf
|
|
ProcedureReturn #False
|
|
EndIf
|
|
EndProcedure
|
|
|
|
Procedure.i FC_Create(UUID.s, Type.i = #Type_Main)
|
|
;Create the Server
|
|
EndProcedure
|
|
|
|
Procedure.i FC_Destroy(UUID.s)
|
|
;Kill Thread Cache Handler Thread.
|
|
EndProcedure
|
|
|
|
Procedure.s FC_Status_Get(UUID.s)
|
|
;Status of the Server
|
|
EndEnumeration
|
|
|
|
Procedure.i FC_GetFile(UUID.s, FileName.s, MemoryID.i)
|
|
;Fill the Cached File to Memory and if response > 0 it was succesfull.
|
|
EndEnumeration
|
|
|
|
Procedure.i FC_SetFile(UUID.s, FileName.s, Memory.i, Size.i)
|
|
;Create cached file thread and save it to thread memory.
|
|
EndEnumeration
|
|
|
|
Procedure.i Cache_Cleaner(UUID.s)
|
|
;Cleans out old Files.
|
|
Protected.i Selected
|
|
Protected.i Counter
|
|
Protected.i MaxSize = Information
|
|
Repeat
|
|
Delay(1000) ;Nur Alle Sekunden Prüfen
|
|
Counter + 1
|
|
LockMutex(file_cache_mutex)
|
|
ResetMap(m_file_cache_map())
|
|
;Debug "FileCacheCleaner" + Str(Counter) + " Map Size:" + Str(MapSize(m_file_cache_map()))
|
|
If NextMapElement(m_file_cache_map())
|
|
Repeat
|
|
m_file_cache(MapKey(m_file_cache_map()))\Timer - 1
|
|
If m_file_cache(MapKey(m_file_cache_map()))\Timer <= 0
|
|
If m_file_cache(MapKey(m_file_cache_map()))\Buffer > 0
|
|
configuration\cache\current - m_file_cache(MapKey(m_file_cache_map()))\Size
|
|
FreeMemory(m_file_cache(MapKey(m_file_cache_map()))\Buffer)
|
|
DeleteMapElement(m_file_cache(), MapKey(m_file_cache_map()))
|
|
lhs_log_ext::Out(configuration\log\CachelogUUID, "Info:["+Str(innercount)+"] Killed:["+MapKey(m_file_cache_map())+"]")
|
|
Selected = DeleteMapElement(m_file_cache_map())
|
|
Else
|
|
lhs_log_ext::Out(configuration\log\CachelogUUID, "Info:["+Str(innercount)+"] Could Not kill:["+MapKey(m_file_cache_map())+"]")
|
|
EndIf
|
|
Else
|
|
Selected = NextMapElement(m_file_cache_map())
|
|
EndIf
|
|
Until Selected = 0
|
|
EndIf
|
|
|
|
UnlockMutex(file_cache_mutex)
|
|
|
|
|
|
ForEver
|
|
EndProcedure
|
|
|
|
Procedure.i Cache_Server(UUID.s)
|
|
;http://purearea.net/pb/english/manual/reference/ug_memory.html
|
|
Protected FileCacheCleanerThread.i
|
|
Protected Tempbuffer.i, MaxSize.i, Current.i
|
|
MaxSize = configuration\cache\maxsize * 1024 * 1024
|
|
FileCacheCleanerThread = CreateThread(@FileCacheCleaner(), MaxSize)
|
|
|
|
Repeat
|
|
WaitSemaphore(file_cache_semaphore)
|
|
lhs_log_ext::Out(configuration\log\CachelogUUID, "Adresse:"+m_file_cache()\Buffer)
|
|
If (m_file_cache()\Size + configuration\cache\current) <= MaxSize
|
|
configuration\cache\current + m_file_cache()\Size
|
|
Tempbuffer = AllocateMemory(m_file_cache()\Size)
|
|
CopyMemory(m_file_cache()\Buffer, Tempbuffer, MemorySize(m_file_cache()\Buffer))
|
|
m_file_cache()\Buffer = Tempbuffer
|
|
m_file_cache()\Timer = configuration\cache\time
|
|
m_file_cache()\Is = #True
|
|
m_file_cache_map(MapKey(m_file_cache())) = #True
|
|
SignalSemaphore(file_cache_semaphore_thread)
|
|
Else
|
|
lhs_log_ext::Out(configuration\log\CachelogUUID, "Cache Full")
|
|
SignalSemaphore(file_cache_semaphore_thread)
|
|
EndIf
|
|
|
|
ForEver
|
|
EndProcedure
|
|
|
|
Procedure.i Cache_File(UUID.s)
|
|
;Procedure for File_Cache Thread who handles the Cachefile
|
|
Protected MyMemory.i, My_Host_UUID.s = StringField(UUID, 0, ";"),My_File_UUID.s = StringField(UUID, 1, ";"), WorkElement.i
|
|
Repeat
|
|
WaitSemaphore(Var_FC_Server\Virtual(UUID)\Semaphore)
|
|
LockMutex(Var_FC_Server\Virtual(UUID)\Mutex)
|
|
WorkElement = FirstElement(Var_FC_Server\Virtual(UUID)\Files()
|
|
|
|
UnlockMutex(Var_FC_Server\Virtual(UUID)\Files(UUID)\Mutex)
|
|
|
|
|
|
ForEver
|
|
|
|
|
|
EndProcedure
|
|
|
|
|
|
EndModule
|
|
|
|
|
|
;Buggy FileCache Routines ... Wiso auch immer das noch nicht richtig Funktioniert.
|
|
|
|
Procedure FileCacheCleaner(Information.i)
|
|
|
|
|
|
EndProcedure
|
|
|
|
Procedure FileCache(Information.i)
|
|
|
|
EndProcedure
|
|
|
|
Procedure AddFileToCache(MemoryID.i, FileName.s, Size.i)
|
|
LockMutex(file_cache_mutex)
|
|
lhs_log_ext::Out(configuration\log\CachelogUUID, "Cache MaxSize:"+Str(configuration\cache\maxsize*1024*1024)+" Actual Size:"+Str(configuration\cache\current))
|
|
m_file_cache(FileName)\Buffer = MemoryID
|
|
m_file_cache(FileName)\Size = Size
|
|
SignalSemaphore(file_cache_semaphore)
|
|
WaitSemaphore(file_cache_semaphore_thread)
|
|
If m_file_cache(FileName)\Is
|
|
lhs_log_ext::Out(configuration\log\CachelogUUID, FileName+" Size:"+Str(m_file_cache(FileName)\Size)+" Timer:"+Str(m_file_cache(FileName)\Timer)+ " new Memory ID:"+Str(m_file_cache(FileName)\Buffer))
|
|
Else
|
|
lhs_log_ext::Out(configuration\log\CachelogUUID, "File not in cache was to full:"+FileName+ " MaxSize:"+Str(configuration\cache\maxsize)+" Actual Size:"+Str(configuration\cache\current/1024/1024))
|
|
EndIf
|
|
|
|
UnlockMutex(file_cache_mutex)
|
|
EndProcedure
|
|
|
|
Procedure.s GetFileFromCache(FileName.s, MemoryID.i)
|
|
Protected String.s
|
|
|
|
If TryLockMutex(file_cache_mutex)
|
|
If m_file_cache(FileName)\Is
|
|
MemoryID = ReAllocateMemory(MemoryID, m_file_cache(FileName)\Size)
|
|
CopyMemory(m_file_cache(FileName)\Buffer, MemoryID, m_file_cache(FileName)\Size)
|
|
String.s = Str(m_file_cache(FileName)\Size) + ":" + Str(MemoryID)
|
|
Else
|
|
String.s = #error_string
|
|
EndIf
|
|
UnlockMutex(file_cache_mutex)
|
|
lhs_log_ext::OutL(configuration\log\CachelogUUID,lhs_log_ext::#Info, "File from Cache:"+FileName)
|
|
lhs_log_ext::OutL(configuration\log\CachelogUUID,lhs_log_ext::#Debugging, "Content: --------------------------------------------------------")
|
|
lhs_log_ext::OutL(configuration\log\CachelogUUID,lhs_log_ext::#Debugging, String)
|
|
lhs_log_ext::OutL(configuration\log\CachelogUUID,lhs_log_ext::#Debugging, "End Content. ----------------------------------------------------")
|
|
ProcedureReturn String.s
|
|
Else
|
|
ProcedureReturn #error_string
|
|
EndIf
|
|
|
|
EndProcedure |