Реализация стандартного шлюза DHCP
Материал из BiTel WiKi
(Новая: import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.Socket; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map...)
Следующая правка →
Версия 06:49, 21 августа 2009
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.Socket; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.regex.Pattern;
import org.apache.xml.serializer.ToXMLStream; import org.xml.sax.SAXException;
import ru.bitel.bgbilling.common.DefaultSetup; import bitel.billing.common.module.ipn.IPNContractStatus; import bitel.billing.server.ipn.bean.Gate; import bitel.billing.server.ipn.dlink.UserPortStatus; import bitel.billing.server.ipn.dlink.UserPortStatus.UserPortStatusParser; import bitel.billing.server.util.Utils;
void parentSync( Gate child, GateWorker childWorker ) { log.debug( "parentSync" );
String host = gate.getHost(); int port = gate.getPort(); int gid = gate.getId();
if( Utils.isEmptyString( host ) || port <= 0 ) { log.error( "Can't get IP/port for BGDHCP for this gate! " ); gateErrors.append( "Не указан адрес сервера dhcp для данного шлюза!" ); return; }
if( log.isDebugEnabled() ) log.debug( gid + " gate: " + host + ":" + port );
Socket socket = null; try { //if( childWorker instanceof UserPortStatusParser ) //{ List userPortStatusList = childWorker.getUserList( childWorker.statusList );
socket = new Socket( host, port ); InputStreamReader isr = new InputStreamReader( socket.getInputStream() ); BufferedReader in = new BufferedReader( isr );
ToXMLStream stream = new ToXMLStream(); stream.setOutputStream( socket.getOutputStream() );
stream.setEncoding( "UTF-8" ); stream.startDocument();
stream.startElement( "sync" ); stream.addAttribute( "type", "dhcp" );
Gate gate = child; syncGate( gate, stream );
stream.startElement( "list" ); stream.addAttribute( "gateId", String.valueOf( gate.getId() ) ); stream.endElement( "list" ); stream.flushPending(); stream.getWriter().flush();
Map portStatusMap = new HashMap(); String line = in.readLine();
while( line != null && !"".equals( line ) ) { String[] p = patternTab.split( line ); if( p.length > 1 ) { UserPortStatus st = new UserPortStatus(); st.ipAddr = p[1]; st.port = Utils.parseIntString( p[0], -1 ); portStatusMap.put( st.port, st ); }
line = in.readLine(); } String gateId = String.valueOf( gate.getId() );
for( UserPortStatus status : userPortStatusList ) { UserPortStatus s = portStatusMap.remove( status.port ); if( (s == null || !s.ipAddr.equals( status.ipAddr ) ) && status.status.status != IPNContractStatus.STATUS_REMOVED ) { if ( status.macAddr == null) {
stream.startElement( "port" ); stream.addAttribute( "gateId", gateId ); stream.addAttribute( "id", String.valueOf( status.port ) ); stream.addAttribute( "ip", status.ipAddr ); stream.endElement( "port" );
} else {
stream.startElement( "portMac" ); stream.addAttribute( "gateId", gateId ); stream.addAttribute( "id", String.valueOf( status.port ) ); stream.addAttribute( "ip", status.ipAddr ); stream.addAttribute( "mac", status.macAddr ); stream.endElement( "portMac" );
} }
//удаляем в одном месте if( s != null && status.status.status == IPNContractStatus.STATUS_REMOVED ) { stream.startElement( "removePort" ); stream.addAttribute( "gateId", gateId ); stream.addAttribute( "id", String.valueOf( status.port ) ); stream.addAttribute( "ip", status.ipAddr ); stream.endElement( "removePort" ); } } stream.flushPending(); stream.getWriter().flush();
stream.endElement( "sync" ); stream.endDocument(); stream.getWriter().flush(); //} } finally { if( socket != null ) { try { socket.close(); } catch (Exception e) {} } } }
protected void syncGates( Gate gate, ToXMLStream stream ) { try { if( gate != null ) { stream.startElement( "gate" ); stream.addAttribute( "id", String.valueOf( gate.getId() ) ); stream.addAttribute( "host", gate.getHost() ); DefaultSetup gateSetup = new DefaultSetup( gate.getConfig(), "\n" ); Map params = gateSetup.getHashValuesWithPrefix( "dhcp." ); for( Entry e : params.entrySet() ) { stream.startElement( "param" ); stream.addAttribute( "id", "dhcp." + e.getKey() ); stream.addAttribute( "value", e.getValue() ); stream.endElement( "param" ); }
if( gate.getChildren() != null ) for( Gate g : gate.getChildren() ) { syncGates( g, stream ); }
stream.endElement( "gate" ); } } catch( SAXException e ) { e.printStackTrace(); } }
protected void syncGate( Gate gate, ToXMLStream stream ) { try { if( gate != null ) { List gateList = new ArrayList(); gateList.add( gate ); Gate g = gate; while( g != null ) { gateList.add( g ); g = g.getParent(); }
HashMap params = new HashMap(); for( int i = gateList.size() - 1; i >= 0; i-- ) { DefaultSetup gateSetup = new DefaultSetup( gateList.get( i ).getConfig(), "\n" ); Map p = gateSetup.getHashValuesWithPrefix( "dhcp." ); for( Entry e : p.entrySet() ) { params.put( e.getKey(), e.getValue() ); } }
stream.startElement( "gate" ); stream.addAttribute( "id", String.valueOf( gate.getId() ) ); stream.addAttribute( "host", gate.getHost() );
for( Entry e : params.entrySet() ) { stream.startElement( "param" ); stream.addAttribute( "id", "dhcp." + e.getKey() ); stream.addAttribute( "value", e.getValue() ); stream.endElement( "param" ); }
stream.endElement( "gate" ); } } catch( SAXException e ) { e.printStackTrace(); } }
protected List getGateListFromTree( Gate root ) { List result = new ArrayList(); getGateListFromTree( result, root ); return result; }
protected void getGateListFromTree( List list, Gate root ) { if( root != null ) list.add( root );
if( root.getChildren() != null ) for( Gate gate : root.getChildren() ) { getGateListFromTree( list, gate ); } }