Arry Posted January 28, 2008 Report Posted January 28, 2008 (edited) Due to the amount of multiline output my new bot is expected to generate, I want to throttle it's output, but I also want to build a responsible queuing system that allows those who have cause the least spam in the past x seconds priority in the queue over the people who have caused the bot to spam a lot. For example, let's say there's an item listing command that fits on 5 lines. If someone is checking their items multiple times in succession, they'll quickly be the cause of a backlog of messages. Instead of cutting that person off, I just want to give any new messages they generate a low priority so that they aren't slowing the bot down for others. I'm thinking that I'm going to need a collection to hold the queue of messages and another collection to hold the current priority order of players. When an output message is generated, responsibility is !@#$%^&*igned (most likely to the same person who's it's sending the private message to). It then checks to see if that player is !@#$%^&*igned a lower than normal priority and then loads the message into the queue accordingly. Does this sound like the best way to go about doing it or would their be a simpler approach that would achieve the same goal? And what types of java collections would be the best fit here? (there's so many to choose from) Edited January 28, 2008 by Arry Quote
»doc flabby Posted January 28, 2008 Report Posted January 28, 2008 One idea would be to multi thread, write text to clients in a different thread to the one that processes the client requests. Quote
Arry Posted January 28, 2008 Author Report Posted January 28, 2008 Well, I'm not worried about slowing down the bot as much as I'm worried about fitting in under the server's spam limitations (before getting silenced). The logjam is definitely going to be how much the server will let me send, not how fast my bot can process incoming requests Quote
»Ceiu Posted January 28, 2008 Report Posted January 28, 2008 What you could do is use a PriorityQueue object with a custom Comparator (or whatever it's called). Basically, the queue will sort items based on where the Comparator says they should go. Then, just make the Comparator keep track of how many messages the users are receiving. As a user receives more messages, their priority goes down. From there, you'd make an object that implements the MessageHandler interface and insert it to the outbound chain of the CNConnection you want to throttle. What it would have to do is keep track of how many outbound messages have gone out, then queue messages as it reaches it's limit. Read the details on the MessageHandler interface and the postMessage and sendMessage functions in CNConnection for details on how to piece it all together. Quote
Arry Posted January 29, 2008 Author Report Posted January 29, 2008 (edited) Wouldn't I have to have a separate thread that would continually check the queue to see if it had messages and if it was safe to send then send any messages that are waiting? Edited January 29, 2008 by Arry Quote
»Ceiu Posted January 29, 2008 Report Posted January 29, 2008 Using another thread would allow you to guarantee that the messages will go out eventually. If you don't use another thread, you run the risk of leaving messages in the queue until the send method is called again. Quote
Arry Posted January 31, 2008 Author Report Posted January 31, 2008 (edited) I'm making progress on this, but I think I'm going to run into a problem using PriorityQueue.... when a player has multiple messages in the queue, those messages need to be maintained in the order they were originally trying to be sent. I'm thinking that PriorityQueue will not garuntee order among equal items (in this case that's messages queued to the same player), and there's nothing internal to the message string that tells me the order it arrived. I'm going to have to verify this, but I think I'm going to need to compensate for this somehow. Edited January 31, 2008 by Arry Quote
JoWie Posted January 31, 2008 Report Posted January 31, 2008 You could try using a stack http://java.sun.com/j2se/1.4.2/docs/api/java/util/Stack.html (Last in is first out) Quote
Arry Posted January 31, 2008 Author Report Posted January 31, 2008 Yeah, if I was just trying to throttle I could use something like that. But I'm also trying to prioritize the Queued messages (spammers last). I'm going to make one that just throttles too, but I think it's unfair that one person could use several multiline output messages and everyone else has to wait till the Queue is cleared. Rather, I want people who haven't recieved many private messages (IE the ones who are sending commands with multiline responses) to be the able to skip ahead of people who are bogging up the queue with their many messages. Quote
»doc flabby Posted February 1, 2008 Report Posted February 1, 2008 (edited) I'm pretty sure the java library has a priority queue class, this is what you would need. http://java.sun.com/j2se/1.5.0/docs/api/ja...orityQueue.html using the class on the other hand looks far from simple. this article may help http://www.oopweb.com/Java/Do!@#$...lume/chap16.htm Edited February 1, 2008 by doc flabby Quote
»Ceiu Posted February 1, 2008 Report Posted February 1, 2008 That's exactly what class we were referring to. Quote
»doc flabby Posted February 1, 2008 Report Posted February 1, 2008 That's exactly what class we were referring to.-_- Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.