//public override T Compute<T>(T val) //{ // return default(T); //} public T[] ArrayCompute <T>(T[] val) { lock (Table) { if (Table[_flag] == null) { Table.Add(_flag, ChangeType(val)); CountTable.Add(_flag, 1); } else { double[] list = (double[])Table[_flag]; CombineSort <double> combine = new CombineSort <double>(); list = combine.GetResult(list, ChangeType(val)); Table[_flag] = list; int count = int.Parse(CountTable[_flag].ToString()); CountTable[_flag] = count + 1; } } if (Waite(_flag)) { return(Array.ConvertAll((double[])Table[_flag], n => (T)Convert.ChangeType(n, typeof(T)))); } throw new TimeoutException(); }
private static byte[] ProcessMergerClient(byte[] bytes) { try { // 1.获取合并包 var client = Serializer.DeserializeFromBytes <MergePacket>(bytes); // 2.处理消息 if (client.Type == MergePacket.MergeType.Sum) { Sum sum = new Sum(client.TimeStamp, client.AppName); double result = sum.Compute(Serializer.DeserializeFromBytes <double>(client.Data)); string flag = "sum_" + client.TimeStamp + "_" + client.AppName; Logger.Info(flag + ",result is " + result); sum.Remove(flag); return(Serializer.SerializeToBytes(result)); } if (client.Type == MergePacket.MergeType.Average) { Average average = new Average(client.TimeStamp, client.AppName); double result = average.Compute(Serializer.DeserializeFromBytes <double>(client.Data)); string flag = "average_" + client.TimeStamp + "_" + client.AppName; Logger.Info(flag + ",result is " + result); average.Remove(flag); return(Serializer.SerializeToBytes(result)); } if (client.Type == MergePacket.MergeType.Distinct) { Distinct distinct = new Distinct(client.TimeStamp, client.AppName); List <object> objects = distinct.Compute(Serializer.DeserializeFromBytes <List <object> >(client.Data)); string flag = "distinct_" + client.TimeStamp + "_" + client.AppName; Logger.Info(flag + ", result count is " + objects.Count); return(Serializer.SerializeToBytes(objects)); } if (client.Type == MergePacket.MergeType.CombineTable) { CombineTable combineTable = new CombineTable(client.TimeStamp, client.AppName); string flag = "combine_table_" + client.TimeStamp + "_" + client.AppName; Logger.Info(flag + ", combine table."); Hashtable objects = combineTable.Compute(Serializer.DeserializeFromBytes <Hashtable>(client.Data)); return(Serializer.SerializeToBytes(objects)); } if (client.Type == MergePacket.MergeType.CombineList) { CombineList combineList = new CombineList(client.TimeStamp, client.AppName); string flag = "combine_list_" + client.TimeStamp + "_" + client.AppName; Logger.Info(flag + ", combine list."); List <object> objects = combineList.Compute(Serializer.DeserializeFromBytes <List <object> >(client.Data)); return(Serializer.SerializeToBytes(objects)); } if (client.Type == MergePacket.MergeType.CombineSort) { try { CombineSort combineSort = new CombineSort(client.TimeStamp, client.AppName); string flag = "combine_sort_" + client.TimeStamp + "_" + client.AppName; Logger.Info(flag + ", combine sort."); object[] objects = combineSort.ArrayCompute(Serializer.DeserializeFromBytes(client.Data)); if (objects == null) { Logger.Warn("Result is null."); } else { Logger.Info("Result count is " + objects.Count()); } return(Serializer.SerializeToBytes(objects)); } catch (Exception exception) { Logger.Error(exception); } object[] errorObjects = { -1 }; return(Serializer.SerializeToBytes(errorObjects)); } } catch (Exception exception) { Logger.Error(exception); } return(Serializer.SerializeToBytes(-1)); }