-3

At the moment I have a very simple IRC bot written in C. When I run it, it sets up the connection and joins the specified channel. All the connection details are hard-coded, but this is not an issue.

What I want to do is have a basic shell run at the same time, meaning the loop I currently have to receive text from the IRC channel and parse it is running in a thread in the background. From this shell, I want to be able to run commands that may send messages to the IRC channel, or may terminate the program gracefully (meaning both threads will need to be closed). This shell is not meant to be any kind of job control shell.

The primary point of this bot is to monitor the channel for any URLs posted and store them in an SQLite database. People in the IRC channel will then be able to search the database for particular URLs. I also want to be able to perform various database commands from the shell, such as search, but also manual insertions and deletions. I mention this because I assume I will need to ensure all database access is thread-safe. Also, ideally, I'd prefer insertions to database to be prioritised over searching, but this is practically last on my list of requirements.

Finally, there is the chance that some commands received from the IRC channel could be spawned off into their own threads. This could be because they are accessing the database, something which definitely has the potential to be less than instant, but also because they are calling a method to block the thread for a period of time to stagger the transmission of messages to the channel.

This is what I'm asking:

  • How do I go about setting up this sort of program? What sort of structure do I need to run two main threads at once and potentially pass messages between the two?
  • Does access to the socket used for communicating to the IRC channel need to be thread-safe?
  • How should I go about making the program thread-safe? Will mutex suffice?

If it matters, the program will almost certainly be running on an Ubuntu Server distribution.

Matthew G
  • 1,090
  • 2
  • 14
  • 23

1 Answers1

0
  1. Use a queue to pass messages (urls,commands, ect) back and forth between the threads.
  2. Make only one thread access the socket.
  3. If you use a queue then only the queue.

I would suggest first looking up and writing a consumer/producer program to get the hang of that and then incorporate it into your irc bot.

sean
  • 3,955
  • 21
  • 28