int TembooMQTTClient::setDeviceId(char* id) { int rc = TEMBOO_ERROR_FAILURE; if (NULL == m_deviceId) { m_deviceId = new char[strlen(id) + 1]; if (NULL != m_deviceId) { strcpy(m_deviceId, id); makeTopics(); rc = TEMBOO_ERROR_OK; } else { rc = TEMBOO_ERROR_MEMORY; } } return rc; }
int main(void){ int n; int fdsrv, fdsrv_w,flags,bread,i; int fdcnt, bwrite, j; int n_user; struct message msg; struct message2 msg2; char str[5]; srand(time(NULL)); printf("ile tematow?\n"); scanf("%d", &n); makeTopics(n); printf("Works\n"); if((mkfifo("fifo1", S_IFIFO|0755)) == -1 ){ perror("Tworzenie fifo1"); exit(EXIT_FAILURE); } if((fdsrv = open("fifo1", O_RDONLY)) == -1){ perror("open fail"); exit(EXIT_FAILURE); } while(1){ if((bread = read(fdsrv, &msg, sizeof(msg))) == -1){ perror("czytanie z kolejki"); /*break;*/ } if(bread > 0){ printf("Message received %d\n", bread); printf("From %d:\n", msg.pid); printf("Msg size %d\n",msg.size); for(i=0;i<msg.size;i++){ printf("Topic %d\n", msg.topics[i] ); } sprintf(str, "%d", msg.pid); if((mkfifo(str, S_IFIFO|0755)) == -1){ perror("Tworzenie fifo dla klienta"); /*exit(EXIT_FAILURE);*/ } if((fdcnt = open(str, O_WRONLY)) == -1){ perror("fdcnt open fail"); exit(EXIT_FAILURE); } for(i=0;i<100;i++) msg2.subtopic[i] = 0; msg2.size = 0; if(addUser(msg) != 0){ printf("users_size %d\n", users_size); for(i=0;i<users_size;i++){ if(users[i].pid == msg.pid){ printf("\n"); n_user= i; i = users_size; } } for(i=0;i<users[n_user].size;i++){ printf("w forze\n"); if( users[n_user].topics[i] < topic_size){ msg2.is_topic = 1; msg2.topic = users[n_user].topics[i]; msg2.size =0; msg2.topic_size = users[n_user].size; printf("Message size[i] %d\n",message_size[i]); for(j=0;j<message_size[i];j++){ msg2.subtopic[j] = message_list[i][j]; msg2.size++; } printf("send\n"); if((bwrite = write(fdcnt, &msg2, sizeof(msg2))) == -1){ perror("write"); exit(EXIT_FAILURE); } printf("sended: %d", bwrite); } else{ msg2.is_topic = 0; msg2.topic = users[n_user].topics[i]; bwrite = write(fdcnt, &msg2, sizeof(msg2)); } } } close(fdcnt); } /*write(fdcnt,0,0);*/ unlink(fdcnt); } close(fdsrv); unlink("fifo1"); return 0; }