source: ResearchApps/PHY/WARPLAB/WARPLab7/M_Code_Reference/classes/wl_transport_eth_udp_java_bcast.m

Last change on this file was 4332, checked in by welsh, 9 years ago

Code cleanup.

File size: 6.8 KB
Line 
1%-------------------------------------------------------------------------
2% WARPLab Framework
3%
4% Copyright 2013, Mango Communications. All rights reserved.
5%           Distributed under the WARP license  (http://warpproject.org/license)
6%
7% Chris Hunter (chunter [at] mangocomm.com)
8% Patrick Murphy (murphpo [at] mangocomm.com)
9% Erik Welsh (welsh [at] mangocomm.com)
10%-------------------------------------------------------------------------
11
12classdef wl_transport_eth_udp_java_bcast < wl_transport & handle_light
13% Java physical layer Ethernet UDP Transport for broadcast traffic
14% User code should not use this object directly-- the parent wl_node will
15% instantiate the appropriate transport object for the hardware in use
16
17%******************************** Properties **********************************
18
19    properties (SetAccess = protected, Hidden = true)
20        sock;
21        status;
22        isopen;
23        maxPayload;
24    end
25
26    properties (SetAccess = public)
27        hdr;
28        address;
29        port;       
30        rxBufferSize;
31    end
32
33%********************************* Methods ************************************
34
35    methods
36        function obj = wl_transport_eth_udp_java_bcast()
37            obj.hdr = wl_transport_header;
38
39            % At the moment, a trigger is the only type of broadcast packet.
40            % In a future release this type will be exposed to objects that
41            % create the broadcast transport object.
42            obj.hdr.pktType = obj.hdr.PKTTYPE_TRIGGER;
43            obj.checkSetup();
44            obj.status = 0;
45
46            configFile = which('wl_config.ini');
47           
48            if(isempty(configFile))
49                error('cannot find wl_config.ini. please run wl_setup.m'); 
50            end
51           
52            readKeys = {'network', '', 'host_address', ''};
53            IP = inifile(configFile,'read',readKeys);
54            IP = IP{1};
55            IP = sscanf(IP,'%d.%d.%d.%d');
56
57            readKeys = {'network', '', 'host_ID', []};
58            hostID = inifile(configFile,'read',readKeys);
59            hostID = hostID{1};
60            hostID = sscanf(hostID,'%d');
61           
62            readKeys = {'network', '', 'bcast_port', []};
63            bcastport = inifile(configFile,'read',readKeys);
64            bcastport = bcastport{1};
65            bcastport = sscanf(bcastport,'%d');
66           
67            obj.address    = sprintf('%d.%d.%d.%d',IP(1),IP(2),IP(3),255);
68            obj.port       = bcastport;
69            obj.hdr.srcID  = hostID;
70            obj.hdr.destID = 65535;    % Changed from 255 in WARPLab 7.1.0           
71            obj.maxPayload = 1000;     % Default value;  Can explicitly set a different maxPayload if
72                                       % you are certain that all nodes support a larger packet size.
73        end
74
75        function checkSetup(obj)
76            % Currently not implemented
77        end
78
79        function setMaxPayload(obj,value)
80            obj.maxPayload = value;
81        end
82       
83        function out = getMaxPayload(obj)
84            out = double(obj.maxPayload);
85        end
86
87        function open(obj,varargin)
88            % varargin{1}: (optional) IP address
89            % varargin{2}: (optional) port
90            %
91            REQUESTED_BUF_SIZE = 2^22;
92               
93            import java.io.*
94            import java.net.DatagramSocket
95            import java.net.DatagramPacket
96            import java.net.InetAddress           
97
98            if(isempty(obj.isopen))
99                if(nargin==3)
100                   if(ischar(varargin{1}))
101                      obj.address = varargin{1}; 
102                   else
103                      obj.address = obj.int2IP(varargin{1});
104                   end
105                   obj.port = varargin{2};
106                end
107               
108                obj.sock = DatagramSocket();
109               
110                obj.sock.setSoTimeout(2000);
111                obj.sock.setReuseAddress(1);
112                obj.sock.setBroadcast(true);
113                obj.sock.setSendBufferSize(REQUESTED_BUF_SIZE);
114                obj.sock.setReceiveBufferSize(REQUESTED_BUF_SIZE); 
115               
116                x = obj.sock.getReceiveBufferSize();
117               
118                if(x < REQUESTED_BUF_SIZE)
119                    fprintf('OS reduced recv buffer size to %d\n', x);
120                end
121
122                obj.rxBufferSize = x;
123                obj.status       = 1;
124                obj.isopen       = 1;             
125            end
126        end
127
128        function close(obj)
129            try
130                obj.sock.close();
131            catch closeError
132                warning( 'Error closing socket; java error was %s', closeError.message)
133            end
134           
135            obj.status=0;
136        end   
137
138        function delete(obj)
139            obj.close();
140        end
141
142        function flush(obj)
143            % Currently not implemented
144        end
145    end %methods
146
147    methods (Hidden = true)
148        function send(obj, type, varargin)
149            % varargin{1}: data   
150
151            import java.io.*
152            import java.net.DatagramSocket
153            import java.net.DatagramPacket
154            import java.net.InetAddress           
155
156            switch(lower(type))
157                case 'trigger'
158                    bitset(obj.hdr.flags,1,0); % no response
159                    obj.hdr.pktType = obj.hdr.PKTTYPE_TRIGGER;
160                case 'message'
161                    obj.hdr.pktType = obj.hdr.PKTTYPE_HTON_MSG;
162            end
163           
164            data = uint32(varargin{1});
165           
166            obj.hdr.msgLength = (length(data))*4; %Length in bytes
167            obj.hdr.flags     = bitset(obj.hdr.flags,1,0);
168            obj.hdr.increment;
169           
170            data  = [obj.hdr.serialize,data];   
171            data8 = [zeros(1,2,'uint8') typecast(swapbytes(uint32(data)),'uint8')];
172           
173            jaddr = InetAddress.getByName(obj.address);
174           
175            pkt_send = DatagramPacket(data8, length(data8), jaddr, obj.port);
176            obj.sock.send(pkt_send);
177        end
178       
179        function dottedIPout = int2IP(obj,intIn)
180            addrChars(4) = mod(intIn, 2^8);
181            addrChars(3) = mod(bitshift(intIn, -8), 2^8);
182            addrChars(2) = mod(bitshift(intIn, -16), 2^8);
183            addrChars(1) = mod(bitshift(intIn, -24), 2^8);
184            dottedIPout  = sprintf('%d.%d.%d.%d', addrChars);
185        end
186
187        function intOut = IP2int(obj,dottedIP)
188            addrChars = sscanf(dottedIP, '%d.%d.%d.%d')';
189            intOut    = 2^0 * addrChars(4) + 2^8 * addrChars(3) + 2^16 * addrChars(2) + 2^24 * addrChars(1);
190        end
191    end
192end % classdef
Note: See TracBrowser for help on using the repository browser.