import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.net.InetAddress;
import java.io.UnsupportedEncodingException;
import java.net.UnknownHostException;


public class USBServer {

    public USBServer() throws UnsupportedEncodingException, UnknownHostException {

        while (true) {
            String request = USB.receive();

            if (request != null) {

                System.out.println("Request " + request + " length = " + request.length());

                if (request.startsWith("DATE")) {
                    DateFormat dateFormat = new SimpleDateFormat("EEE, MMM d, yyyy. 'Week number ' w'.'");
                    Date date = new Date();
                    USB.send(dateFormat.format(date));
                }

                if (request.startsWith("TIME")) {
                    DateFormat dateFormat = new SimpleDateFormat("'Current time is ' HH:mm:ss");
                    Date date = new Date();
                    USB.send(dateFormat.format(date));
                }

                if (request.startsWith("GREETINGS")) {
                    USB.send("Hello, from " + InetAddress.getLocalHost());
                }

                if (request.startsWith("SHUTDOWN")) {
                    DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
                    Date date = new Date();
                    USB.send("Goodbye! Shutdown at " + dateFormat.format(date));
                    break;
                }                   
            }
        }

    }

    public static void main(String[] args) throws UnsupportedEncodingException, UnknownHostException {
        new USBServer();
    }
}
