having trouble reading and writing from and to a fifo seamlessly

Im aware that the fifo is being read and so cant be written to however I cant work out where or how, I get a bad file descriptor error, in the case of this code it means that nothing is written to the fifo

this is the main method, I have taken out the music functionality and other bits that I dont think have any relevance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
 //this block gets the directory the user is using for the program
  if ((cwd = getenv("HOME")) == NULL)  {

        ~~~~~
  }


  //create and initialise IPC object
  IPC ipc( PATH + "/tmp/fifo");

  //checks if args have been put in correctly and this instance is the music
  //program or just passing the music a message
  if( argc == 2 && ipc.IPCOriginal()){

       ~~~some music logic~~~

  //while music is not over, might want to slow this down later too
  while( song.isPlaying() && running){

    //some music logic
  }

        //if this thread is to simply sit and spin then it had better not do too
        //mutch too often, this function sleeps for 1 second
        sleep( 1);
    }

    std::cout<< track + " stopped, closing." <<std::endl;
    ipc.IPCClose();
    return 0;

  //in this case the instance of  the program is just going to send a message
  //and then exit
  }else if( argc == 2 && !ipc.IPCOriginal()){

    //sends the message that was in args and exits the program
    ///FOR NOW SIMPLY KILLS THE PROGRAM
    ipc.IPCSend( kill.c_str());
    ipc.IPCClose();
    return 0;
  }else{

    //if no argument, or one that is not a recognised command
    //is passed to this instande and an original instance is allready running
    //then send the kill command to shut the ptogram down
    if( !ipc.IPCOriginal()){

        ipc.IPCSend( kill.c_str());

    //else the program was called with bad arguments or without any
    //displaying notes on working commands
    }else{

      //notify of error
    }

    ipc.IPCClose();
    return 0;
  }


this is the IPC code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//constructor
IPC::IPC( std::string _location){

  //set fifo to home directory and check if there is allready a
  //fifo type file there
  fifo = _location.c_str();

  std::string path = _location;

  path += "revengeMusic.pid";

  pidfile = open( path.c_str(), O_CREAT | O_RDONLY);

  mkfifo( fifo, S_IWUSR | S_IRUSR |S_IRGRP | S_IROTH);
}


//sends a message to the original process
void IPC::IPCSend( const char buf[]){

  if( rc){

     fd = open( fifo, O_WRONLY);
     write( fd, buf, sizeof(buf));
     close( fd);

    perror("FML");

 }else{

   std::cout << "cannot write to read only fifo." << std::endl;
 }
}

//listens for an incomming message and returns it
std::string IPC::IPCGet(){

    if( rc == 0){

      fd = open( fifo, O_RDONLY);
      read(fd, message, MAX_BUF);
      std::cout << message << std::endl;
      close( fd);

      if( message[0] != '\0'){

        return std::string( message);
      }

      perror("FML");
    }else{

      std::cout << "cannot read from write only fifo." << std::endl;
    }
}
Topic archived. No new replies allowed.