SHARE
TWEET

Untitled

a guest Nov 14th, 2017 124 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <wait.h>
  5. #include <signal.h>
  6. #include <string.h>
  7. #include <time.h>
  8. #include <sys/ipc.h>
  9. #include <sys/msg.h>
  10. #include <sys/types.h>
  11. #define KAMIEN 0
  12. #define PAPIER 1
  13. #define NOZYCE 2
  14. #define NENOUGHTREPORTS 3
  15. #define STATS 4
  16. #define CONNECTION 5
  17. #define ROZLACZENIE 6
  18. #define TRUE 1
  19. #define FALSE 0
  20. typedef struct msgStruct{
  21. long mtype;
  22. int pid;
  23. int messageType;
  24. int place;
  25. int numberOfPlayers;
  26. int points;
  27. } Msg;
  28. typedef struct statStruct{
  29. int points;
  30. int place;
  31. int pid;
  32. int reports;
  33. int symbol;
  34. int used;
  35. } Stats;
  36. int ipcId;
  37. Msg message;
  38. int msgSize=sizeof(Msg)-sizeof(long);
  39. Stats stats[2];
  40. int counter,ture;
  41. void sort(){
  42. int i, j, tempPoints,tempPid,tempUsed,tempReports,tmpCounter;
  43. for (i = 0; i<2-1; i++)
  44. {
  45. for (j=0; j<2-1-i; j++)
  46. {
  47. if (stats[j].points < stats[j+1].points)
  48. {
  49. tempPoints = stats[j+1].points;
  50. stats[j+1].points = stats[j].points;
  51. stats[j].points = tempPoints;
  52. tempPid=stats[j+1].pid;
  53. stats[j+1].pid=stats[j].pid;
  54. stats[j].pid=tempPid;
  55. tempUsed=stats[j+1].used;
  56. stats[j+1].used=stats[j].used;
  57. stats[j].used=tempUsed;
  58. tempReports=stats[j+1].reports;
  59. stats[j+1].reports=stats[j].reports;
  60. stats[j].reports=tempReports;
  61. }
  62. }
  63. }
  64. for (i=0;i<2;i++){
  65. if (stats[i].used==TRUE){
  66. stats[i].place=tmpCounter;
  67. tmpCounter++;
  68. printf("PID: %d, place: %d, points %d\n",stats[i].pid,stats[i].place,stats[i].points);
  69. }
  70. }
  71. }
  72. void fileExit(int nr_sig)
  73. {
  74. int i;
  75. for (i=0;i<2;i++){
  76. if(stats[i].pid!=0){
  77. message.messageType=ROZLACZENIE;
  78. message.mtype=stats[i].pid;
  79. msgsnd(ipcId,&message,msgSize,0);
  80. }
  81. }
  82. msgctl(ipcId,IPC_RMID,NULL);
  83. exit(0);
  84. }
  85.  
  86. void playerJoin(){
  87. if(counter==2){
  88. printf("cannot join , lack of place \n");
  89. message.messageType=ROZLACZENIE;
  90. message.mtype=message.pid;
  91. msgsnd(ipcId,&message,msgSize,0);
  92. } else {
  93. int i;
  94. for (i=0;i<2;i++){
  95. if (stats[i].pid==0)
  96. {
  97. printf("player %d join\n",message.pid);
  98. stats[i].pid=message.pid;
  99. stats[i].used=TRUE;
  100. break;
  101. }
  102. }    
  103. counter++;
  104. }
  105. }
  106.  
  107. void playerExit(){
  108. int i;
  109. for (i=0;i<2;i++){
  110. if (stats[i].pid==message.pid)
  111. {
  112. printf("player %d leave\n",message.pid);
  113. stats[i].pid=0;
  114. stats[i].used=FALSE;
  115. stats[i].points=0;
  116. stats[i].place=0;
  117. break;
  118. }
  119. }
  120. counter--;
  121. }
  122.  
  123. void sendResults(){
  124. int i;
  125. for (i=0;i<2;i++){
  126. if(stats[i].reports==TRUE){
  127. message.messageType=STATS;
  128. message.mtype=stats[i].pid;
  129. message.place=stats[i].place;
  130. message.numberOfPlayers=counter;
  131. message.points=stats[i].points;
  132. if(msgsnd(ipcId,&message,msgSize,0)!=0){
  133. perror("Blad wyslania messagei");
  134. }
  135. stats[i].reports=FALSE;
  136. }
  137. }
  138. }
  139.  
  140. void countPoints(){
  141. int points;
  142. int i;
  143. int counterK=0;
  144. int counterN=0;
  145. int counterP=0;
  146. for (i=0;i<2;i++){
  147. if (stats[i].reports==TRUE){
  148. if (stats[i].symbol==KAMIEN) counterK++;
  149. else if (stats[i].symbol==PAPIER) counterP++;
  150. else if (stats[i].symbol==NOZYCE) counterN++;
  151. }
  152. }
  153. for (i=0;i<2;i++){
  154. if (stats[i].reports==TRUE){
  155. if(stats[i].symbol==KAMIEN){
  156. points=counterN-counterP;
  157. stats[i].points+=points;
  158. printf("player: %d got %d points\n",stats[i].pid,points);
  159. }
  160. if(stats[i].symbol==PAPIER){
  161. points=counterK-counterN;
  162. stats[i].points+=points;
  163. printf("player: %d got %d points\n",stats[i].pid,points);
  164. }
  165. if(stats[i].symbol==NOZYCE){
  166. points=counterP-counterK;
  167. stats[i].points+=points;
  168. printf("player: %d got %d points\n",stats[i].pid,points);
  169. }
  170. }
  171. }
  172. }
  173. int main(){
  174. signal(SIGINT,fileExit);
  175. int key=ftok("/tmp/server.c",1337);
  176. ipcId=msgget(key,IPC_CREAT|0666);
  177. if (ipcId==-1)
  178. {
  179. perror("failed");
  180. exit(0);
  181. }
  182. while(1){
  183. sleep(10);
  184. printf("\n\n Ture: %d\n\n",ture);
  185. ture++;
  186. int notCounter=0;
  187. while(msgrcv(ipcId,&message,msgSize,1,IPC_NOWAIT)==msgSize){
  188. if (message.messageType==CONNECTION){
  189. playerJoin();
  190. }
  191. else if (message.messageType==ROZLACZENIE){
  192. playerExit();
  193. } else{
  194. int i;
  195. for (i=0;i<2;i++){
  196. if (stats[i].pid==message.pid)
  197. {
  198. printf("notification to player %d : ",message.pid);
  199. stats[i].symbol=message.messageType;
  200. if (stats[i].symbol==PAPIER) printf("papier\n");
  201. if (stats[i].symbol==KAMIEN) printf("kamien\n");
  202. if (stats[i].symbol==NOZYCE) printf("nozyce\n");
  203. stats[i].reports=TRUE;
  204. break;
  205. }
  206. }
  207. notCounter++;
  208. }
  209. }
  210. if (notCounter==0){
  211. printf("Brak zgloszen\n");
  212. } else if(notCounter==1){
  213. printf("Tylko jedno zgloszenie\n");
  214. int pid_odbiorcy=message.pid;
  215. message.messageType=NENOUGHTREPORTS;
  216. message.mtype=pid_odbiorcy;
  217. if(msgsnd(ipcId,&message,msgSize,0)!=0){
  218. perror("Blad wyslania messagei");
  219. }
  220. } else
  221. {    
  222. printf("Wystarczajaca liczba zgloszen\n");
  223. countPoints();
  224. sort();
  225. sendResults();    
  226. }
  227. }
  228. return 0;
  229. }
RAW Paste Data
Top