OpenCores
URL https://opencores.org/ocsvn/fpga-cf/fpga-cf/trunk

Subversion Repositories fpga-cf

[/] [fpga-cf/] [trunk/] [java/] [src/] [edu/] [byu/] [cc/] [plieber/] [fpgaenet/] [fcp/] [FCPPacket.java] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 peteralieb
/**
2
 *
3
 */
4
package edu.byu.cc.plieber.fpgaenet.fcp;
5
 
6
import java.net.DatagramPacket;
7
import java.net.InetAddress;
8
 
9
/**
10
 * @author plieber
11
 *
12
 */
13
public class FCPPacket {
14
        public InetAddress source;
15
        public InetAddress dest;
16
        public int srcPort;
17
        public int dstPort;
18
        public int version;
19
        public int command;
20
        public int port;
21
        public int seq;
22
        public int len;
23
        public byte[] data;
24
 
25
        /**
26
         * Constructs an FCP packet from the data of a UDP datagram packet.
27
         * @param packet The UDP datagram packet containing exactly one FCP packet.
28
         */
29
        public FCPPacket(DatagramPacket packet) {
30
                source = packet.getAddress();
31
                srcPort = packet.getPort();
32
                version = (packet.getData()[0] & 0xf0) >> 4;
33
                command = packet.getData()[0] & 0x0f;
34
                port = packet.getData()[1] & 0xff;
35
                seq = ((packet.getData()[2] << 8) & 0xff00) + (packet.getData()[3] & 0xff);
36
                len = ((packet.getData()[4] << 8) & 0xff00) + (packet.getData()[5] & 0xff);
37
                data = new byte[len];
38
                for (int i = 0; i < len; i++) {
39
                        data[i] = packet.getData()[6 + i];
40
                }
41
        }
42
 
43
        public FCPPacket() {
44
                // TODO Auto-generated constructor stub
45
        }
46
 
47
        @Override
48
        public String toString() {
49
                String ret = "FCP IcapPacket { version=" + version + ", command=" + command
50
                                + ", port=" + port + ", seq=" + seq + ", len=" + len;
51
                if (this.len > 0 && data != null) {
52
                        ret += " <data: ";
53
                        for (int i=0; i<(len-1); i++) {
54
                                ret += String.format("%H", data[i] & 0xff) + ",";
55
                        }
56
                        ret += String.format("%H", data[len-1] & 0xff) + ">";
57
                }
58
                return ret + " }";
59
        }
60
 
61
        /**
62
         * Wraps this FCP packet into a datagram packet for sending over a UDP socket.
63
         * @param dest Destination internet address
64
         * @param port remote port number
65
         * @return
66
         */
67
        public DatagramPacket wrapInDatagram(InetAddress dest, int port) {
68
                byte[] buf = new byte[6 + (this.command != 4 ? len: 0)];
69
 
70
                buf[0] = (byte) (((version << 4) & 0xf0) | (command & 0x0f));
71
                buf[1] = (byte) this.port;
72
                buf[2] = (byte) ((seq >> 8) & 0xff);
73
                buf[3] = (byte) (seq & 0xff);
74
                buf[4] = (byte) ((len >> 8) & 0xff);
75
                buf[5] = (byte) (len & 0xff);
76
                if (this.command != 4) {
77
                        for (int i = 0; i < len; i++) {
78
                                buf[6 + i] = data[i];
79
                        }
80
                }
81
 
82
                DatagramPacket packet = new DatagramPacket(buf, buf.length);
83
                packet.setPort(port);
84
                packet.setAddress(dest);
85
                return packet;
86
        }
87
 
88
        /**
89
         * Wraps this FCP packet into a datagram packet for sending over a UDP socket.
90
         * @return
91
         */
92
        public DatagramPacket wrapInDatagram() {
93
                byte[] buf = new byte[6 + (this.command != 4 ? len: 0)];
94
 
95
                buf[0] = (byte) (((version << 4) & 0xf0) | (command & 0x0f));
96
                buf[1] = (byte) this.port;
97
                buf[2] = (byte) ((seq >> 8) & 0xff);
98
                buf[3] = (byte) (seq & 0xff);
99
                buf[4] = (byte) ((len >> 8) & 0xff);
100
                buf[5] = (byte) (len & 0xff);
101
                if (this.command != 4) {
102
                        for (int i = 0; i < len; i++) {
103
                                buf[6 + i] = data[i];
104
                        }
105
                }
106
 
107
                DatagramPacket packet = new DatagramPacket(buf, buf.length);
108
                packet.setPort(this.dstPort);
109
                packet.setAddress(this.dest);
110
                return packet;
111
        }
112
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.