Advertisement
Guest User

msgLogger.c

a guest
Feb 25th, 2013
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.65 KB | None | 0 0
  1. /**
  2. A server that receives messages from a client and stores them in a file along
  3. with the current date and the identifiers
  4. **/
  5.  
  6. #include "msg.h"
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <sys/neutrino.h>
  11.  
  12. #include <time.h>
  13.  
  14. int main(int argc, char *argv[]) {
  15.  
  16. //The node descriptor, process id, and connection id
  17. int nd = 0;
  18. int pid = getpid();
  19. int chid;
  20.  
  21. //Where the message wil be stored
  22. MESSAGE msg;
  23.  
  24. //File name for the PID file and the message file
  25. int rcvid;
  26. char *pidFileName;
  27. FILE *pidHandle;
  28. FILE *msgLogHandle;
  29.  
  30. //To get the current time
  31. time_t currentCtime;
  32. char currentDate[26];
  33.  
  34. //Name of the log file entered
  35. char *msgLogFile = argv[1];
  36.  
  37. //Allocate memory and append .pid to the file
  38. pidFileName = malloc(strlen(argv[0]) + 5);
  39.  
  40. strcpy(pidFileName, argv[0]);
  41. strcat(pidFileName, ".pid");
  42.  
  43. //Open the file
  44. pidHandle = fopen(pidFileName, "w");
  45.  
  46. //Check if channel was created
  47. if ((chid = ChannelCreate(0)) == -1)    {
  48.         perror("Logger couldn't create a channel");
  49.         exit(EXIT_FAILURE);
  50. }
  51.  
  52. //Place the identifier contents into the pid file
  53. if (fprintf(pidHandle, "%d %d %d", nd, pid, chid) < 0)  {
  54.         perror("Logger failed to write its pid file \n");
  55.         exit(EXIT_FAILURE);
  56. }
  57.  
  58. fclose(pidHandle);
  59.  
  60. //Open the message log
  61. msgLogHandle = fopen(msgLogFile, "w");
  62.  
  63.  
  64.   for(;;) {
  65.                 // Block and wait for a request from a client
  66.  
  67.                 rcvid = MsgReceive (chid, &msg, sizeof(msg), NULL);
  68.  
  69.                 switch (msg.m_hdr)      {
  70.  
  71.                         case MSG_DATA:
  72.  
  73.                                 //Message successfully received, store the data into the file
  74.                                 MsgReply(rcvid, MSG_OK, &msg, sizeof(msg));
  75.  
  76.                                 currentCtime = time(NULL);
  77.                                 ctime_r(&currentCtime, currentDate);
  78.                                 fprintf(msgLogHandle, "%s %d/%d/%d sent: %s \n", currentDate, nd, pid, chid, msg.m_data);
  79.                                 fflush(msgLogHandle);
  80.                                 break;
  81.  
  82.                         case MSG_END:
  83.  
  84.                          //End of message
  85.                                 MsgReply(rcvid, MSG_END, NULL, NULL);
  86.  
  87.                                 break;
  88.  
  89.                         default:
  90.  
  91.                                 //error message is logged
  92.  
  93.                                 MsgReply(rcvid, MSG_INVALID, &msg, sizeof(msg));
  94.  
  95.                                 break;
  96.  
  97.                                 }
  98.   }
  99.  
  100.  
  101. fclose(pidHandle);
  102.  
  103. //Usually won't get to this part
  104. MsgReply(rcvid, EOK, &msg, sizeof (msg));
  105.  
  106.  
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement