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/] [FCPSendThread.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.io.IOException;
7
import java.net.DatagramPacket;
8
import java.util.LinkedList;
9
import java.util.concurrent.LinkedBlockingQueue;
10
 
11
;
12
 
13
/**
14
 * @author Peter Lieber
15
 *
16
 */
17
class FCPSendThread extends Thread {
18
        private FCPProtocol protocol;
19
        protected LinkedBlockingQueue<FCPPacket> sendQueue;
20
        volatile protected boolean done = false;
21
        volatile protected boolean sendTimeout = false;
22
        volatile protected boolean ioException = false;
23
        protected long whileCount = 0;
24
 
25
        public FCPSendThread(FCPProtocol protocol) {
26
                this.protocol = protocol;
27
                this.sendQueue = new LinkedBlockingQueue<FCPPacket>();
28
        }
29
 
30
        @Override
31
        public void run() {
32
                while (true) {
33
                        try {
34
                                if (done == true)
35
                                        return;
36
                                while (sendQueue.isEmpty()) {
37
                                        if (done == true)
38
                                                return;
39
                                }
40
                                FCPPacket fcppacket = sendQueue.take();
41
                                if (fcppacket.command == 0 || fcppacket.command == 4) {
42
                                        fcppacket.seq = ++protocol.snd_cur;
43
                                        if (fcppacket.seq > 65000) {
44
                                                this.reconnect();
45
                                                fcppacket.seq = ++protocol.snd_cur;
46
                                        }
47
                                }
48
                                else
49
                                        fcppacket.seq = 0;
50
 
51
                                //System.out.println("Sending Packet: " + fcppacket.toString());
52
                                DatagramPacket packet = fcppacket.wrapInDatagram(fcppacket.dest, fcppacket.dstPort);
53
 
54
                                // parse
55
                                if ( true ) {//protocol.sendWindow <= 20) {
56
                                        if ((fcppacket.command == 0 || fcppacket.command == 4 || fcppacket.command == 2)
57
                                                        && fcppacket.seq > protocol.snd_last_ack + protocol.sendWindow) {
58
                                                int numResendsLeft = 5;
59
                                                long timeouttime = System.currentTimeMillis() + protocol.timeout;
60
                                                long newWhileCnt = whileCount + 1;
61
                                                while ((fcppacket.command == 0 || fcppacket.command == 4)
62
                                                                && fcppacket.seq > protocol.snd_last_ack + protocol.sendWindow) {
63
                                                        whileCount = newWhileCnt;
64
                                                        //System.out.println("Waiting");
65
                                                        if (System.currentTimeMillis() > timeouttime) {
66
                                                                if (numResendsLeft <= 0)
67
                                                                        throw new FCPException("Communication Error: Resend Limit Reached");
68
                                                                numResendsLeft--;
69
                                                                protocol.socket.send(protocol.packetOutbox.get(protocol.snd_last_ack + 1).wrapInDatagram());
70
                                                                System.out.println("Resent: " + (protocol.snd_last_ack + 1));
71
                                                                timeouttime = System.currentTimeMillis() + protocol.timeout;
72
                                                        }
73
                                                        if (done == true)
74
                                                                return;
75
                                                }
76
                                        }
77
                                }
78
 
79
                                if (fcppacket.command == 0 || fcppacket.command == 4)
80
                                        protocol.packetOutbox.put(fcppacket.seq, fcppacket);
81
                                protocol.socket.send(packet);
82
                        } catch (IOException e) {
83
                                this.ioException = true;
84
                                return;
85
                        } catch (FCPException e) {
86
                                this.sendTimeout = true;
87
                                return;
88
                        } catch (InterruptedException e) {
89
 
90
                        }
91
                }
92
        }
93
 
94
        private void reconnect() {
95
                FCPPacket fp = new FCPPacket();
96
                fp.version = 0;
97
                fp.command = 2;
98
                fp.port = 0;
99
                fp.seq = 0;
100
                fp.len = 0;
101
                fp.dest = protocol.connectedAddress;
102
                fp.dstPort = protocol.connectedPort;
103
                try {
104
                        protocol.connected = false;
105
                        protocol.packetOutbox.clear();
106
                        protocol.socket.send(fp.wrapInDatagram());
107
                } catch (IOException e) {
108
                        // TODO Auto-generated catch block
109
                        e.printStackTrace();
110
                }
111
                while (!protocol.connected);
112
        }
113
}

powered by: WebSVN 2.1.0

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