Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
CloseConnect---Projeckszi/additional topics
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
198 lines (134 sloc)
8.2 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// code by zain | |
import java.time.LocalDateTime; // recieves LocalDateTime class from java.time package | |
import java.time.format.DateTimeFormatter; // recieves DateTimeFormatter from java.time.format package | |
import java.util.*; // provides access to arrays | |
import java.util.Date; | |
import java.io.*; | |
import java.util.concurrent.*; | |
import java.util.concurrent.atomic.*; | |
import java.text.SimpleDateFormat; // recieves from java.text package | |
// recieving/ publishing time and date version 1 | |
public static String getCurrentTime(){ // Method declaration for getting current time as a string | |
SimpleDateFormat dateFormat = new SimpleDateFormat( // Creating a SimpleDateFormat object for formatting date and time | |
"dd-MMM-yyyy HH:mm:ss"); | |
long timestamp = System.currentTimeMillis(); // Getting current time in milliseconds | |
Date date=new Date(timestamp); // Creating a Date object using the timestamp | |
String timestampStr = dateFormat.format(date); | |
return timestampStr; // Returning the formatted date and time as a string | |
} | |
--------------------------------------------------------------------------------------------------------------------------------------- | |
// code from here onwards was not used in main mqtt project | |
// recieving/ publishing time and date version 2 | |
public class Main { // Define public class | |
public static void publishTimeDateLocation(String client, String currentLocation) { // Defining method to publish current time, date, and location | |
LocalDateTime now = LocalDateTime.now(); // Getting current date and time | |
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ("yyyy-MM-dd HH:mm:ss"); //defining formatter to format the date and time | |
String currentTimeDate = now.format(formatter); // Format the current date and time using the defined formatter | |
String message = "Date: " + currentTimeDate + ", Location: " + currentLocation; // writing a message for output of time, date and location | |
System.out.println("Publishing to topic: 'TIME, DATE AND LOCATION'"); // Printing message showing publishing to "TIME, DATE AND LOCATION" topic | |
System.out.println(message); | |
} | |
public static void main(String[] args) { // main method | |
// Example of output: | |
publishTimeDateLocation("client", "Coventry"); | |
} | |
} | |
-------------------------------------------------------------------------------------------------------------------------------- | |
// getting time date and location updates | |
public class Main { // defining public class | |
// Handling time updates | |
public static void handleTimeUpdate(String time) { // Defining method to handle time updates | |
System.out.println("Time update: " + time); // Printing message indicating a time update | |
} | |
// Handling date updates | |
public static void handleDateUpdate(String date) { // Defining method to handle date updates | |
System.out.println("Date update: " + date); // Printing message indicating a date update | |
} | |
// Handling location updates | |
public static void handleLocationUpdate(String location) { // Defining method to handle location updates | |
System.out.println("Location update: " + location); // Printing message indicating a location update | |
} | |
public static void main(String[] args) { // main method | |
// Example output: | |
String currentTime = "12:00 PM"; // output current time/date/location | |
String currentDate = "2024-03-21"; | |
String currentLocation = "Birmingham"; | |
// Publishing the updates | |
handleTimeUpdate(currentTime); | |
handleDateUpdate(currentDate); | |
handleLocationUpdate(currentLocation); | |
} | |
} | |
------------------------------------------------------------------------------------------------------------------------------ | |
//error handling techniques | |
public class MessagingProtocol { // defining public class named MessagingProtocol | |
static final String BROKER_URL = "tcp://mqtt.eclipseprojects.io:1883"; // Define constants for broker URL and topic | |
static final String TOPIC = "MESSAGE"; | |
public static void main(String[] args) { // Main method | |
try { | |
BrokerConnection(); // connecting to broker | |
} catch (Exception e) { | |
handleException(e); //// Handling any exceptions that occur during connection establishment | |
} | |
} | |
private static void BrokerConnection() { // Method for establishing connection to broker | |
System.out.println("Connected to broker."); // message showing connection to a broker | |
System.out.println("Subscribed to topic: " + TOPIC); // message showing subscription to a topic | |
// Keeping the program running | |
while (true) { | |
} | |
} | |
// Handling exceptions and outputting results | |
private static void handleException(Exception e) { | |
System.err.println("An exception occurred: " + e.getMessage()); | |
} | |
} | |
------------------------------------------------------------------------------------------------------------------------------- | |
//priority levels for messages | |
public class MessagePriorityPublisher { // defining public class called MessagePriorityPublisher | |
private static final String TOPIC = "PRIORITY_TOPIC"; // Defining constant for the topic to publish messages | |
public static void main(String[] args) { // main method | |
try { | |
// Loop indefinitely to continuously publish messages | |
while (true) { | |
// Generating new message | |
String message = createMessage(); | |
int priority = determinePriorityLevel(); // Determining the priority level of the message | |
// Adjusting Quality of Service (QoS) level based on the priority level | |
int qos = adjustQoSLevel(priority); | |
// Publishing message with its according priority level | |
System.out.println("Published '" + message + "' to Topic " + TOPIC + " with Priority level " + priority); | |
// Sleep for some time before publishing next message | |
Thread.sleep(6000); | |
} | |
} catch (InterruptedException e) { // Printing the stack trace if thread is interrupted | |
e.printStackTrace(); | |
} | |
} | |
private static String createMessage() { | |
// Adding logic to get the message content | |
return "New message recieved at " + getCurrentTime(); | |
} | |
private static int determinePriorityLevel() { | |
// Adding logic to determine the priority level | |
// For example, return a value between 0 and 2 for the priority level | |
return (int) (Math.random() * 3); | |
} | |
private static int adjustQoSLevel(int priority) { | |
// Adjusting the QoS level based on the priority level | |
switch (priority) { | |
case 0: | |
return 0; // Lowest priority level, QOS 0 | |
case 1: | |
return 1; // Medium priority level, QOS 1 | |
case 2: | |
return 2; // Highest priority level, QOS 2 | |
default: | |
return 1; // Default to QOS 1 if priority level is not retained | |
} | |
} | |
private static String getCurrentTime() { // Method to get current time | |
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // Create a SimpleDateFormat object with desired date format | |
return dateFormat.format(new Date()); // Getting current date and time formatted according to the SimpleDateFormat | |
} | |
} | |