JAVA PROGRAM: ( Model Message/Transfer System) Build a object oriented model of
ID: 3884322 • Letter: J
Question
JAVA PROGRAM: (Model Message/Transfer System)
Build a object oriented model of parts of a message transfer system that can transfer digital data between two end points (perhaps with intermediate relays). Start with three objects: message, transmitter, receiver.
So far this is the code for Message class:
public class Message {
static long messageID = 0;
String message;
String contentType = "text/plain";
String source;
String destination;
// Constructors
Message(){
message = "";
}
Message(String msg){
message = msg;
}
// Accessors
public long getUID() {
return messageID;
}
public String getSource() {
return source;
}
public String getDestination() {
return destination;
}
public String getType() {
return contentType;
}
public String toString(){
return "Message " + messageID+ ": "+message;
}
public String dump(){
return "Message " + messageID+ ": "+message + " From: "+ source + " to: "+destination;
}
// Mutators
public void setContent(String text){
message = text;
}
void setSource(String address) {
source=address;
}
void setDestination(String address) {
destination=address;
}
}
Still need Receiver and Transmitter class:
For Receiver class:
A receiver is an endpoint of a transmission. For this program, a receiver may be connected to a transmitter. The transmitter delivers a message to the receiver. The receiver can note that a message is available and it can be retrieved by “getting the message”. In this program, if a second message comes in before the first is retrieved then an overrun condition exists. The overrun condition can be cleared with with a reset which will also clear any available message.
>Constructors
-Receiver() - create a receiver with an address of “unknown”
-Receiver(String address) - create a receiver with specified address”
>Queries
-String getAddress() - returns address of receiver
-boolean overrun() - returns true if one or more messages are delivered while the receiver is holding a message
-boolean available() - returns true if message available
>Commands
-void deliver( Message message) - deliver message into Receiver
-Message getMessage() - returns message from receiver if available or null otherwise
-void reset() - resets the overrun condition on the receiver and clears any stored message inside the receiver
For Transmitter class:
A transmitter is an origination point for a message communication. In this program, a receiver may be directly connected to the transmitter. The transmitter can send a message to the connected receiver attaching its address as the source address of the message and the receiver’s address as the destination address.
>Constructors
-Transmitter() - create a transmitter with the source address “unknown”.
-Transmitter(String address) - create a transmitter with the specified address.
>Queries
-String getAddress() - return the address of the transmitter
>Commands
-void connect(Receiver receiver) - connect to the specified receiver so that messages can be sent to it. Only one connection at a time is possible for this program. Fail silently if there are issues.
-void send(Message message) - send message to connected receiver if any (fail silently), apply addresses for source and destination
-void disconnect() - disconnect from the connected receiver if any (fail silently)
Explanation / Answer
public class receiver
{
private static Socket part;
public static void main(String[] args)
{
try
{
int port = 25000;
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server Started and listening to the port 25000");
while(true)
{
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String number = br.readLine();
System.out.println("Message received from client is "+number);
try
{
int numberInIntFormat = Integer.parseInt(number);
int returnValue = numberInIntFormat*2;
returnMessage = String.valueOf(returnValue) + " ";
}
catch(NumberFormatException e)
{
returnMessage = "Please send a proper number ";
}
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(returnMessage);
System.out.println("Message sent to the client is "+returnMessage);
bw.flush();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
socket.close();
}
catch(Exception e){}
}
}}
public class transmitter
{
public static void main (String [] args )
throws IOException
{
int filesize=1022386;
int bytesRead;
int currentTot = 0;
Socket socket = new Socket("127.0.0.1",15123);
byte [] bytearray = new byte [filesize];
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("copy.doc"); BufferedOutputStream bos = new BufferedOutputStream(fos); bytesRead = is.read(bytearray,0,bytearray.length);
currentTot = bytesRead;
do
{
bytesRead = is.read(bytearray, currentTot, (bytearray.length-currentTot)); if(bytesRead >= 0) currentTot += bytesRead;
}
while(bytesRead > -1);
write(bytearray, 0 , currentTot);
flush();
close();
socket.close();
}
}
private QueueConnectionFactory qconFactory;
private QueueConnection qcon;
private QueueSession qsession;
private QueueSender qsender;
private Queue queue;
private TextMessage msg;
/**
* Creates all the necessary objects for sending
* messages to a JMS queue.
*
* @param ctx JNDI initial context
* @param queueName name of queue
* @exception NamingException if operation cannot be performed
* @exception JMSException if JMS fails to initialize due to internal error
*/
public void init(Context ctx, String queueName)
throws NamingException, JMSException
{
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup(queueName);
qsender = qsession.createSender(queue);
msg = qsession.createTextMessage();
qcon.start();
}
/**
* Sends a message to a JMS queue.
*
* @param message message to be sent
* @exception JMSException if JMS fails to send message due to internal error
*/
public void send(String message) throws JMSException {
msg.setText(message);
qsender.send(msg);
}
/**
* Closes JMS objects.
* @exception JMSException if JMS fails to close objects due to internal error
*/
public void close() throws JMSException {
qsender.close();
qsession.close();
qcon.close();
}
main() method.
@param args WebLogic Server URL
@exception Exception if operation fails
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("Usage: java examples.jms.queue.QueueSend WebLogicURL");
return;
}
InitialContext ic = getInitialContext(args[0]);
QueueSend qs = new QueueSend();
qs.init(ic, QUEUE);
readAndSend(qs);
qs.close();
}
private static void readAndSend(QueueSend qs)
throws IOException, JMSException
{
BufferedReader msgStream = new BufferedReader(new InputStreamReader(System.in));
String line=null;
boolean quitNow = false;
do {
System.out.print("Enter message ("quit" to quit): ");
line = msgStream.readLine();
if (line != null && line.trim().length() != 0) {
qs.send(line);
System.out.println("JMS Message Sent: "+line+" ");
quitNow = line.equalsIgnoreCase("quit");
}
} while (! quitNow);
}
private static InitialContext getInitialContext(String url)
throws NamingException
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY)
return new InitialContext(env);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.