简体   繁体   English

如何将值的集合分配到动态列表?

[英]How to distribute collection of values to dynamic list?

I am developing TCP client-server console application. 我正在开发TCP客户端-服务器控制台应用程序。 I have N number of clients connected to server. 我有N个客户端连接到服务器。 On a server side I need to share M records and each record should be sent only once to client. 在服务器端,我需要共享M条记录,并且每条记录仅应发送给客户端一次。 Each record should be received only by 1 client. 每条记录只能由1位客户接收。 For example number of records N=4 and number of clients M=10 and result should be: 例如,记录数N = 4,客户数M = 10,结果应为:

record1 – to client1
record2 – to client2
record3 – to client3
record4 – to client4
record5 – to client1
record6 – to client2
record7 – to client3
record9 – to client4 
record10 – to client5

The problem is that the number of records M is fixed, but the number clients N is not fixed (sometimes N=3, sometimes N=5, etc) 问题是记录的数量M是固定的,但客户端的数量N是固定的(有时N = 3,有时N = 5,等等)

Could you please suggest me a solution to organize this type of flow control? 您能否建议我一个组织此类流量控制的解决方案?

I'd try to implement something like this: 我会尝试实现这样的事情:

  • You'll need one list or collecting holding all clients being connected. 您将需要一个列表或收集所有连接的客户端。 You'll need random access, ie you'll have to add clients to the front or back and you'll have to be able to remove any element at any time. 您将需要随机访问,即必须将客户端添加到前端或后端,并且必须能够随时删除任何元素。 Bonus points if the list is sorted by number of assigned records (ascending). 如果列表按分配的记录数排序(升序),则奖励积分。

  • When there's a new record to process (or while you iterate over a list with them): 当有新记录要处理时(或在您使用它们遍历列表时):

    • Send the record to the client being in front of your list. 将记录发送到列表前面的客户。
    • Remove the client from the front of the list and add it to the back (unless the list is sorted anyway). 从列表的开头删除客户端,然后将其添加到列表的后面(除非对列表进行了排序)。
  • When a new client connects, it's added to the front of your list. 当新客户端连接时,它将被添加到列表的前面。

  • If a client disconnects, it's removed from the list and the records are readded for redistribution. 如果客户端断开连接,则会将其从列表中删除,并读取记录以进行重新分配。

If I didn't make any mistake while thinking about this, this should ensure that always one of the clients with the least number of tasks will get a new record unless many clients drop mid-processing. 如果我在考虑这一点时没有犯任何错误,那么这应该确保总是有最少任务数的客户端之一获得新记录,除非许多客户端放弃中间处理。 In that case a sorted list would be an advantage. 在这种情况下,排序列表将是一个优势。

Of course, there's room for optimization and it's not the best strategy if your number of clients doesn't change while processing (and only before you start doing so). 当然,还有优化的余地,如果您的客户数量在处理过程中(并且只有在您开始进行更改之前)没有发生变化,这也不是最佳策略。 In this case you could simply determine the client that's supposed to process record n using i = n % number_of_clients . 在这种情况下,您可以使用i = n % number_of_clients来简单地确定应该处理记录n的客户端。

You want to loop through all records, and simultaneously loop through clients, but use modulus to ensure you loop back through clients if m > n , like this: 您想遍历所有记录,并同时遍历客户端,但是如果m > n ,则可以使用模数来确保遍历客户端,如下所示:

var records = GetRecords(); // size m
var clients = GetClients(); // size n

if (clients.Length == 0)
{
    // return or throw here, depending on your application
}

for (var i = 0; i < records.Length; i++)
{
    var record = records[i];
    var client = clients[i%clients.Length];

    SendRecord(record, client);
}

With this solution, m and n could both be dynamic. 使用此解决方案, mn都可以是动态的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM