如何将值集合分发到动态列表? [英] How to distribute collection of values to dynamic list?

查看:82
本文介绍了如何将值集合分发到动态列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发TCP客户端 - 服务器控制台应用程序。我有N个客户端连接到服务器。在服务器端,我需要共享M条记录,每条记录只应发送一次给客户端。每个记录只能由1个客户接收。

例如记录数N = 4,客户数M = 10,结果应为:

record1 - client1

record2 - 到client2

record3 - 到client3

record4 - 到client4

record5 - 到client1

record6 - 到client2

record7 - 到client3

record9 - 到client4

record10 - 到client5





问题是记录数M是固定的,但客户N的数量不固定(有时N = 3,有时N = 5 ,等等)



你能否告诉我一个组织这种流量控制的解决方案?

I am developing TCP client-server console application. I have N number of clients connected to server. On a server side I need to share M records and each record should be sent only once to client. Each record should be received only by 1 client.
For example number of records N=4 and number of clients M=10 and result should be:
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)

Could you please suggest me a solution to organize this type of flow control?

推荐答案

我的算法将为客户端使用一个队列,两个不同的线程,一个用于分发记录,一个用于收集结果。





分配:



while(记录存在){

client = queue.dequeue();

如果(client == null)继续;



跟踪器。添加(记录,客户端);

client.send(记录);

}



收集结果:



while(listen){

client = tracker.remove(record);

queue.enqueue(client);

}



- 客户端可以注册/注销自己。

- 您还应该设置一个超时值进行处理。当超时时,您假设客户端已经消失,您应该重新处理记录。在这种情况下,再次使用队列来处理记录。



基本上,这将是我的解决方案。
my algorithm would be using a queue for clients, and two different threads, one for distributing records and one for collecting results.


distribution:

while(a record exists) {
client = queue.dequeue();
if(client==null) continue;

tracker.add(record, client);
client.send(record);
}

collecting result:

while(listen) {
client=tracker.remove(record);
queue.enqueue(client);
}

- client can register/unregister itself.
- you should also set a timeout value for processing. when timeout, you assume the client has gone, and you should reprocess the record. in that case, again use a queue for records to be processed.

basically, that would be my solution.


I尝试实现这样的事情:



您需要一个列表或收集所有连接的客户端。您需要随机访问,即您必须在前面或后面添加客户端,您必须能够随时删除任何元素。如果列表按指定的记录数量(升序)排序,则奖励积分。



当有新的记录要处理时(或者当你用它们迭代列表时) :



- 将记录发送到列表前面的客户端。

- 从列表前面删除客户端将它添加到后面(除非列表无论如何都要排序)。

- 当新客户端连接时,它会被添加到列表的前面。



如果客户端断开连接,它将从列表中删除,并且记录会被重新分配。

如果我在考虑这个问题时没有犯任何错误,这应该确保总是其中一个具有最少任务数的客户端将获得新记录,除非许多客户端丢弃中间处理。在这种情况下,排序列表将是一个优势。



当然,还有优化空间,如果您的客户数量没有变化,这不是最佳策略处理(并且只在你开始这样做之前)。在这种情况下,您可以使用i = n%number_of_clients简单地确定应该处理记录n的客户端。



http://stackoverflow.com/questions/21829187/how-to-distribute-collection-of-values-to-动态列表 [ ^ ]
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, i.e. 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.

http://stackoverflow.com/questions/21829187/how-to-distribute-collection-of-values-to-dynamic-list[^]


这篇关于如何将值集合分发到动态列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆