Esempio n. 1
0
        /// <summary>
        /// Receive statistics from server node based on provided host.
        /// </summary>
        /// <param name="host">The host represents the ip address of a server node.</param>
        /// <returns>an object of type <see cref="IndexusStatistic"/></returns>
        internal static IndexusStatistic Statistic(string host)
        {
            #region Access Log
#if TRACE
            {
                Handler.LogHandler.Tracking("Access Method: " + typeof(CacheUtil).FullName + "->" + ((object)MethodBase.GetCurrentMethod()).ToString() + " ;");
            }
#endif
            #endregion Access Log

            Interlocked.Increment(ref counterStatistic);

            var result = new IndexusStatistic();
            var msg    = new IndexusMessage(Handler.Unique.NextUniqueId(), IndexusMessage.ActionValue.Statistic);

            if (!string.IsNullOrEmpty(host))
            {
                msg.Hostname = host;
            }

            if (HandleProtocol(msg, string.Empty))
            {
                var serverNodeStatistic = Formatters.Serialization.BinaryDeSerialize <IndexusStatistic>(msg.Payload);
                result.ServiceTotalSize                 = serverNodeStatistic.ServiceTotalSize;
                result.ServiceAmountOfObjects           = serverNodeStatistic.ServiceAmountOfObjects;
                result.ServiceUsageList                 = serverNodeStatistic.ServiceUsageList;
                result.ApiCounterAdd                    = counterAdd;
                result.ApiCounterGet                    = counterGet;
                result.ApiCounterRemove                 = counterRemove;
                result.ApiCounterStatistic              = counterStatistic;
                result.ApiCounterSuccess                = counterSuccess;
                result.ApiCounterFailed                 = counterFailed;
                result.ApiHitSuccess                    = counterHitSuccess;
                result.ApiHitFailed                     = counterHitFailed;
                result.ApiCounterFailedNodeNotAvailable = counterFailedNodeNotAvailable;
            }
            else
            {
                return(new IndexusStatistic(
                           counterAdd,
                           counterGet,
                           counterRemove,
                           counterStatistic,
                           -1,
                           -1,
                           -1,
                           -1
                           ));
            }

            return(result);
        }
Esempio n. 2
0
 public void BindStats()
 {
     if (this.InvokeRequired)
     {
         UpdateStats inv = new UpdateStats(this.BindStats);
         this.Invoke(inv, new object[] { });
     }
     else
     {
         Cursor.Current = Cursors.WaitCursor;
         COM.IndexusStatistic stat = CACHE.SharedCache.GetStats();
         // Put the words data in a DataTable so that column sorting works.
         DataTable dataTable = new DataTable();
         dataTable.Columns.Add("IP/Name", typeof(string));
         dataTable.Columns.Add("Amount", typeof(long));
         dataTable.Columns.Add("Size KB", typeof(long));
         foreach (COM.ServerStats st in stat.NodeDate)
         {
             dataTable.Rows.Add(new object[] { st.Name, st.AmountOfObjects, st.CacheSize / 1024 });
         }
         this.GvStats.DataSource = dataTable;
         Cursor.Current          = Cursors.Default;
     }
 }
Esempio n. 3
0
		/// <summary>
		/// Receive statistics from server node based on provided host.
		/// </summary>
		/// <param name="host">The host represents the ip address of a server node.</param>
		/// <returns>an object of type <see cref="IndexusStatistic"/></returns>
		internal static IndexusStatistic Statistic(string host)
		{
			#region Access Log
#if TRACE
			
			{
				Handler.LogHandler.Tracking("Access Method: " + typeof(CacheUtil).FullName + "->" + ((object)MethodBase.GetCurrentMethod()).ToString() + " ;");
			}
#endif
			#endregion Access Log

			Interlocked.Increment(ref counterStatistic);

			IndexusStatistic result = new IndexusStatistic();
			IndexusMessage msg = new IndexusMessage(Handler.Unique.NextUniqueId(), IndexusMessage.ActionValue.Statistic);

			if (!string.IsNullOrEmpty(host))
			{
				msg.Hostname = host;
			}

			if (HandleProtocol(msg, string.Empty))
			{
				IndexusStatistic serverNodeStatistic = Formatters.Serialization.BinaryDeSerialize<IndexusStatistic>(msg.Payload);
				result.ServiceTotalSize = serverNodeStatistic.ServiceTotalSize;
				result.ServiceAmountOfObjects = serverNodeStatistic.ServiceAmountOfObjects;
				result.ServiceUsageList = serverNodeStatistic.ServiceUsageList;
				result.apiCounterAdd = counterAdd;
				result.apiCounterGet = counterGet;
				result.apiCounterRemove = counterRemove;
				result.apiCounterStatistic = counterStatistic;
				result.apiCounterSuccess = counterSuccess;
				result.apiCounterFailed = counterFailed;
				result.apiHitSuccess = counterHitSuccess;
				result.apiHitFailed = counterHitFailed;
				result.apiCounterFailedNodeNotAvailable = counterFailedNodeNotAvailable;

			}
			else
			{
				return new IndexusStatistic(
					counterAdd,
					counterGet,
					counterRemove,
					counterStatistic,
					-1,
					-1,
					-1,
					-1
				);
			}

			return result;
		}
Esempio n. 4
0
		/// <summary>
		/// Handles the client message.
		/// </summary>
		/// <param name="state">The state.</param>
		private void HandleClientMessage(COM.Sockets.SharedCacheStateObject state)
		{
			#region Access Log
#if TRACE
			
			{
				COM.Handler.LogHandler.Tracking("Access Method: " + this.GetType().ToString() + "->" + ((object)MethodBase.GetCurrentMethod()).ToString() + " ;");
			}
#endif
			#endregion Access Log

			if (state != null)
			{
				COM.IndexusMessage msg = null;

				try
				{
					System.IO.MemoryStream stream = new System.IO.MemoryStream(state.DataBigBuffer);
					stream.Seek(0, 0);
					msg = new COM.IndexusMessage(stream);
				}
				catch (Exception ex)
				{
					#region Error Handling
					COM.Handler.LogHandler.Fatal(string.Format("[{0}] Could not create IndexusMessage from received data", state.WorkSocket.RemoteEndPoint.ToString()), ex);
					COM.Handler.LogHandler.Error(string.Format("[{0}] Could not create IndexusMessage from received data", state.WorkSocket.RemoteEndPoint.ToString()), ex);
					COM.Handler.LogHandler.Traffic(string.Format("[{0}] Could not create IndexusMessage from received data", state.WorkSocket.RemoteEndPoint.ToString()));
					Console.WriteLine(string.Format("[{0}] Could not create IndexusMessage from received data", state.WorkSocket.RemoteEndPoint.ToString()));

					using (COM.IndexusMessage resultMsg = new COM.IndexusMessage())
					{
						resultMsg.Action = COM.IndexusMessage.ActionValue.Error;
						resultMsg.Status = COM.IndexusMessage.StatusValue.Response;
						resultMsg.Payload = COM.Formatters.Serialization.BinarySerialize(
							new COM.CacheException("Could not convert MemoryStream to indeXusMessage", msg.Action, msg.Status, ex)
						);

						this.SendResponse(state, resultMsg);
					}
					return;
					#endregion Error Handling
				}

				if (msg != null)
				{
					// check status first [Request || Response]
					switch (msg.Status)
					{
						case COM.IndexusMessage.StatusValue.Request:
						case COM.IndexusMessage.StatusValue.ReplicationRequest:
							{
								try
								{
									#region Logging
#if DEBUG
									if (1 == COM.Provider.Server.IndexusServerReplicationCache.ProviderSection.ServerSetting.LoggingEnable)
									{
										// Add request to log
										COM.Handler.LogHandler.Traffic(
											string.Format(COM.Constants.TRAFFICLOG,
												state.WorkSocket.RemoteEndPoint,
												System.Threading.Thread.CurrentThread.ManagedThreadId,
												msg.Id,
												msg.Action.ToString(),
												msg.Status.ToString(),
												state.DataBigBuffer.LongLength
												)
											);
									}
#else
								if (1 == COM.Provider.Server.IndexusServerReplicationCache.ProviderSection.ServerSetting.LoggingEnable)
								{
									// Add request to log
									COM.Handler.LogHandler.Traffic(
										string.Format(COM.Constants.TRAFFICLOG,
											state.WorkSocket.RemoteEndPoint,
											System.Threading.Thread.CurrentThread.ManagedThreadId,
											msg.Id,
											msg.Action.ToString(),
											msg.Status.ToString(),
											state.DataBigBuffer.LongLength
											)
										);
								}
#endif
									#endregion
									#region request case
									COM.IndexusMessage replicationMessage = null;

									// if replication Request arrives as status value a different server node is the holder of this object
									if (ServiceLogic.NetworkDistribution.ReplicationEnabled && msg.Status != COM.IndexusMessage.StatusValue.ReplicationRequest)
									{
										// create a new object of received msg. to broadcast on servers.
										replicationMessage = new SharedCache.WinServiceCommon.IndexusMessage();
										replicationMessage.Action = msg.Action;
										replicationMessage.Status = COM.IndexusMessage.StatusValue.ReplicationRequest;
										replicationMessage.Key = msg.Key;
										replicationMessage.Payload = msg.Payload;
									}

									// switch over cache actions
									switch (msg.Action)
									{
										case SharedCache.WinServiceCommon.IndexusMessage.ActionValue.VersionNumberSharedCache:
											{
												#region VersionNumberSharedCache
												msg.Action = COM.IndexusMessage.ActionValue.Successful;
												msg.Payload = COM.Formatters.Serialization.BinarySerialize(
														new AssemblyInfo().Version
													);
												this.SendResponse(state, msg);
												break;
												#endregion VersionNumberSharedCache
											}
										case COM.IndexusMessage.ActionValue.VersionNumberClr:
											{
												#region VersionNumberClr
												msg.Action = COM.IndexusMessage.ActionValue.Successful;
												msg.Payload = COM.Formatters.Serialization.BinarySerialize(Environment.Version.ToString());
												this.SendResponse(state, msg);
												break;
												#endregion VersionNumberClr
											}
										case COM.IndexusMessage.ActionValue.Ping:
											{
												#region Ping
												msg.Action = COM.IndexusMessage.ActionValue.Successful;
												msg.Payload = null;
												this.SendResponse(state, msg);
												break;
												#endregion Ping
											}
										case COM.IndexusMessage.ActionValue.Add:
											{
												#region Add Case
												try
												{
#if DEBUG
													if (writeStats)
													{
														Console.WriteLine(@"Adding new Item with Key: {0}", msg.Key);
													}
#endif
													LocalCache.Add(msg.Key, msg.Payload);
													// if the given msg expires is not MaxValue
													// it will be listed sub-process which clean
													// up in iterations the cache.
													// QuickFix
													if (msg.Expires != DateTime.MaxValue)
													{
														if (this.expire != null)
														{
															this.expire.Expire.DumpCacheItemAt(msg.Key, msg.Expires);
														}
													}

													// update cleanup list with new object
													CacheCleanup.Update(msg);


													msg.Action = COM.IndexusMessage.ActionValue.Successful;
													msg.Payload = null;
													msg.Status = COM.IndexusMessage.StatusValue.Response;

													// send object back throug the connection
													this.SendResponse(state, msg);

													#region rschuetz: MODIFIED: 21-07-2007: distribute object over wire to other installations
													// Question is if the client needs to wait until this happens, 
													// or should the client first get an answer and just then it 
													// will distribute it.
													if (ServiceLogic.NetworkDistribution.ReplicationEnabled && msg.Status != COM.IndexusMessage.StatusValue.ReplicationRequest)
													{
														ServiceLogic.NetworkDistribution.Replicate(replicationMessage);
													}
													#endregion
												}
												catch (Exception ex)
												{
													using (COM.IndexusMessage resultMsg = new COM.IndexusMessage())
													{
														resultMsg.Action = COM.IndexusMessage.ActionValue.Error;
														resultMsg.Status = COM.IndexusMessage.StatusValue.Response;
														resultMsg.Payload = COM.Formatters.Serialization.BinarySerialize(
															new COM.CacheException(msg.Action.ToString(), msg.Action, msg.Status, ex)
														);

														this.SendResponse(state, resultMsg);
													}
												}
												finally
												{
													// handle max size and purge issues
													if (this.cacheAmountOfObjects != -1 && this.cacheAmountOfObjects <= LocalCache.CalculatedCacheSize && !CacheCleanup.PurgeIsRunning)
													{
#if DEBUG
														if (writeStats)
														{
															Console.WriteLine(@"Current Size of Cache: {0} ; {1} ", LocalCache.CalculatedCacheSize, LocalCache.CalculatedCacheSize <= 0 ? 0 : LocalCache.CalculatedCacheSize / (1024 * 1024));
														}
#endif
														List<string> remove = CacheCleanup.Purge(LocalCache.CalculatedCacheSize);
														if (remove != null)
														{
															lock (bulkObject)
															{
																foreach (string s in remove)
																{
																	LocalCache.Remove(s);
																}
															}
														}
													}
												}
												break;
												#endregion Add Case
											}
										case COM.IndexusMessage.ActionValue.ExtendTtl:
											{
												#region Extend Time To Live
												// workitem: http://www.codeplex.com/SharedCache/WorkItem/View.aspx?WorkItemId=6129
												try
												{
#if DEBUG
													if (writeStats)
													{
														Console.WriteLine("Search for Object with Key: {0}, Current Cache Size: {1}", msg.Key, LocalCache.Amount());
													}
#endif
													CacheCleanup.Update(msg);
													this.expire.Expire.DumpCacheItemAt(msg.Key, msg.Expires);

													msg.Action = COM.IndexusMessage.ActionValue.Successful;
													msg.Status = COM.IndexusMessage.StatusValue.Response;
													msg.Payload = null;
													this.SendResponse(state, msg);
												}
												catch (Exception ex)
												{
													using (COM.IndexusMessage resultMsg = new COM.IndexusMessage())
													{
														resultMsg.Action = COM.IndexusMessage.ActionValue.Error;
														resultMsg.Status = COM.IndexusMessage.StatusValue.Response;
														resultMsg.Payload = COM.Formatters.Serialization.BinarySerialize(
															new COM.CacheException(msg.Action.ToString(), msg.Action, msg.Status, ex)
														);

														this.SendResponse(state, resultMsg);
													}
												}
												break;
												#endregion Extend Time To Live												
											}
										case COM.IndexusMessage.ActionValue.Get:
											{
												#region Receive object from cache
												byte[] objectFromCache = new byte[1] { 0 };
												try
												{
#if DEBUG
													if (writeStats)
													{
														Console.WriteLine("Search for Object with Key: {0}, Current Cache Size: {1}", msg.Key, LocalCache.Amount());
													}
#endif
													objectFromCache = LocalCache.Get(msg.Key);
													// send new object back to client within same socket which contains the cache object.
													msg.Status = COM.IndexusMessage.StatusValue.Response;
													msg.Action = COM.IndexusMessage.ActionValue.Successful;

													msg.Payload = objectFromCache;
													// update cleanup list
													CacheCleanup.Update(msg);

													// bugfix: http://www.codeplex.com/WorkItem/View.aspx?ProjectName=SharedCache&WorkItemId=6167
													// return null if item is already expried
													if (this.expire.Expire.CheckExpired(msg.Key))
													{
														this.expire.Expire.Remove(msg.Key);
														LocalCache.Remove(msg.Key);
														CacheCleanup.Remove(msg.Key);

#if DEBUG
														if (writeStats)
														{
															Console.WriteLine("Cleared Object with Key: {0}, Current Cache Size: {1}", msg.Key, LocalCache.Amount());
														}
#endif
														if (1 == COM.Provider.Server.IndexusServerReplicationCache.ProviderSection.ServerSetting.LoggingEnable)
														{
															COM.Handler.LogHandler.Info(string.Format("Item with Key:'{0}' has been removed and Server node {1} return NULL", msg.Key, Environment.MachineName));
														}

														msg.Payload = null;
													}
													this.SendResponse(state, msg);
												}
												catch (Exception ex)
												{
													using (COM.IndexusMessage resultMsg = new COM.IndexusMessage())
													{
														resultMsg.Action = COM.IndexusMessage.ActionValue.Error;
														resultMsg.Status = COM.IndexusMessage.StatusValue.Response;
														resultMsg.Payload = COM.Formatters.Serialization.BinarySerialize(
															new COM.CacheException(msg.Action.ToString(), msg.Action, msg.Status, ex)
														);

														this.SendResponse(state, resultMsg);
													}
												}
												break;
												#endregion
											}
										case COM.IndexusMessage.ActionValue.Remove:
											{
												#region Remove Case
												try
												{
#if DEBUG
													if (writeStats)
													{
														Console.WriteLine("Remove Object with Key: {0}, current Cache amount: {1}", msg.Key, LocalCache.Amount());
													}
#endif
													// remove it from the cache.
													LocalCache.Remove(msg.Key);
													if (this.expire != null)
													{
														// remove it from the cache expiry job.
														this.expire.Expire.Remove(msg.Key);
													}
													// update cleanup list
													CacheCleanup.Remove(msg.Key);

													#region rschuetz: MODIFIED: 21-07-2007: distribute object over wire to other installations
													// Question is if the client needs to wait until this happens, 
													// or should the client first get an answer and just then it 
													// will distribute it.
													if (ServiceLogic.NetworkDistribution.ReplicationEnabled && msg.Status != COM.IndexusMessage.StatusValue.ReplicationRequest)
													{
														ServiceLogic.NetworkDistribution.Replicate(replicationMessage);
													}
													#endregion

													msg.Action = COM.IndexusMessage.ActionValue.Successful;
													msg.Status = COM.IndexusMessage.StatusValue.Response;
													msg.Payload = null;
													this.SendResponse(state, msg);
												}
												catch (Exception ex)
												{
													using (COM.IndexusMessage resultMsg = new COM.IndexusMessage())
													{
														resultMsg.Action = COM.IndexusMessage.ActionValue.Error;
														resultMsg.Status = COM.IndexusMessage.StatusValue.Response;
														resultMsg.Payload = COM.Formatters.Serialization.BinarySerialize(
															new COM.CacheException(msg.Action.ToString(), msg.Action, msg.Status, ex)
														);

														this.SendResponse(state, resultMsg);
													}
												}
												break;
												#endregion Remove Case
											}
										case COM.IndexusMessage.ActionValue.RegexGet:
											{
												#region Receiving data based on regular expression pattern
												try
												{
													#region
#if DEBUG
													if (writeStats)
													{
														Console.WriteLine(@"RegexGet Operation");
													}
#endif
													#endregion
													IDictionary<string, byte[]> result = new Dictionary<string, byte[]>();

													string regularExpressionPattern = Encoding.UTF8.GetString(msg.Payload);
													Regex regex = new Regex(regularExpressionPattern, RegexOptions.CultureInvariant);

													List<string> actualData = LocalCache.GetAllKeys();

													foreach (string n in actualData)
													{
														if (regex.IsMatch(n))
														{
															result.Add(n, LocalCache.Get(n));
														}
													}

													msg.Action = COM.IndexusMessage.ActionValue.Successful;
													msg.Status = COM.IndexusMessage.StatusValue.Response;
													msg.Payload = COM.Formatters.Serialization.BinarySerialize(result);

													this.SendResponse(state, msg);
												}
												catch (Exception ex)
												{
													#region Exception Handling
													using (COM.IndexusMessage resultMsg = new COM.IndexusMessage())
													{
														resultMsg.Action = COM.IndexusMessage.ActionValue.Error;
														resultMsg.Status = COM.IndexusMessage.StatusValue.Response;
														resultMsg.Payload = COM.Formatters.Serialization.BinarySerialize(
															new COM.CacheException(msg.Action.ToString(), msg.Action, msg.Status, ex)
														);
														this.SendResponse(state, resultMsg);
													}
													#endregion Exception Handling
												}
												break;
												#endregion Receiving data based on regular expression pattern
											}
										case COM.IndexusMessage.ActionValue.RegexRemove:
											{
												#region Regex Remove
												try
												{
													Regex regex = new Regex(Encoding.UTF8.GetString(msg.Payload), RegexOptions.CultureInvariant);

													#region local logging
#if DEBUG
													if (writeStats)
													{
														Console.WriteLine("Regex Remove Keys which match pattern: {0}", regex.ToString());
													}
#endif
													#endregion local logging
													List<string> keysToRemove = new List<string>();
													List<string> actualData = LocalCache.GetAllKeys();
													foreach (string n in actualData)
													{
														if (regex.IsMatch(n))
															keysToRemove.Add(n);
													}

													foreach (string key in keysToRemove)
													{
														// remove it from the cache.
														LocalCache.Remove(key);
														if (this.expire != null)
														{
															// remove it from the cache expiry job.
															this.expire.Expire.Remove(key);
														}
														// update cleanup list
														CacheCleanup.Remove(key);
													}


													#region rschuetz: MODIFIED: 21-07-2007: distribute object over wire to other installations
													// Question is if the client needs to wait until this happens, 
													// or should the client first get an answer and just then it 
													// will distribute it.
													if (ServiceLogic.NetworkDistribution.ReplicationEnabled && msg.Status != COM.IndexusMessage.StatusValue.ReplicationRequest)
													{
														ServiceLogic.NetworkDistribution.Replicate(replicationMessage);
													}
													#endregion

													msg.Action = COM.IndexusMessage.ActionValue.Successful;
													msg.Status = COM.IndexusMessage.StatusValue.Response;
													msg.Payload = null;
													this.SendResponse(state, msg);
												}
												catch (Exception ex)
												{
													using (COM.IndexusMessage resultMsg = new COM.IndexusMessage())
													{
														resultMsg.Action = COM.IndexusMessage.ActionValue.Error;
														resultMsg.Status = COM.IndexusMessage.StatusValue.Response;
														resultMsg.Payload = COM.Formatters.Serialization.BinarySerialize(
															new COM.CacheException(msg.Action.ToString(), msg.Action, msg.Status, ex)
														);

														this.SendResponse(state, resultMsg);
													}
												}
												break;
												#endregion Regex Remove
											}
										case COM.IndexusMessage.ActionValue.RemoveAll:
											{
												#region Clear all cache items!
												try
												{
#if DEBUG
													if (writeStats)
													{
														Console.WriteLine("Clear all cache items!");
													}
#endif
													lock (bulkObject)
													{
														// remove all items from cache
														LocalCache.Clear();

														if (this.expire != null)
														{
															// remove all items from the cache expiry job.
															this.expire.Expire.Clear();
														}

														// remove all items from cleanup
														CacheCleanup.Clear();
													}

													msg.Action = COM.IndexusMessage.ActionValue.Successful;
													msg.Status = COM.IndexusMessage.StatusValue.Response;
													msg.Payload = null;
													this.SendResponse(state, msg);
												}
												catch (Exception ex)
												{
													using (COM.IndexusMessage resultMsg = new COM.IndexusMessage())
													{
														resultMsg.Action = COM.IndexusMessage.ActionValue.Error;
														resultMsg.Status = COM.IndexusMessage.StatusValue.Response;
														resultMsg.Payload = COM.Formatters.Serialization.BinarySerialize(
															new COM.CacheException(msg.Action.ToString(), msg.Action, msg.Status, ex)
														);

														this.SendResponse(state, resultMsg);
													}


												}
												break;
												#endregion
											}
										case COM.IndexusMessage.ActionValue.GetAllKeys:
											{
												#region Get All Keys
												try
												{
#if DEBUG
													if (writeStats)
													{
														Console.WriteLine("Get All available Key's!");
													}
#endif
													List<string> keys = LocalCache.GetAllKeys();

													msg.Action = COM.IndexusMessage.ActionValue.Successful;
													msg.Status = COM.IndexusMessage.StatusValue.Response;
													msg.Payload = COM.Formatters.Serialization.BinarySerialize(keys);

													this.SendResponse(state, msg);
												}
												catch (Exception ex)
												{
													using (COM.IndexusMessage resultMsg = new COM.IndexusMessage())
													{
														resultMsg.Action = COM.IndexusMessage.ActionValue.Error;
														resultMsg.Status = COM.IndexusMessage.StatusValue.Response;
														resultMsg.Payload = COM.Formatters.Serialization.BinarySerialize(
															new COM.CacheException(msg.Action.ToString(), msg.Action, msg.Status, ex)
														);
														this.SendResponse(state, resultMsg);
													}
												}
												break;
												#endregion
											}
										case COM.IndexusMessage.ActionValue.Statistic:
											{
												#region Statistic

												try
												{
#if DEBUG
													if (writeStats)
													{
														Console.WriteLine("Message Action: {0}", msg.Action.ToString());
													}
#endif
													COM.IndexusStatistic stats;
													// previous clients are sending a stats object, newer clients are 
													// sending nothing to reduce interaction on the wire.
													if (msg.Payload != null)
														stats = COM.Formatters.Serialization.BinaryDeSerialize<COM.IndexusStatistic>(msg.Payload);
													else
														stats = new COM.IndexusStatistic();

													// receive cache attributes and add them to cache object
													stats.ServiceAmountOfObjects = LocalCache.Amount();
													stats.ServiceTotalSize = LocalCache.Size();
													stats.ServiceUsageList = CacheCleanup.GetAccessStatList();

													// adding results to return message and set all needed attributes.
													msg.Payload = COM.Formatters.Serialization.BinarySerialize(stats);
													msg.Action = COM.IndexusMessage.ActionValue.Successful;
													msg.Status = COM.IndexusMessage.StatusValue.Response;

													this.SendResponse(state, msg);
												}
												catch (Exception ex)
												{
													using (COM.IndexusMessage resultMsg = new COM.IndexusMessage())
													{
														resultMsg.Action = COM.IndexusMessage.ActionValue.Error;
														resultMsg.Status = COM.IndexusMessage.StatusValue.Response;
														resultMsg.Payload = COM.Formatters.Serialization.BinarySerialize(
															new COM.CacheException(msg.Action.ToString(), msg.Action, msg.Status, ex)
														);
														this.SendResponse(state, resultMsg);
													}
												}
												break;
												#endregion
											}
										case COM.IndexusMessage.ActionValue.GetAbsoluteTimeExpiration:
											{
												// As requested: http://www.codeplex.com/SharedCache/WorkItem/View.aspx?WorkItemId=6170
												#region Collecting absolut expiration DateTime for provided keys
												try
												{
#if DEBUG
													if (writeStats)
													{
														Console.WriteLine(@"GetAbsoluteTimeExpiration Operation: {0}", msg.Key);
													}
#endif
													IDictionary<string, DateTime> result = new Dictionary<string, DateTime>();
													List<string> requstedData = COM.Formatters.Serialization.BinaryDeSerialize<List<string>>(msg.Payload);
													if (requstedData != null)
													{
														foreach (string key in requstedData)
														{
															bool addItem = true;

															if (this.expire.Expire.CheckExpired(key))
															{
																result.Add(key, DateTime.MinValue);
															}
															else
															{
																result.Add(key, this.expire.Expire.GetExpireDateTime(key));
															}
														}

														msg.Action = COM.IndexusMessage.ActionValue.Successful;
														msg.Status = COM.IndexusMessage.StatusValue.Response;
														msg.Payload = COM.Formatters.Serialization.BinarySerialize(result);

														this.SendResponse(state, msg);
													}
													else
													{
														throw new Exception("Payload could not be deserialized into a list of strings!");
													}
												}
												catch (Exception ex)
												{
													#region Exception Handling
													using (COM.IndexusMessage resultMsg = new COM.IndexusMessage())
													{
														resultMsg.Action = COM.IndexusMessage.ActionValue.Error;
														resultMsg.Status = COM.IndexusMessage.StatusValue.Response;
														resultMsg.Payload = COM.Formatters.Serialization.BinarySerialize(
															new COM.CacheException(msg.Action.ToString(), msg.Action, msg.Status, ex)
														);
														this.SendResponse(state, resultMsg);
													}
													#endregion Exception Handling
												}
												break;
												#endregion Collecting absolut expiration DateTime for provided keys
											}
										case COM.IndexusMessage.ActionValue.MultiGet:
											{
												#region Receiving a list of single items from the cache
												try
												{
#if DEBUG
													if (writeStats)
													{
														Console.WriteLine(@"MultiGet Operation: {0}", msg.Key);
													}
#endif
													IDictionary<string, byte[]> result = new Dictionary<string, byte[]>();
													List<string> requstedData = COM.Formatters.Serialization.BinaryDeSerialize<List<string>>(msg.Payload);
													if (requstedData != null)
													{
														foreach (string key in requstedData)
														{
															bool addItem = true;
															// bugfix: http://www.codeplex.com/WorkItem/View.aspx?ProjectName=SharedCache&WorkItemId=6167
															// return null if item is already expried
															if (this.expire.Expire.CheckExpired(key))
															{
																this.expire.Expire.Remove(key);
																LocalCache.Remove(key);
																CacheCleanup.Remove(key);
#if DEBUG
																if (writeStats)
																{
																	Console.WriteLine("Cleared Object with Key: {0}, Current Cache Size: {1}", msg.Key, LocalCache.Amount());
																}
#endif
																if (1 == COM.Provider.Server.IndexusServerReplicationCache.ProviderSection.ServerSetting.LoggingEnable)
																{
																	COM.Handler.LogHandler.Info(string.Format("Item with Key:'{0}' has been removed and Server node {1} return NULL", msg.Key, Environment.MachineName));
																}

																addItem = false;
															}

															if (addItem)
															{
																result.Add(key, LocalCache.Get(key));
															}
														}

														msg.Action = COM.IndexusMessage.ActionValue.Successful;
														msg.Status = COM.IndexusMessage.StatusValue.Response;
														msg.Payload = COM.Formatters.Serialization.BinarySerialize(result);

														this.SendResponse(state, msg);
													}
													else
													{
														throw new Exception("Payload could not be deserialized into a list of strings!");
													}
												}
												catch (Exception ex)
												{
													#region Exception Handling
													using (COM.IndexusMessage resultMsg = new COM.IndexusMessage())
													{
														resultMsg.Action = COM.IndexusMessage.ActionValue.Error;
														resultMsg.Status = COM.IndexusMessage.StatusValue.Response;
														resultMsg.Payload = COM.Formatters.Serialization.BinarySerialize(
															new COM.CacheException(msg.Action.ToString(), msg.Action, msg.Status, ex)
														);
														this.SendResponse(state, resultMsg);
													}
													#endregion Exception Handling
												}
												break;
												#endregion Receiving a list of single items from the cache
											}
										case COM.IndexusMessage.ActionValue.MultiAdd:
											{
												#region Adding a bunch of data
												try
												{
													#region Stats
#if DEBUG
													if (writeStats)
													{
														Console.WriteLine(@"Adding a bunch of data - MultiAdd");
													}
#endif
													#endregion Stats

													IDictionary<string, byte[]> data =
														COM.Formatters.Serialization.BinaryDeSerialize<IDictionary<string, byte[]>>(msg.Payload);

													foreach (KeyValuePair<string, byte[]> item in data)
													{
														LocalCache.Add(item.Key, item.Value);
														// if the given msg expires is not MaxValue
														// it will be listed sub-process which clean
														// up in iterations the cache.
														// QuickFix
														if (msg.Expires != DateTime.MaxValue)
														{
															if (this.expire != null)
															{
																this.expire.Expire.DumpCacheItemAt(item.Key, msg.Expires);
															}
														}

														using (COM.IndexusMessage multiMsg = new COM.IndexusMessage(
																			COM.Handler.Unique.NextUniqueId(), msg.Status, msg.Action, msg.ItemPriority,
																			msg.Hostname, msg.Expires, item.Key, item.Value
																		)
																	)
														{
															// update cleanup list with new object
															CacheCleanup.Update(multiMsg);
														}
													}

													msg.Action = COM.IndexusMessage.ActionValue.Successful;
													msg.Payload = null;
													msg.Status = COM.IndexusMessage.StatusValue.Response;

													// send object back throug the connection
													this.SendResponse(state, msg);

													#region rschuetz: MODIFIED: 21-07-2007: distribute object over wire to other installations
													// Question is if the client needs to wait until this happens, 
													// or should the client first get an answer and just then it 
													// will distribute it.
													if (ServiceLogic.NetworkDistribution.ReplicationEnabled && msg.Status != COM.IndexusMessage.StatusValue.ReplicationRequest)
													{
														ServiceLogic.NetworkDistribution.Replicate(replicationMessage);
													}
													#endregion
												}
												catch (Exception ex)
												{
													using (COM.IndexusMessage resultMsg = new COM.IndexusMessage())
													{
														resultMsg.Action = COM.IndexusMessage.ActionValue.Error;
														resultMsg.Status = COM.IndexusMessage.StatusValue.Response;
														resultMsg.Payload = COM.Formatters.Serialization.BinarySerialize(
															new COM.CacheException(msg.Action.ToString(), msg.Action, msg.Status, ex)
														);

														this.SendResponse(state, resultMsg);
													}
												}
												finally
												{
													// handle max size and purge issues
													if (this.cacheAmountOfObjects != -1 && this.cacheAmountOfObjects <= LocalCache.CalculatedCacheSize && !CacheCleanup.PurgeIsRunning)
													{
#if DEBUG
														if (writeStats)
														{
															Console.WriteLine(@"Current Size of Cache: {0} ; {1} ", LocalCache.CalculatedCacheSize, LocalCache.CalculatedCacheSize <= 0 ? 0 : LocalCache.CalculatedCacheSize / (1024 * 1024));
														}
#endif
														List<string> remove = CacheCleanup.Purge(LocalCache.CalculatedCacheSize);
														if (remove != null)
														{
															lock (bulkObject)
															{
																foreach (string s in remove)
																{
																	LocalCache.Remove(s);
																}
															}
														}
													}
												}
												break;
												#endregion Adding a bunch of data
											}
										case COM.IndexusMessage.ActionValue.MultiDelete:
											{
												#region Remove a bunch of data
												try
												{
													#region Stats
#if DEBUG
													if (writeStats)
													{
														Console.WriteLine("MultiDelete Object with Key: {0}, current Cache amount: {1}", msg.Key, LocalCache.Amount());
													}
#endif
													#endregion Stats

													List<string> requstedDataToDelete = COM.Formatters.Serialization.BinaryDeSerialize<List<string>>(msg.Payload);

													foreach (string key in requstedDataToDelete)
													{
														// remove it from the cache.
														LocalCache.Remove(key);
														if (this.expire != null)
														{
															// remove it from the cache expiry job.
															this.expire.Expire.Remove(key);
														}
														// update cleanup list
														CacheCleanup.Remove(key);
													}

													msg.Action = COM.IndexusMessage.ActionValue.Successful;
													msg.Status = COM.IndexusMessage.StatusValue.Response;
													msg.Payload = null;
													this.SendResponse(state, msg);
												}
												catch (Exception ex)
												{
													using (COM.IndexusMessage resultMsg = new COM.IndexusMessage())
													{
														resultMsg.Action = COM.IndexusMessage.ActionValue.Error;
														resultMsg.Status = COM.IndexusMessage.StatusValue.Response;
														resultMsg.Payload = COM.Formatters.Serialization.BinarySerialize(
															new COM.CacheException(msg.Action.ToString(), msg.Action, msg.Status, ex)
														);

														this.SendResponse(state, resultMsg);
													}
												}
												break;

												#endregion Remove a bunch of data
											}

									}
									break;
									#endregion request case
								}
								finally
								{
									// msg is not needed anymore and since its implmente IDisposable it can be disposed at this place
									if (msg != null)
									{
										msg.Dispose();
									}
								}


							}
						case COM.IndexusMessage.StatusValue.Response:
							{
								#region response case
#if DEBUG
								if (writeStats)
								{
									Console.WriteLine("Received Action: {0}", msg.Status.ToString());
								}
#endif
								// Server never should receive a Response from the client!
								msg.Action = COM.IndexusMessage.ActionValue.Error;
								string info = string.Format("[{0}]Error, the message should have the Status Request!", state.WorkSocket.RemoteEndPoint);
								Console.WriteLine(info);
								COM.Handler.LogHandler.Traffic(info);
								COM.Handler.LogHandler.Error(info);
								break;
								#endregion response case
							}
					}
				}
			}
			else
			{
				COM.Handler.LogHandler.Error("Cannot handle state because state is NULL");
				Console.WriteLine("Cannot handle state because state is NULL");
			}
		}