Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
muktar1907 committed May 2, 2024
2 parents 515d1c9 + b370b18 commit ab9847f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
43 changes: 29 additions & 14 deletions src/main/java/uta/cse3310/HttpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,49 @@ public HttpServer(int portNum, String dirName)
dirname = dirName;
}
//method begin
public void begin()
{
try
{
// Method to start the HTTP server
public void begin() {
try {
// Create a File object representing the directory
File dir = new File(dirname);

// Check if directory is readable, throw exception if not
if (!dir.canRead())
throw new FileNotFoundException(dir.getAbsolutePath());

// Create an HTTPServer instance listening on the specified port
HTTPServer server = new HTTPServer(port);
VirtualHost host = server.getVirtualHost(null);
host.setAllowGeneratedIndex(true);

// Get the default virtual host from the server
VirtualHost host = server.getVirtualHost(null);

// Allow generated index for the virtual host
host.setAllowGeneratedIndex(true);

// Add context for serving files from root directory
host.addContext("/", new FileContextHandler(dir));
host.addContext("/api/time", new ContextHandler()
{
public int serve(Request req, Response resp) throws IOException
{

// Add context for serving current time
host.addContext("/api/time", new ContextHandler() {
// Serve method to handle requests for current time
public int serve(Request req, Response resp) throws IOException {
// Get current time in milliseconds
long now = System.currentTimeMillis();

// Set content type header
resp.getHeaders().add("Content-Type", "text/plain");

// Send response with current time
resp.send(200, String.format("%tF %<tT", now));
return 0;
}
});

// Start the HTTP server
server.start();
} catch (Exception e)
{
} catch (Exception e) {
// Print error message if any exception occurs
System.err.println("error: " + e);
}

}

}
17 changes: 14 additions & 3 deletions src/main/java/uta/cse3310/WordList.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,50 @@
import java.nio.file.Paths;

public class WordList {
// Static list to store words read from file
public static List<String> list;

// Method to read words from a file and return shuffled word list
public static ArrayList<String> getWordList(String wordsFile) {
ArrayList<String> wordList = new ArrayList<>();

try {
// Read all lines from the specified file
List<String> lines = Files.readAllLines(Paths.get(wordsFile));

// If file is empty, print message and return empty list
if (lines.isEmpty()) {
System.out.println("The file is empty: " + wordsFile);
return wordList; // Return empty list if the file is empty
return wordList;
}

// Store lines in a static list and shuffle the words
list = new ArrayList<>(lines);
shuffleWords();

// Add shuffled words to word list
wordList.addAll(list);
} catch (IOException e) {
// Print error message and stack trace if an exception occurs
System.out.println("Error reading the file: " + wordsFile);
e.printStackTrace(); // Print the stack trace for debugging
e.printStackTrace();
}

return wordList;
}

// Method to shuffle words in the static list
public static void shuffleWords() {
Collections.shuffle(list);
}

// Method to update word list based on character limit
public static ArrayList<String> updatedWordList(ArrayList<String> wordBank) {
int totalCharacters = 0;
int index = 0;
ArrayList<String> updatedList = new ArrayList<>();

// Iterate through word bank and add words until character limit is reached
while (totalCharacters < 500 && index < wordBank.size()) {
String word = wordBank.get(index);
if (totalCharacters + word.length() <= 500) {
Expand All @@ -55,4 +67,3 @@ public static ArrayList<String> updatedWordList(ArrayList<String> wordBank) {

return updatedList;
}
}

0 comments on commit ab9847f

Please sign in to comment.