0% found this document useful (0 votes)
76 views6 pages

5 Forwarder-Receiver

This document describes a peer-to-peer message exchange scenario using TCP/IP as the underlying communication protocol. It includes class diagrams and sequence diagrams. Key classes include Forwarder for sending messages, Receiver for receiving messages, Message for encapsulating messages, and Registry for storing address mappings between nodes. The Server class ties it all together by initializing a Receiver, Forwarder, and Registry, sending a test message from Server to itself, and printing the received message.

Uploaded by

Komal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views6 pages

5 Forwarder-Receiver

This document describes a peer-to-peer message exchange scenario using TCP/IP as the underlying communication protocol. It includes class diagrams and sequence diagrams. Key classes include Forwarder for sending messages, Receiver for receiving messages, Message for encapsulating messages, and Registry for storing address mappings between nodes. The Server class ties it all together by initializing a Receiver, Forwarder, and Registry, sending a test message from Server to itself, and printing the received message.

Uploaded by

Komal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

5.

Forwarder-Receiver:
Example: A simple peer-to-peer message exchange scenario; Underlying communication
protocol is TCP/IP.

a.Pattern Instance

b.Class diagram
c.Sequence Diagram

Entry.java:

public class Entry


{
private String destinationId;
private int portNr;
public Entry(String theDest, int theport)
{
destinationId = theDest;
portNr = theport;
}
public String dest() {
return destinationId;
}
public int port() {
return portNr;
}
}

Forwarder.java:
import java.io.*;
import java.net.Socket;
public class Forwarder
{
public Server Forwarder;
private Socket s;
private OutputStream oStr;
private String myName;
private Registry reg;
public Forwarder(String theName, Registry reg)
{
myName = theName;
this.reg = reg;
}
private byte[] marshal(Message theMsg)
{
return theMsg.data.getBytes();
}
private void deliver(String theDest, byte[] data)
{
try
{
Entry entry = reg.get(theDest);

s = new Socket(entry.dest(), entry.port());


oStr = s.getOutputStream();
oStr.write(data);
oStr.flush();
oStr.close();
s.close();
}
catch(IOException e)
{
System.out.println("Forerror" +e);
}
}
Message.java:
public class Message
{
public String sender;
public String data;
public Message(String thesender, String rawData)
{
sender = thesender;
data = rawData;
}
}

Reciver.java:

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Reciver
{
private ServerSocket srvS;
private Registry reg;
private Socket s;
private InputStream iStr;
private String myName;
public Reciver(String theName, Registry reg)
{
myName = theName;
this.reg = reg;
try
{
Entry entry = reg.get(myName);

srvS = new ServerSocket(entry.port());


System.out.println("Server started");
}
catch (Exception e)
{
e.printStackTrace();
}
}
private Message unmarshal(byte[] anarray)
{
return new Message(myName, new String(anarray));
}
private byte[] receive()
{
int val;
byte buffer[] = null;
try
{
s = srvS.accept();
iStr = s.getInputStream();
val = iStr.read();
buffer = new byte[val];
iStr.read(buffer);
iStr.close();
s.close();
srvS.close();
}
catch (IOException e)
{
System.out.println("Error" + e);
}
return buffer;
}

public Message receiveMsg()


{
return unmarshal(receive());
}
private void IPCmsg()
{
}
}

Registry.java:

import java.util.*;

public class Registry


{
private Hashtable hTable = new Hashtable();
public void put(String theKey, Entry theEntry)
{
hTable.put(theKey, theEntry);
}
public Entry get(String aKey)
{
return (Entry) hTable.get(aKey);
}
}

Server.java:

public class Server


{
Reciver r;
Forwarder f;
static Registry reg = new Registry();
public void execute()
{
Message result = null;
r = new Reciver("Server", reg);
f = new Forwarder("Server", reg);
Message msg = new Message("Server", " I am alive");
f.sendMsg("Server", msg);
result = r.receiveMsg();
System.out.println(result.data.trim());
}
public static void main(String args[])
{
Entry entry = new Entry("127.0.0.1", 2900);
reg.put("Client", entry);
entry = new Entry("127.0.0.1", 2900);
reg.put("Server", entry);
new Server().execute();
}
}

You might also like