Saturday, January 8, 2011

Abstract Class Usage for Process to Library Communication

Problem statement:
 
A process is doing lots of different functionalities, and each functionality is a library. So, In general we allways write a interface class where in the both will talk to each other.
But
Let us suppose library want to use some facilities of Main Process . Then
 
1) write an abstract class with the functions which needs to be implemented by main process
2) Main process will inherit this abstract class and main process code will implement these functions.
3) Make sure that this abstract class will have initized function which will be called by Main Process by passing its own this pointer.
So, now this abstract class will always holds the main process class pointer.
4) When ever library wants to avail the main process services, we can get those using the this pointer which it is received. (Any how Main Process class is already inherited and implemented in main process code)
 
//Main Process
Class Main:: public SumServiceCallBackLibrary  //Mainclass is inherited AbstractClassCallBack
{
        public :
                void initializeSubModule1()  {
                        libraryObject.Initialize(this);
                }
               
               
};
 
Main::startShutdown()
{
 
   cout << "Main class received message from library to shut down the system";
};
 
//abstract class which is also interface between library and process
Class SumServiceCallBackLibrary 
{
virtual void  startShutdown() = 0;
};
 
Class LibraryClass
{
  public:
    Initalize(SumServiceCallBackLibrary  * r_cbPtr)  { m_pcSSCBptr= r_cbPtr; }
 
    initiateShutdown() {
        m_pcSSCBptr->startShutDown();  //Here we are calling the implmentation of main process.
    }
               
 
  private:
    SumServiceCallBackLibrary  *m_pcSSCBptr;
}
 
 

When Socket Recv() call got blocked even sockfd is having data

Problem Description:

We have a server where in it is waiting to receive some data so that it can respond with answer. And we have coded such that we are blocked on Select() call,
Once select() gets unblocked we will verity on which FD the data has been triggered using FD_ISSET ,

But the problem is FD_ISSET is returning success mentioning there is data on that socket descriptor, but when we tried to call recv() the data is not getting read so on recv() call the program got blocked.

Don’t know the reason why this happens: (Read like etherenet will do some kind of filling the multiple messages for single eth packet, but not sure whether this is the reason)

But I solved this issue :

By making recv call socket as unblocking
And
Here is how:

(In server code : accept() will return the socket (hSok) then make that socket descriptor as unblocking

int fcntl_flag = 0;

if ((fcntl_flag = fcntl(hSok, F_GETFL, 0)) < 0)
{
printf("\n Error getting sockfd flags F_GETFL %s\n",strerror(errno));

}
if (fcntl(hSok, F_SETFL, fcntl_flag | O_NONBLOCK) < 0)
{
printf("\n Error setting sockfd flags F_SETFL %s\n",strerror(errno));
}