0

I have a server client app written in C++ on Linux. When a client connects to my server, the server spawns a thread that waits for the client to send the server commands to execute. the commands are OS dependent. the thread that the client is talking to the server on, calls global functions that perform the required command that the client wants. So I have to have two functions for every OS depended command the client sends to the server to be executed. All of these global functions are defined in the same header that the main thread function is. It's getting a bit messy with all of these functions for different OS's. My idea is to write two classes that are called WindowsFuncs and LinuxFuncs that have static member functions that perform the required command for that OS the class was designed for. What are some of stackoverflows ideas on how to clean my logic?

TheFuzz
  • 2,487
  • 5
  • 27
  • 46

1 Answers1

0

This doesn't sound like a threading problem. Sounds like you can use simple inheritance.

Using something like

abstract class OSMethods {
  void listDir();
}

class OSMethodsLinux : OSMethods {
  void listDir() { system.exec("ls"); }
} 
class OSMethodsWin : OSMethods {
  void listDir() { system.exec("dir"); }
}

Then server client processing code has method like

  void accept(Socket s, OSMethods m) {
     s.readCommand();
     m.listDir();  // or whatever
  }

Make sure you pass correct instance to accept of either Linux or Win class. So no static methods.

Generally I've found that you will need no static methods in your programs (except main) unless you are doing clever stuff, most things just don't need them and they lead to less flexible design.

DaveC
  • 1,990
  • 14
  • 13
  • Random question: I want the thread to safely call all of these functions so where would I place the mutex variable for these class methods? – TheFuzz Dec 11 '10 at 03:18
  • Put a single mutex inside the single instance of OSMethodsXXX which every method in OSMethodsXXX obtains at start of method and releases when done. Make sure you only make a single instance of the OSMethodsXXX. – DaveC Dec 11 '10 at 03:28
  • since I'm building on a linux box, how can I leave windows code in there so that I don't have to keep commenting it out, before i build? just ifdefs? – TheFuzz Dec 11 '10 at 04:07