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