/** * Read file * * @param {char *} fileName * @param {int} socketFd */ void readFile(char *fileName, int socketFd) { if (fileExists(fileName) == 0) { attemptWrite(socketFd, FILE_NOT_FOUND, strlen(FILE_NOT_FOUND)); printf("File not found.\n"); return; } FILE *fp = fopen(fileName, "r"); if (fp == NULL) { printf("File could not be opened.\n"); return; } char *fileContents, *line; long fileLength; size_t length; ssize_t read; fseek(fp, 0, SEEK_END); fileLength = ftell(fp); fseek(fp, 0, SEEK_SET); fileContents = malloc(fileLength); if (fileContents) { fread(fileContents, 1, fileLength, fp); } attemptWrite(socketFd, fileContents, strlen(fileContents)); fclose(fp); }
/** * Handle ftp read * * @param {int} clientId * @param {int} clientSocketFd * @param {char *} fileName */ void handleFtpRead(int clientId, int clientSocketFd, char *fileName) { if (fileExists(fileName)) { printf("Client #%d - File found: `%s`\n", clientId, fileName); readFile(fileName, clientSocketFd); } else { printf("Client #%d - File not found: `%s`\n", clientId, fileName); attemptWrite(clientSocketFd, FILE_NOT_FOUND, strlen(FILE_NOT_FOUND)); } }
void NoStarveReadWriteMutex::acquireWrite() const { // if we can acquire the rwlock the easy way, we're done if (attemptWrite()) { return; } // failed to get the rwlock, do it the hard way: // locking the mutex and setting writerWaiting will cause all new readers to // block on the mutex rather than on the rwlock. mutex_.lock(); writerWaiting_ = true; ReadWriteMutex::acquireWrite(); writerWaiting_ = false; mutex_.unlock(); }
bool NoStarveReadWriteMutex::timedWrite(int64_t milliseconds) const { // if we can acquire the rwlock the easy way, we're done if (attemptWrite()) { return true; } // failed to get the rwlock, do it the hard way: // locking the mutex and setting writerWaiting will cause all new readers to // block on the mutex rather than on the rwlock. if (!mutex_.timedlock(milliseconds)) { return false; } writerWaiting_ = true; int ret = ReadWriteMutex::timedWrite(milliseconds); writerWaiting_ = false; mutex_.unlock(); return ret == 0; }
/** * Handle a new connection * * @param {int} newSocketFd */ void handleConnection(int newSocketFd) { char ioBuffer[256]; memset(ioBuffer, '\0', 256); // reset the ioBuffer for i/o attemptRead(newSocketFd, ioBuffer, 255); printf("Before call to isValidClientId(), "); printClients(); int clientId = atoi(ioBuffer); int isValidId = isValidClientId(clientId); printf("Checking for valid client id...\n"); if (isValidId == 0) { attemptWrite(newSocketFd, INVALID_CLIENT_ID, strlen(INVALID_CLIENT_ID)); return; } else { attemptWrite(newSocketFd, VALID_CLIENT_ID, strlen(VALID_CLIENT_ID)); memset(ioBuffer, '\0', 256); // reset the ioBuffer for i/o attemptRead(newSocketFd, ioBuffer, 255); printf("%s\n", ioBuffer); // print client id response message from client addClient(clientId); printf("After call to addClient(), "); printClients(); attemptWrite(newSocketFd, WELCOME_MESSAGE, strlen(WELCOME_MESSAGE)); printf("New client #%d\n", clientId); while (1) { printf("Waiting for command from client #%d...\n", clientId); char opFile[256]; // read opFile from client memset(ioBuffer, '\0', 256); // reset the ioBuffer for i/o attemptRead(newSocketFd, ioBuffer, 255); strcpy(opFile, ioBuffer); // if client requests, end session if (strcmp(ioBuffer, END_SESSION) == 0) { printf("Terminating client #%d\n", clientId); removeClient(clientId); return; } // otherwise, send continue response attemptWrite(newSocketFd, CONTINUE, strlen(CONTINUE)); // read opCode from client memset(ioBuffer, '\0', 256); // reset the ioBuffer for i/o attemptRead(newSocketFd, ioBuffer, 255); // proceed based on opCode if (strcmp(ioBuffer, PROCESS_READ) == 0) { printf("Processing read for file: `%s`\n", opFile); readFile(opFile, newSocketFd); // send file to client } else if (strcmp(ioBuffer, PROCESS_WRITE) == 0) { printf("Processing write for file: `%s`\n", opFile); attemptWrite(newSocketFd, CONTINUE, strlen(CONTINUE)); memset(ioBuffer, '\0', 256); attemptRead(newSocketFd, ioBuffer, 255); // read file contents writeToFile(opFile, ioBuffer); } } } }