Реализация стандартного шлюза Switch

Материал из BiTel WiKi

Перейти к: навигация, поиск
import uk.co.westhawk.snmp.pdu.*;
import uk.co.westhawk.snmp.stack.*;
import ru.bitel.common.*;
import bitel.billing.server.util.*;
import bitel.billing.server.ipn.*;
import bitel.billing.common.module.ipn.*;
 
   String PDU_PREFIX = "1.3.6.1.2.1.2.2.1.7";	
   int requestCount = 0;	
	SnmpContext context = null;
 
    protected void doSync()
	{
		String host = gate.getHost();
		int port = gate.getPort();
		int gid = gate.getId();
 
        if( log.isDebugEnabled() )
        {
            log.debug( gid + " gate: " + host + ":" + port + " commutinty " + gate.getKeyword() );
        }
 
        try
		{
            String snmpVersion = gate.getGateType().getConfigOption( "snmp.version" );
			if( Utils.isBlankString( snmpVersion ) )
			{
				snmpVersion = "1";
			}
 
            if( log.isDebugEnabled() )
            {
                log.debug( "SNMP version: " + snmpVersion );
            }
 
            if( !snmpVersion.equals( "0" ) )
            {
                if ( snmpVersion.equals( "2" ) || snmpVersion.equals( "2c" ) )
                {
                    context = new SnmpContextv2c( host, port );
                }
                else 
                {
                    context = new SnmpContext( host, port );
                }
 
                context.setCommunity( gate.getKeyword() );
                SnmpUtils.setBufferSizes( context, 2, 2 );
 
                OneSetPdu setPdu = new OneSetPdu( context );
                setPdu.addObserver( this );
 
                for( UserStatus status : statusList )
                {
                    String ports = status.rule.getRuleText();
                    StringTokenizer st = new StringTokenizer( ports, "," );
 
                    while( st.hasMoreTokens() )
                    {
                        int ifNumber = Utils.parseIntString( st.nextToken().trim(), -1 );					
                        if( ifNumber >= 0 )
                        {
                            int value = 2;						
                            if( status.status == IPNContractStatus.STATUS_OPEN )
                            {
                                value = 1;
                            }
                            else
                            {
                                value = 2;
                            }
 
                            setPdu.addOid( PDU_PREFIX + "." + ifNumber, new AsnInteger( value ) );
 
                            if( log.isDebugEnabled() )
                            {
                                log.debug( "Add oid " + host + " " + PDU_PREFIX + "." + ifNumber + " i " + value );
                            }
                        }
                    }
                }
 
                setPdu.send();
                requestCount++;
                waitResponse();
            }
 
		}
		catch ( Exception e )
		{
			throw new RuntimeException ( e );
		}
        finally
        {
            if( context != null )
            {
                context.destroy();
            }
        }
	}
 
	protected void waitResponse()
	    throws InterruptedException
    {
	    while( requestCount != 0 )
	    {
	        Thread.sleep( 100 );
	    }
    }
 
	public void update( Observable o, Object arg )
	{
		requestCount--;		
 
		if( arg instanceof AgentException )
		{
			log.error( arg );			
			gateErrors.append( 
					"Ошибка шлюза " + gate.getHost() + 
					" : " + ((AgentException)arg).getMessage() );
 
            //interrupt();
		}	
 
	}
 
    public boolean isWorking()
	{
		return isAlive() || requestCount != 0;
	}
Личные инструменты