74
EX.NO: IMPLEMENTATION OF DYNAMIC MULTICASTING DATE: WITH CONCURRENCY CONTROL AIM: To implement dynamic multicasting with concurrency control using network simulator tool. PROCEDURE: 1. Create a tcl script called “dynamic multicasting.tcl” 2. Execute the script by “$ ns dynamic_multicasting.tcl” 3. In the script, two files are generated namely dynamic_multicasting.tr as tracefile and dynamic_multicasting.nam as the network animator file. CODING: # Create scheduler #Create an event scheduler wit multicast turned on set ns [new Simulator -multicast on] #$ns multicast #Turn on Tracing settf [open output.tr w] $ns trace-all $tf # Turn on nam Tracing

Network programs

Embed Size (px)

Citation preview

EX.NO: IMPLEMENTATION OF DYNAMIC MULTICASTING DATE:WITH CONCURRENCY CONTROL

AIM: To implement dynamic multicasting with concurrency control using network simulator tool.

PROCEDURE:1. Create a tcl script called dynamic multicasting.tcl2. Execute the script by $ ns dynamic_multicasting.tcl3. In the script, two files are generated namely dynamic_multicasting.tr as tracefile and dynamic_multicasting.nam as the network animator file.

CODING:

# Create scheduler

#Create an event scheduler wit multicast turned on

set ns [new Simulator -multicast on]

#$ns multicast

#Turn on Tracing

settf [open output.tr w]

$ns trace-all $tf

# Turn on nam Tracing

setfd [open mcast.nam w]

$ns namtrace-all $fd

# Create nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

set n4 [$ns node]

set n5 [$ns node]

set n6 [$ns node]

set n7 [$ns node]

# Create links

$ns duplex-link $n0 $n2 1.5Mb 10ms DropTail

$ns duplex-link $n1 $n2 1.5Mb 10ms DropTail

$ns duplex-link $n2 $n3 1.5Mb 10ms DropTail

$ns duplex-link $n3 $n4 1.5Mb 10ms DropTail

$ns duplex-link $n3 $n7 1.5Mb 10ms DropTail

$ns duplex-link $n4 $n5 1.5Mb 10ms DropTail

$ns duplex-link $n4 $n6 1.5Mb 10ms DropTail

# Routing protocol: say distance vector

#Protocols: CtrMcast, DM, ST, BST

setmproto DM

setmrthandle [$ns mrtproto $mproto {}]

# Allocate group addresses

set group1 [Node allocaddr]

set group2 [Node allocaddr]

# UDP Transport agent for the traffic source

set udp0 [new Agent/UDP]

$ns attach-agent $n0 $udp0

$udp0 set dst_addr_ $group1

$udp0 set dst_port_ 0

set cbr1 [new Application/Traffic/CBR]

$cbr1 attach-agent $udp0

# Transport agent for the traffic source

set udp1 [new Agent/UDP]

$ns attach-agent $n1 $udp1

$udp1 set dst_addr_ $group2

$udp1 set dst_port_ 0

set cbr2 [new Application/Traffic/CBR]

$cbr2 attach-agent $udp1

# Create receiver

set rcvr1 [new Agent/Null]

$ns attach-agent $n5 $rcvr1

$ns at 1.0 "$n5 join-group $rcvr1 $group1"

set rcvr2 [new Agent/Null]

$ns attach-agent $n6 $rcvr2

$ns at 1.5 "$n6 join-group $rcvr2 $group1"

set rcvr3 [new Agent/Null]

$ns attach-agent $n7 $rcvr3

$ns at 2.0 "$n7 join-group $rcvr3 $group1"

set rcvr4 [new Agent/Null]

$ns attach-agent $n5 $rcvr1

$ns at 2.5 "$n5 join-group $rcvr4 $group2"

set rcvr5 [new Agent/Null]

$ns attach-agent $n6 $rcvr2

$ns at 3.0 "$n6 join-group $rcvr5 $group2"

set rcvr6 [new Agent/Null]

$ns attach-agent $n7 $rcvr3

$ns at 3.5 "$n7 join-group $rcvr6 $group2"

$ns at 4.0 "$n5 leave-group $rcvr1 $group1"

$ns at 4.5 "$n6 leave-group $rcvr2 $group1"

$ns at 5.0 "$n7 leave-group $rcvr3 $group1"

$ns at 5.5 "$n5 leave-group $rcvr4 $group2"

$ns at 6.0 "$n6 leave-group $rcvr5 $group2"

$ns at 6.5 "$n7 leave-group $rcvr6 $group2"

# Schedule events

$ns at 0.5 "$cbr1 start"

$ns at 9.5 "$cbr1 stop"

$ns at 0.5 "$cbr2 start"

$ns at 9.5 "$cbr2 stop"

#post-processing

$ns at 10.0 "finish"

proc finish {} {

global ns tf

$ns flush-trace

close $tf

execnammcast.nam&

exit 0

}

# Fornam

#Colors for packets from two mcast groups

$ns color 10 red

$ns color 11 green

$ns color 30 purple

$ns color 31 green

# Manual layout: order of the link is significant!

#$ns duplex-link-op $n0 $n1 orient right

#$ns duplex-link-op $n0 $n2 orient right-up

#$ns duplex-link-op $n0 $n3 orient right-down

# Show queue on simplex link n0->n1

#$ns duplex-link-op $n2 $n3 queuePos 0.5

# Group 0 source

$udp0 set fid_ 10

$n0 color red

$n0 label "Source 1"

# Group 1 source

$udp1 set fid_ 11

$n1 color green

$n1 label "Source 2"

$n5 label "Receiver 1"

$n5 color blue

$n6 label "Receiver 2"

$n6 color blue

$n7 label "Receiver 3"

$n7 color blue

#$n2 add-mark m0 red

#$n2 delete-mark m0"

# Animation rate

$ns set-animation-rate 3.0ms

$ns run

OUTPUT:

RESULT:Thus the dynamic multicasting with concurrency control has been implemented successfully.

EX NO: IMPLEMENTATION OF D* ALGORITHMDATE: WITH SPATIAL DATA STRUCTURES

AIM:To write a program to implement D* algorithm with spatial data structures.ABOUT D* ALGORITHM:The original D* ALGORITHM was introduced by Anthony Stentz in 1994. The name D* comes from the term "Dynamic A*", because the algorithm behaves like A* except that the arc costs can change as the algorithm runs. It is an incremental search algorithm. OperationLike Dijkstra's algorithm and A*, D* maintains a list of nodes to be evaluated, known as the "OPEN list". Nodes are marked as having one of several states: NEW, meaning it has never been placed on the OPEN list OPEN, meaning it is currently on the OPEN list CLOSED, meaning it is no longer on the OPEN list RAISE, indicating its cost is higher than the last time it was on the OPEN list LOWER, indicating its cost is lower than the last time it was on the OPEN list

PROCEDURE:1. Start the program.2. Include the Dstar.h header file.3. In the main () method call dstar() method and input the start state.4. Perform the initialize, update cell, get path and replan operation.5. Display the goal state and its cost.6. Stop the program.

CODING:#include "Dstar.h"

int main() {Dstar *dstar = new Dstar();list mypath;

dstar->init(0,0,10,5); // set start to (0,0) and goal to (10,5)dstar->updateCell(3,4,-1); // set cell (3,4) to be non traversabledstar->updateCell(2,2,42.432); // set set (2,2) to have cost 42.432

dstar->replan(); // plan a pathmypath = dstar->getPath(); // retrieve path

dstar->updateStart(10,2); // move start to (10,2)dstar->replan(); // plan a pathmypath = dstar->getPath(); // retrieve path

dstar->updateGoal(0,1); // move goal to (0,1)dstar->replan(); // plan a pathmypath = dstar->getPath(); // retrieve path

return 0;}

RESULT:Thus the program to implement D* algorithm with spatial data structures is successfully executed and verified.EX No: IMPLEMENTATION OF 2D TRANSFORMATIONDATE: TRANSLATION, SCALING, ROTATION, MIRROR REFLECTION AND SHEARING WITH A MENU DRIVEN PROGRAM

AIM: To implement 2D transformation: Translation, Scaling, Rotation, Mirror Reflection and Shearing with a menu driven program.

PROCEDURE:1. Create a java class called Transform.java2. The operations like Translation, Scaling , Rotation, Mirror Reflection and Shearing are performed by using the methods of AffineTransform class of the java.awt package.3. Embed this Transform.java with an html file called Transform.html4. Compile the file using javac Transform.java5. Run the applet by using appletviewer Transform.html

CODING:

import java.lang.Integer;import java.awt.*;import java.awt.event.*;import java.awt.font.*;import java.awt.geom.*;import java.awt.image.*;import java.awt.event.ItemListener;import java.awt.event.ItemEvent;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import javax.swing.*;/* * This applet renders a shape, selected by the user, with a paint,stroke, and rendering method, * also selected by the user.*/public class Transform extends JApplet implements ItemListener,ActionListener { JLabel primLabel, lineLabel, paintLabel, transLabel, strokeLabel; TransPanel display; static JComboBox primitive, line, paint, trans, stroke; JButton redraw; public static boolean no2D = false; public void init() { GridBagLayout layOut = new GridBagLayout(); getContentPane().setLayout(layOut); GridBagConstraints c = new GridBagConstraints(); c.weightx = 1.0; c.fill = GridBagConstraints.BOTH; primLabel = new JLabel(); primLabel.setText("Primitive"); Font newFont = getFont().deriveFont(1); primLabel.setFont(newFont); primLabel.setHorizontalAlignment(JLabel.CENTER); layOut.setConstraints(primLabel, c); getContentPane().add(primLabel);

lineLabel = new JLabel(); lineLabel.setText("Lines"); lineLabel.setFont(newFont); lineLabel.setHorizontalAlignment(JLabel.CENTER); layOut.setConstraints(lineLabel, c); getContentPane().add(lineLabel);

paintLabel = new JLabel(); paintLabel.setText("Paints"); paintLabel.setFont(newFont); paintLabel.setHorizontalAlignment(JLabel.CENTER); layOut.setConstraints(paintLabel, c); getContentPane().add(paintLabel);

c.gridwidth = GridBagConstraints.RELATIVE; transLabel = new JLabel(); transLabel.setText("Transforms"); transLabel.setFont(newFont); transLabel.setHorizontalAlignment(JLabel.CENTER); layOut.setConstraints(transLabel, c); getContentPane().add(transLabel);

c.gridwidth = GridBagConstraints.REMAINDER; strokeLabel = new JLabel(); strokeLabel.setText("Rendering"); strokeLabel.setFont(newFont); strokeLabel.setHorizontalAlignment(JLabel.CENTER);

layOut.setConstraints(strokeLabel, c); getContentPane().add(strokeLabel); GridBagConstraints ls = new GridBagConstraints(); ls.weightx = 1.0; ls.fill = GridBagConstraints.BOTH; primitive = new JComboBox( new Object []{ "rectangle", "ellipse", "text"}); primitive.addItemListener(this); newFont = newFont.deriveFont(0, 14.0f); primitive.setFont(newFont); layOut.setConstraints(primitive, ls); getContentPane().add(primitive);

line = new JComboBox( new Object []{ "thin", "thick", "dashed"}); line.addItemListener(this); line.setFont(newFont); layOut.setConstraints(line, ls); getContentPane().add(line);

paint = new JComboBox( new Object[]{ "solid", "gradient", "polka"}); paint.addItemListener(this); paint.setFont(newFont); layOut.setConstraints(paint, ls); getContentPane().add(paint);

ls.gridwidth = GridBagConstraints.RELATIVE; trans = new JComboBox( new Object[]{"Identity","rotate","scale","shear","translate","mirror reflection"});trans.addItemListener(this);trans.setFont(newFont);layOut.setConstraints(trans, ls);getContentPane().add(trans); ls.gridwidth = GridBagConstraints.REMAINDER; stroke = new JComboBox( new Object[]{ "Stroke", "Fill", "Stroke & Fill"}); stroke.addItemListener(this); stroke.setFont(newFont); layOut.setConstraints(stroke, ls); getContentPane().add(stroke);GridBagConstraints button = new GridBagConstraints();button.gridwidth = GridBagConstraints.REMAINDER;redraw = new JButton("Redraw");redraw.addActionListener(this); redraw.setFont(newFont); layOut.setConstraints(redraw, button); getContentPane().add(redraw); GridBagConstraints tP = new GridBagConstraints(); tP.fill = GridBagConstraints.BOTH; tP.weightx = 1.0; tP.weighty = 1.0;

tP.gridwidth = GridBagConstraints.REMAINDER; display = new TransPanel(); layOut.setConstraints(display, tP); display.setBackground(Color.white); getContentPane().add(display); validate(); } public void itemStateChanged(ItemEvent e){} public void actionPerformed(ActionEvent e) { display.setTrans(trans.getSelectedIndex()); display.renderShape(); } public static void main( String[] argv ) { if ( argv.length > 0 && argv[0].equals( "-no2d" ) ) { Transform.no2D = true; } JFrame frame = new JFrame( "Transform" ); frame.addWindowListener( new WindowAdapter(){ public void windowClosing( WindowEvent e ){ System.exit( 0 ); } }); JApplet applet = new Transform(); frame.getContentPane().add( BorderLayout.CENTER, applet ); applet.init(); frame.setSize( 550, 400 ); frame.setVisible(true); }}

class TransPanel extends JPanel { AffineTransform at = new AffineTransform(); int w, h; Shape shapes[] = new Shape[3]; BufferedImage bi; boolean firstTime = true; public TransPanel(){ setBackground(Color.white); shapes[0] = new Rectangle(0, 0, 100, 100); shapes[1] = new Ellipse2D.Double(0.0, 0.0, 100.0, 100.0); TextLayout textTl = new TextLayout("Text", new Font("Helvetica", 1, 96), new FontRenderContext(null, false, false));AffineTransform textAt = new AffineTransform();textAt.translate(0, (float)textTl.getBounds().getHeight()); shapes[2] = textTl.getOutline(textAt); } public void setTrans(int transIndex) { // Sets the AffineTransform. switch ( transIndex ) { case 0 : at.setToIdentity(); at.translate(w/2, h/2); break; case 1 : at.rotate(Math.toRadians(45)); break; case 2 : at.scale(0.5, 0.5); break; case 3 : at.shear(0.5, 0.0); break;case 4 : at.translate(0.5, 0.5); break;case 5 : at.scale(-1.0,1.0); break; } }

public void renderShape() { repaint(); } public void paintComponent(Graphics g) {super.paintComponent(g);if ( !Transform.no2D ) {Graphics2D g2 = (Graphics2D) g;Dimension d = getSize();w = d.width;h = d.height; // Prints out the intructions.

String instruct = "Pick a primitive, line style, paint, transform,"; TextLayout thisTl = new TextLayout(instruct, new Font("Helvetica", 0, 10), g2.getFontRenderContext()); float width = (float)thisTl.getBounds().getWidth();float height = (float)thisTl.getBounds().getHeight(); thisTl.draw(g2, w/2-width/2, 15);instruct = "and rendering method and click the Redraw button."; thisTl = new TextLayout(instruct, new Font("Helvetica", 0, 10), g2.getFontRenderContext()); width = (float)thisTl.getBounds().getWidth(); thisTl.draw(g2, w/2-width/2, height + 17);// Initialize the transform.if (firstTime) { at.setToIdentity(); at.translate(w/2, h/2); firstTime = false;} // Sets the Stroke.Stroke oldStroke = g2.getStroke();

switch ( Transform.line.getSelectedIndex() ) { case 0 : g2.setStroke(new BasicStroke(3.0f)); break; case 1 : g2.setStroke(new BasicStroke(8.0f)); break; case 2 : float dash[] = {10.0f}; g2.setStroke(new BasicStroke(3.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f)); break; } // Sets the Paint.Paint oldPaint = g2.getPaint(); switch ( Transform.paint.getSelectedIndex() ) { case 0 : g2.setPaint(Color.blue);break; case 1 : g2.setPaint(new GradientPaint(0, 0, Color.lightGray, w-250, h, Color.blue, false)); break; case 2 : BufferedImage buffi = new BufferedImage(15, 15, BufferedImage.TYPE_INT_RGB); Graphics2D buffig = buffi.createGraphics(); buffig.setColor(Color.blue); buffig.fillRect(0, 0, 15, 15); buffig.setColor(Color.lightGray); buffig.translate((15/2)-(5/2), (15/2)-(5/2)); buffig.fillOval(0, 0, 7, 7); Rectangle r = new Rectangle(0,0,25,25); g2.setPaint(new TexturePaint(buffi, r)); break; } // Sets the Shape. Shape shape = shapes[Transform.primitive.getSelectedIndex()]; Rectangle r = shape.getBounds();

// Sets the selected Shape to the center of the Canvas. AffineTransform saveXform = g2.getTransform();AffineTransform toCenterAt = new AffineTransform(); toCenterAt.concatenate(at); toCenterAt.translate(-(r.width/2), -(r.height/2)); g2.transform(toCenterAt); // Sets the rendering method. switch ( Transform.stroke.getSelectedIndex() ) { case 0 : g2.draw(shape); break; case 1 : g2.fill(shape); break; case 2 : Graphics2D tempg2 = g2; g2.fill(shape); g2.setColor(Color.darkGray); g2.draw(shape); g2.setPaint(tempg2.getPaint()); break; }g2.setStroke(oldStroke);g2.setPaint(oldPaint); g2.setTransform(saveXform);}}}

OUTPUT:

Rotate:

Scale:

Shear:

Translate:

Mirror Reflection:

RESULT:Thus the 2D transformation program for Translation, Scaling, Rotation, Mirror Reflection and Shearing with a menu driven program has been implemented successfully.

EX NO: IMPLEMENT VARIOUS IMAGE COMPRESSIONDATE: ALGORITHMS.

AIM

To create a program to implement an image compression algorithm.

PROCEDURE1. Start the program.2. Create a class named Huffman encoder and declare the variables.3. Create two file types namely in-file & out-file.4. The file name, file size, compressed size and ratio are displayed finally and traverse the tree.5. Create a class named Huffman decoder and declare the variables.6. Decode the values and create a priority queue; then a tree is created.7. The compressed size and size after decompression is displayed.8. Stop the program.

CODING

//Huffman Encoderpackage Algorithms.Huffman;import java.io.*;import javax.swing.*;public class HuffmanEncoder{private staticString code[],summary="";privateint totalBytes=0;private int count=0;private File inputFile;private File outputFile ;private FileOutputStream fos;private ObjectOutputStream oos;private BufferedOutputStream bos;private FileInputStream fis;private BufferedInputStream bis;private boolean done=false;public HuffmanEncoder(File inFile, File outFile){inputFile=inFile;outputFile=outFile;encode();}public void encode(){

int freq[]=new int[256];

for(int i=0;if2){HuffmanNode t = new HuffmanNode(null,(f1+f2),0,two,one,null);one.up=t;two.up=t;q.insertM(t);}else{HuffmanNode t=new HuffmanNode(null,(f1+f2),0,one,two,null);one.up=t;two.up=t;q.insertM(t);}}tree =q.removeFirst();}catch(Exception e){System.out.println("Priority Queue error");}code=new String[256];for(int i=0;i0){while(outbyte.length()=0;j--)code[n.getValue()]+=arr[j];}if(n.lchild!=null)traverse(n.lchild);if(n.rchild!=null)traverse(n.rchild);}private String toBinary(int b){ int arr[]=new int[8]; String s=""; for(int i=0;i=0;i--){ s+=arr[i]; } return s; }private int toInt(String b){ int output=0,wg=128; for(int i=0;i1){one=q.removeFirst();two=q.removeFirst();int f1=one.getFreq();int f2=two.getFreq();if (f1>f2){HuffmanNode t=new HuffmanNode(null,(f1+f2),0,two,one,null);one.up=t;two.up=t;q.insertM(t);}else{HuffmanNode t=new HuffmanNode(null,(f1+f2),0,one,two,null);one.up=t;two.up=t;q.insertM(t);}}tree =q.removeFirst();}catch(Exception exc){System.out.println("Priority queue exception");}String s="";

try{mycount=in.available();while (totalBytes32){for(int a=0;a0){if(s.length()>16)counter=16;else counter=s.length();for(int a=0;a16)&0xFF); }

public int getGreen( int p ) {return ((p>>8)&0xFF);} public int getBlue( int p ) {return (p & 0xff);}

public int createRGB(int r, int g, int b){ return new Color(r, g, b).getRGB(); }String seeColor(String c){String temp="";if (c.indexOf("-")!=-1 )temp="-";if((c.indexOf("R")>=0) || (c.indexOf("r")>=0) )temp+="charR";else if((c.indexOf("G")>=0) || (c.indexOf("g")>=0) )temp+="charG";else if((c.indexOf("B")>=0) || (c.indexOf("b")>=0) )temp+="charB";else {temp="num";}return temp;}int getNumber(int c,String ch,String number){int temp=0;if(ch.equals("num")){temp=Integer.parseInt(number);}else if(ch.indexOf("charR")>=0){temp=getRed(c);}else if(ch.indexOf("charG")>=0){temp=getGreen(c);

}else if(ch.indexOf("charB")>=0){temp=getBlue(c);}if(ch.indexOf("-")>=0)temp=255-temp;return temp;}BufferedImage chabgeColor(BufferedImage image,String parR,String parG,String parB){System.out.println("**R= "+parR+" **G= "+parG+" **B="+parB);int w=image.getWidth(this);int h=image.getHeight(this);int c,red,green,blue;String cR,cG,cB;BufferedImage newImage=new BufferedImage(w,h,1);cR=seeColor(parR);cG=seeColor(parG);cB=seeColor(parB);System.out.println("R= "+cR+" G= "+cG+" B="+cB);for(int y=0;y