src/com/shrewdify/diaroser/impl/PeerImpl.java
@Override
		public byte[] processCCRT(byte[] b) throws IOException, NoSpaceException,SQLException,ArithmeticException {
			log.debug("Processing CCRT Request");
			HashSet<Avp> avpset = null;
			byte temp[] = Arrays.copyOfRange(b, Constants.ZERO, Constants.TWENTY);
			Hashtable<Integer, Avp> hs = par.decodeAvpSet(b, Constants.TWENTY);
			int validity=par.checkValid(hs, uri);
			if (validity>Constants.ZERO) {
				int finalbal = Constants.ZERO;
				String sessionid = par.bytesToOctetString(hs.get(Dictionary.SESSION_ID).rawData)
						+ uri;
				Hashtable<String, String> params=null;
				synchronized(sessions){
					params= sessions.get(sessionid);
				};

				int wrresUnits = (int) par.bytesToInt((par.decodeAvpSet((par
						.decodeAvpSet(hs.get(Dictionary.MULTIPLE_SERVICES_CREDIT_CONTROL).getData(), 0)).get(Dictionary.USED_SERVICE_UNIT)
						.getData(), 0)).get(Dictionary.CC_TIME).getData());
				
				int balance = Integer.valueOf(params.get(Constants.REMAINING_BALANCE));
				int reservedUnit = Integer.valueOf(params.get(Constants.RESERVED_BALANCE));
				int getUnitPerPerios = Integer.valueOf(params
						.get(Constants.TIME_PERIOD));

				if (wrresUnits > Constants.ZERO) {
					int intialAmountBuff = Integer.valueOf(params
							.get(Constants.INITIAL_CHARGE));
					String respons[]=bill(balance, Integer.valueOf(params.get(Constants.CHARGE_PER_PERIOD)),getUnitPerPerios, Integer.parseInt(params
							.get(Constants.GRANTED_UNITS)), wrresUnits, params.get(Constants.SUBSCRIPTIONID), 1);
					if (params.get(Constants.MCC).equals(Config.unBillMcc)
							&& params.get(Constants.MNC).equals(
									Config.unBillMnc)) {
						balance = reservedUnit;
					} else {
						balance = Integer.valueOf(respons[1]) - intialAmountBuff;
						finalbal = reservedUnit - balance;
						//finalbal = Integer.valueOf(finalbal);
						
					}
				} else
					finalbal = reservedUnit;
				boolean result = db.updateUserBalance(params.get(Constants.SUBSCRIPTIONID), reservedUnit, finalbal);
				int resCode = Constants.SUCCESS_CODE;
				if (!result)
					resCode = Constants.UNKNOWN_ERROR_CODE;
				avpset = par.copyforResponse(Constants.ONE, resCode, hs);
				Avp a = new Avp(Dictionary.MULTIPLE_SERVICES_CREDIT_CONTROL, (short) 0, 0, par.encodeAvp(new Avp(Dictionary.GRANTED_SERVICE_UNITS,
						(short) 0, 0, par.encodeAvp(new Avp(Dictionary.CC_TIME, (short) 0, 0,
								par.int32ToBytes(-1))))));
				avpset.add(a);
				byte spack[] = par.merge(par.encodeAvpSet(avpset), temp);
				synchronized(sessions){
					sessions.remove(sessionid);
				};
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				HashMap<String,String> cdrData = new HashMap<String, String>();
				cdrData.put(Constants.SRC_USERNAME, params.get(Constants.ANUMBER));
				cdrData.put(Constants.SRC_HOSTNAME, "truphone");
				cdrData.put(Constants.BNUMBER, params.get(Constants.BNUMBER));
				cdrData.put(Constants.START_TIME, params.get(Constants.START_TIME));
				cdrData.put(Constants.END_TIME,sdf.format(new Date(System.currentTimeMillis())));
				cdrData.put(Constants.ACCOUNT,"abcdef");
				cdrData.put(Constants.AMOUNTCONSUMED,"" + finalbal);
				cdrData.put(Constants.RATECARD,"" + 1);
				cdrData.put(Constants.INITIAL_CHARGE,params.get(Constants.INITIAL_CHARGE));
				cdrData.put(Constants.CHARGEMODE,"" + 1);
//				/try{
				System.out.println(params.get(Constants.CHARGE_PER_PERIOD));
				long consumedtime  = (long)(Integer.valueOf(finalbal)/Integer.valueOf(params.get(Constants.CHARGE_PER_PERIOD))) * Long.valueOf(params.get(Constants.TIME_PERIOD));
				cdrData.put(Constants.TIMECONSUMED, ""+consumedtime);
				
				//cdrData.put(Constants.TIMECONSUMED, ""+consumedtime);
				boolean loc=false;
				try {
					loc=cDRQueue.getLock();
					while(!loc){
						loc=cDRQueue.getLock();
						delay(1);
					}
					if(loc)
						cDRQueue.add(cdrData);
				}
				catch(Exception e) {
					e.printStackTrace();
				}
				finally {
					if(loc){
						cDRQueue.lock.unlock();
						delay(1);
					}
				}
				
				return spack;
			}
			else
				return handleError(validity,hs,temp);
		}
/**
		 * It process the Terminate Requests , it extracts Sender No,Reserved Units,Charge Per Block, Time Per BLock from session and Used units from its bytes
		 * and updates balance corresponding to sender no and calculates response.
		 * Before returning the response it store all the information to session.		  
		 *  
		 */
It process the Terminate Requests , it extracts Sender No,Reserved Units,Charge Per Block, Time Per BLock from session and Used units from its bytes
and updates balance corresponding to sender no and calculates response.
Before returning the response it store all the information to session.
It process the Terminate Requests it extracts Sender No Reserved Units Charge Per Block Time Per BLock from session and Used units from its bytes and updates balance corresponding to sender no and calculates response . Before returning the response it store all the information to session .
@Override
		public byte[] processCCRT(byte[] b) throws IOException, NoSpaceException,SQLException,ArithmeticException {
			log.debug("Processing CCRT Request");
			HashSet<Avp> avpset = null;
			byte temp[] = Arrays.copyOfRange(b, Constants.ZERO, Constants.TWENTY);
			Hashtable<Integer, Avp> hs = par.decodeAvpSet(b, Constants.TWENTY);
			int validity=par.checkValid(hs, uri);
			if (validity>Constants.ZERO) {
				int finalbal = Constants.ZERO;
				String sessionid = par.bytesToOctetString(hs.get(Dictionary.SESSION_ID).rawData)
						+ uri;
				Hashtable<String, String> params=null;
				synchronized(sessions){
					params= sessions.get(sessionid);
				};
				int wrresUnits = (int) par.bytesToInt((par.decodeAvpSet((par
						.decodeAvpSet(hs.get(Dictionary.MULTIPLE_SERVICES_CREDIT_CONTROL).getData(), 0)).get(Dictionary.USED_SERVICE_UNIT)
						.getData(), 0)).get(Dictionary.CC_TIME).getData());
				int balance = Integer.valueOf(params.get(Constants.REMAINING_BALANCE));
				int reservedUnit = Integer.valueOf(params.get(Constants.RESERVED_BALANCE));
				int getUnitPerPerios = Integer.valueOf(params
						.get(Constants.TIME_PERIOD));
				if (wrresUnits > Constants.ZERO) {
					int intialAmountBuff = Integer.valueOf(params
							.get(Constants.INITIAL_CHARGE));
					String respons[]=bill(balance, Integer.valueOf(params.get(Constants.CHARGE_PER_PERIOD)),getUnitPerPerios, Integer.parseInt(params
							.get(Constants.GRANTED_UNITS)), wrresUnits, params.get(Constants.SUBSCRIPTIONID), 1);
					if (params.get(Constants.MCC).equals(Config.unBillMcc)
							&& params.get(Constants.MNC).equals(
									Config.unBillMnc)) {
						balance = reservedUnit;
					} else {
						balance = Integer.valueOf(respons[1]) - intialAmountBuff;
						finalbal = reservedUnit - balance;
					}
				} else
					finalbal = reservedUnit;
				boolean result = db.updateUserBalance(params.get(Constants.SUBSCRIPTIONID), reservedUnit, finalbal);
				int resCode = Constants.SUCCESS_CODE;
				if (!result)
					resCode = Constants.UNKNOWN_ERROR_CODE;
				avpset = par.copyforResponse(Constants.ONE, resCode, hs);
				Avp a = new Avp(Dictionary.MULTIPLE_SERVICES_CREDIT_CONTROL, (short) 0, 0, par.encodeAvp(new Avp(Dictionary.GRANTED_SERVICE_UNITS,
						(short) 0, 0, par.encodeAvp(new Avp(Dictionary.CC_TIME, (short) 0, 0,
								par.int32ToBytes(-1))))));
				avpset.add(a);
				byte spack[] = par.merge(par.encodeAvpSet(avpset), temp);
				synchronized(sessions){
					sessions.remove(sessionid);
				};
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				HashMap<String,String> cdrData = new HashMap<String, String>();
				cdrData.put(Constants.SRC_USERNAME, params.get(Constants.ANUMBER));
				cdrData.put(Constants.SRC_HOSTNAME, "truphone");
				cdrData.put(Constants.BNUMBER, params.get(Constants.BNUMBER));
				cdrData.put(Constants.START_TIME, params.get(Constants.START_TIME));
				cdrData.put(Constants.END_TIME,sdf.format(new Date(System.currentTimeMillis())));
				cdrData.put(Constants.ACCOUNT,"abcdef");
				cdrData.put(Constants.AMOUNTCONSUMED,"" + finalbal);
				cdrData.put(Constants.RATECARD,"" + 1);
				cdrData.put(Constants.INITIAL_CHARGE,params.get(Constants.INITIAL_CHARGE));
				cdrData.put(Constants.CHARGEMODE,"" + 1);
				System.out.println(params.get(Constants.CHARGE_PER_PERIOD));
				long consumedtime  = (long)(Integer.valueOf(finalbal)/Integer.valueOf(params.get(Constants.CHARGE_PER_PERIOD))) * Long.valueOf(params.get(Constants.TIME_PERIOD));
				cdrData.put(Constants.TIMECONSUMED, ""+consumedtime);
				boolean loc=false;
				try {
					loc=cDRQueue.getLock();
					while(!loc){
						loc=cDRQueue.getLock();
						delay(1);
					}
					if(loc)
						cDRQueue.add(cdrData);
				}
				catch(Exception e) {
					e.printStackTrace();
				}
				finally {
					if(loc){
						cDRQueue.lock.unlock();
						delay(1);
					}
				}
				return spack;
			}
			else
				return handleError(validity,hs,temp);
		}
@ Override public byte [ ] processCCRT ( byte [ ] b ) throws IOException , NoSpaceException , SQLException , ArithmeticException { log . debug ( "Processing CCRT Request" ) ; HashSet < Avp > avpset = null ; byte temp [ ] = Arrays . copyOfRange ( b , Constants . ZERO , Constants . TWENTY ) ; Hashtable < Integer , Avp > hs = par . decodeAvpSet ( b , Constants . TWENTY ) ; int validity = par . checkValid ( hs , uri ) ; if ( validity > Constants . ZERO ) { int finalbal = Constants . ZERO ; String sessionid = par . bytesToOctetString ( hs . get ( Dictionary . SESSION_ID ) . rawData ) + uri ; Hashtable < String , String > params = null ; synchronized ( sessions ) { params = sessions . get ( sessionid ) ; } ; int wrresUnits = ( int ) par . bytesToInt ( ( par . decodeAvpSet ( ( par . decodeAvpSet ( hs . get ( Dictionary . MULTIPLE_SERVICES_CREDIT_CONTROL ) . getData ( ) , 0 ) ) . get ( Dictionary . USED_SERVICE_UNIT ) . getData ( ) , 0 ) ) . get ( Dictionary . CC_TIME ) . getData ( ) ) ; int balance = Integer . valueOf ( params . get ( Constants . REMAINING_BALANCE ) ) ; int reservedUnit = Integer . valueOf ( params . get ( Constants . RESERVED_BALANCE ) ) ; int getUnitPerPerios = Integer . valueOf ( params . get ( Constants . TIME_PERIOD ) ) ; if ( wrresUnits > Constants . ZERO ) { int intialAmountBuff = Integer . valueOf ( params . get ( Constants . INITIAL_CHARGE ) ) ; String respons [ ] = bill ( balance , Integer . valueOf ( params . get ( Constants . CHARGE_PER_PERIOD ) ) , getUnitPerPerios , Integer . parseInt ( params . get ( Constants . GRANTED_UNITS ) ) , wrresUnits , params . get ( Constants . SUBSCRIPTIONID ) , 1 ) ; if ( params . get ( Constants . MCC ) . equals ( Config . unBillMcc ) && params . get ( Constants . MNC ) . equals ( Config . unBillMnc ) ) { balance = reservedUnit ; } else { balance = Integer . valueOf ( respons [ 1 ] ) - intialAmountBuff ; finalbal = reservedUnit - balance ; } } else finalbal = reservedUnit ; boolean result = db . updateUserBalance ( params . get ( Constants . SUBSCRIPTIONID ) , reservedUnit , finalbal ) ; int resCode = Constants . SUCCESS_CODE ; if ( ! result ) resCode = Constants . UNKNOWN_ERROR_CODE ; avpset = par . copyforResponse ( Constants . ONE , resCode , hs ) ; Avp a = new Avp ( Dictionary . MULTIPLE_SERVICES_CREDIT_CONTROL , ( short ) 0 , 0 , par . encodeAvp ( new Avp ( Dictionary . GRANTED_SERVICE_UNITS , ( short ) 0 , 0 , par . encodeAvp ( new Avp ( Dictionary . CC_TIME , ( short ) 0 , 0 , par . int32ToBytes ( - 1 ) ) ) ) ) ) ; avpset . add ( a ) ; byte spack [ ] = par . merge ( par . encodeAvpSet ( avpset ) , temp ) ; synchronized ( sessions ) { sessions . remove ( sessionid ) ; } ; SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ; HashMap < String , String > cdrData = new HashMap < String , String > ( ) ; cdrData . put ( Constants . SRC_USERNAME , params . get ( Constants . ANUMBER ) ) ; cdrData . put ( Constants . SRC_HOSTNAME , "truphone" ) ; cdrData . put ( Constants . BNUMBER , params . get ( Constants . BNUMBER ) ) ; cdrData . put ( Constants . START_TIME , params . get ( Constants . START_TIME ) ) ; cdrData . put ( Constants . END_TIME , sdf . format ( new Date ( System . currentTimeMillis ( ) ) ) ) ; cdrData . put ( Constants . ACCOUNT , "abcdef" ) ; cdrData . put ( Constants . AMOUNTCONSUMED , "" + finalbal ) ; cdrData . put ( Constants . RATECARD , "" + 1 ) ; cdrData . put ( Constants . INITIAL_CHARGE , params . get ( Constants . INITIAL_CHARGE ) ) ; cdrData . put ( Constants . CHARGEMODE , "" + 1 ) ; System . out . println ( params . get ( Constants . CHARGE_PER_PERIOD ) ) ; long consumedtime = ( long ) ( Integer . valueOf ( finalbal ) / Integer . valueOf ( params . get ( Constants . CHARGE_PER_PERIOD ) ) ) * Long . valueOf ( params . get ( Constants . TIME_PERIOD ) ) ; cdrData . put ( Constants . TIMECONSUMED , "" + consumedtime ) ; boolean loc = false ; try { loc = cDRQueue . getLock ( ) ; while ( ! loc ) { loc = cDRQueue . getLock ( ) ; delay ( 1 ) ; } if ( loc ) cDRQueue . add ( cdrData ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } finally { if ( loc ) { cDRQueue . lock . unlock ( ) ; delay ( 1 ) ; } } return spack ; } else return handleError ( validity , hs , temp ) ; }
It process the Terminate Requests , it extracts Sender No,Reserved Units,Charge Per Block, Time Per BLock from session and Used units from its bytes
 and updates balance corresponding to sender no and calculates response.
It process the Terminate Requests it extracts Sender No Reserved Units Charge Per Block Time Per BLock from session and Used units from its bytes and updates balance corresponding to sender no and calculates response .
//finalbal = Integer.valueOf(finalbal); //				/try{ //cdrData.put(Constants.TIMECONSUMED, ""+consumedtime);