Реализация стандартного шлюза DHCP
Материал из BiTel WiKi
(Различия между версиями)
Stark (Обсуждение | вклад) |
Stark (Обсуждение | вклад) (исправил несколько ошибок, не работало) |
||
Строка 10: | Строка 10: | ||
import java.util.Map.Entry; | import java.util.Map.Entry; | ||
import java.util.regex.Pattern; | import java.util.regex.Pattern; | ||
- | + | ||
import org.apache.xml.serializer.ToXMLStream; | import org.apache.xml.serializer.ToXMLStream; | ||
import org.xml.sax.SAXException; | import org.xml.sax.SAXException; | ||
- | + | ||
import ru.bitel.bgbilling.common.DefaultSetup; | import ru.bitel.bgbilling.common.DefaultSetup; | ||
import bitel.billing.common.module.ipn.IPNContractStatus; | import bitel.billing.common.module.ipn.IPNContractStatus; | ||
Строка 20: | Строка 20: | ||
import bitel.billing.server.ipn.dlink.UserPortStatus.UserPortStatusParser; | import bitel.billing.server.ipn.dlink.UserPortStatus.UserPortStatusParser; | ||
import bitel.billing.server.util.Utils; | import bitel.billing.server.util.Utils; | ||
- | + | import bitel.billing.server.ipn.GateWorker; | |
+ | |||
void parentSync( Gate child, GateWorker childWorker ) | void parentSync( Gate child, GateWorker childWorker ) | ||
{ | { | ||
- | log.debug( "parentSync" ); | + | Pattern patternTab = Pattern.compile( "\\t" ); |
- | + | log.debug( "parentSync" ); | |
+ | |||
String host = gate.getHost(); | String host = gate.getHost(); | ||
int port = gate.getPort(); | int port = gate.getPort(); | ||
int gid = gate.getId(); | int gid = gate.getId(); | ||
- | + | ||
if( Utils.isEmptyString( host ) || port <= 0 ) | if( Utils.isEmptyString( host ) || port <= 0 ) | ||
{ | { | ||
Строка 35: | Строка 37: | ||
return; | return; | ||
} | } | ||
- | + | ||
if( log.isDebugEnabled() ) | if( log.isDebugEnabled() ) | ||
log.debug( gid + " gate: " + host + ":" + port ); | log.debug( gid + " gate: " + host + ":" + port ); | ||
- | + | ||
- | + | ||
Socket socket = null; | Socket socket = null; | ||
try | try | ||
Строка 45: | Строка 47: | ||
//if( childWorker instanceof UserPortStatusParser ) | //if( childWorker instanceof UserPortStatusParser ) | ||
//{ | //{ | ||
- | List userPortStatusList = childWorker.getUserList( childWorker.statusList ); | + | //List userPortStatusList = childWorker.getUserList( childWorker.statusList ); |
- | + | List userPortStatusList = childWorker.getUserList( statusList ); | |
+ | |||
socket = new Socket( host, port ); | socket = new Socket( host, port ); | ||
InputStreamReader isr = new InputStreamReader( socket.getInputStream() ); | InputStreamReader isr = new InputStreamReader( socket.getInputStream() ); | ||
BufferedReader in = new BufferedReader( isr ); | BufferedReader in = new BufferedReader( isr ); | ||
- | + | ||
ToXMLStream stream = new ToXMLStream(); | ToXMLStream stream = new ToXMLStream(); | ||
stream.setOutputStream( socket.getOutputStream() ); | stream.setOutputStream( socket.getOutputStream() ); | ||
- | + | ||
stream.setEncoding( "UTF-8" ); | stream.setEncoding( "UTF-8" ); | ||
stream.startDocument(); | stream.startDocument(); | ||
- | + | ||
stream.startElement( "sync" ); | stream.startElement( "sync" ); | ||
stream.addAttribute( "type", "dhcp" ); | stream.addAttribute( "type", "dhcp" ); | ||
- | + | ||
Gate gate = child; | Gate gate = child; | ||
syncGate( gate, stream ); | syncGate( gate, stream ); | ||
- | + | ||
stream.startElement( "list" ); | stream.startElement( "list" ); | ||
stream.addAttribute( "gateId", String.valueOf( gate.getId() ) ); | stream.addAttribute( "gateId", String.valueOf( gate.getId() ) ); | ||
Строка 68: | Строка 71: | ||
stream.flushPending(); | stream.flushPending(); | ||
stream.getWriter().flush(); | stream.getWriter().flush(); | ||
- | + | ||
Map portStatusMap = new HashMap(); | Map portStatusMap = new HashMap(); | ||
String line = in.readLine(); | String line = in.readLine(); | ||
- | + | ||
while( line != null && !"".equals( line ) ) | while( line != null && !"".equals( line ) ) | ||
{ | { | ||
Строка 82: | Строка 85: | ||
portStatusMap.put( st.port, st ); | portStatusMap.put( st.port, st ); | ||
} | } | ||
- | + | ||
line = in.readLine(); | line = in.readLine(); | ||
} | } | ||
- | + | ||
String gateId = String.valueOf( gate.getId() ); | String gateId = String.valueOf( gate.getId() ); | ||
- | + | ||
for( UserPortStatus status : userPortStatusList ) | for( UserPortStatus status : userPortStatusList ) | ||
{ | { | ||
UserPortStatus s = portStatusMap.remove( status.port ); | UserPortStatus s = portStatusMap.remove( status.port ); | ||
- | + | ||
- | + | ||
- | + | ||
if( (s == null || !s.ipAddr.equals( status.ipAddr ) ) && | if( (s == null || !s.ipAddr.equals( status.ipAddr ) ) && | ||
status.status.status != IPNContractStatus.STATUS_REMOVED ) | status.status.status != IPNContractStatus.STATUS_REMOVED ) | ||
Строка 114: | Строка 117: | ||
stream.endElement( "portMac" ); | stream.endElement( "portMac" ); | ||
} | } | ||
- | + | ||
} | } | ||
- | + | ||
- | + | ||
//удаляем в одном месте | //удаляем в одном месте | ||
- | + | ||
if( s != null && status.status.status == IPNContractStatus.STATUS_REMOVED ) | if( s != null && status.status.status == IPNContractStatus.STATUS_REMOVED ) | ||
{ | { | ||
Строка 128: | Строка 131: | ||
stream.endElement( "removePort" ); | stream.endElement( "removePort" ); | ||
} | } | ||
- | + | ||
- | + | ||
- | + | ||
} | } | ||
- | + | ||
stream.flushPending(); | stream.flushPending(); | ||
stream.getWriter().flush(); | stream.getWriter().flush(); | ||
- | + | ||
stream.endElement( "sync" ); | stream.endElement( "sync" ); | ||
stream.endDocument(); | stream.endDocument(); | ||
Строка 141: | Строка 144: | ||
//} | //} | ||
} | } | ||
- | + | ||
finally | finally | ||
{ | { | ||
Строка 155: | Строка 158: | ||
} | } | ||
} | } | ||
- | + | ||
protected void syncGates( Gate gate, ToXMLStream stream ) | protected void syncGates( Gate gate, ToXMLStream stream ) | ||
{ | { | ||
Строка 174: | Строка 177: | ||
stream.endElement( "param" ); | stream.endElement( "param" ); | ||
} | } | ||
- | + | ||
if( gate.getChildren() != null ) | if( gate.getChildren() != null ) | ||
for( Gate g : gate.getChildren() ) | for( Gate g : gate.getChildren() ) | ||
Строка 180: | Строка 183: | ||
syncGates( g, stream ); | syncGates( g, stream ); | ||
} | } | ||
- | + | ||
stream.endElement( "gate" ); | stream.endElement( "gate" ); | ||
} | } | ||
Строка 189: | Строка 192: | ||
} | } | ||
} | } | ||
- | + | ||
protected void syncGate( Gate gate, ToXMLStream stream ) | protected void syncGate( Gate gate, ToXMLStream stream ) | ||
{ | { | ||
Строка 204: | Строка 207: | ||
g = g.getParent(); | g = g.getParent(); | ||
} | } | ||
- | + | ||
HashMap params = new HashMap(); | HashMap params = new HashMap(); | ||
for( int i = gateList.size() - 1; i >= 0; i-- ) | for( int i = gateList.size() - 1; i >= 0; i-- ) | ||
Строка 215: | Строка 218: | ||
} | } | ||
} | } | ||
- | + | ||
stream.startElement( "gate" ); | stream.startElement( "gate" ); | ||
stream.addAttribute( "id", String.valueOf( gate.getId() ) ); | stream.addAttribute( "id", String.valueOf( gate.getId() ) ); | ||
stream.addAttribute( "host", gate.getHost() ); | stream.addAttribute( "host", gate.getHost() ); | ||
- | + | ||
for( Entry e : params.entrySet() ) | for( Entry e : params.entrySet() ) | ||
{ | { | ||
Строка 227: | Строка 230: | ||
stream.endElement( "param" ); | stream.endElement( "param" ); | ||
} | } | ||
- | + | ||
stream.endElement( "gate" ); | stream.endElement( "gate" ); | ||
} | } | ||
Строка 236: | Строка 239: | ||
} | } | ||
} | } | ||
- | + | ||
protected List getGateListFromTree( Gate root ) | protected List getGateListFromTree( Gate root ) | ||
{ | { | ||
Строка 243: | Строка 246: | ||
return result; | return result; | ||
} | } | ||
- | + | ||
protected void getGateListFromTree( List list, Gate root ) | protected void getGateListFromTree( List list, Gate root ) | ||
{ | { | ||
if( root != null ) | if( root != null ) | ||
list.add( root ); | list.add( root ); | ||
- | + | ||
if( root.getChildren() != null ) | if( root.getChildren() != null ) | ||
for( Gate gate : root.getChildren() ) | for( Gate gate : root.getChildren() ) |
Текущая версия на 04:58, 29 февраля 2012
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; import bitel.billing.server.ipn.GateWorker; void parentSync( Gate child, GateWorker childWorker ) { Pattern patternTab = Pattern.compile( "\\t" ); 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 ); List userPortStatusList = childWorker.getUserList( 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 ); } }