PageRenderTime 19ms CodeModel.GetById 10ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/smpp/src/main/java/org/mobicents/protocols/smpp/ReceiverThread.java

http://mobicents.googlecode.com/
Java | 167 lines | 118 code | 23 blank | 26 comment | 12 complexity | 30131a98cd03ba66ac275240b173fa4d MD5 | raw file
  1/*
  2 * JBoss, Home of Professional Open Source
  3 * Copyright 2011, Red Hat, Inc. and individual contributors
  4 * by the @authors tag. See the copyright.txt in the distribution for a
  5 * full listing of individual contributors.
  6 *
  7 * This is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU Lesser General Public License as
  9 * published by the Free Software Foundation; either version 2.1 of
 10 * the License, or (at your option) any later version.
 11 *
 12 * This software is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15 * Lesser General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU Lesser General Public
 18 * License along with this software; if not, write to the Free
 19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21 */
 22
 23package org.mobicents.protocols.smpp;
 24
 25import java.io.IOException;
 26
 27import org.slf4j.Logger;
 28import org.slf4j.LoggerFactory;
 29
 30import org.mobicents.protocols.smpp.event.ReceiverExceptionEvent;
 31import org.mobicents.protocols.smpp.event.ReceiverExitEvent;
 32import org.mobicents.protocols.smpp.event.SMPPEvent;
 33import org.mobicents.protocols.smpp.message.SMPPPacket;
 34import org.mobicents.protocols.smpp.net.ReadTimeoutException;
 35import org.mobicents.protocols.smpp.util.APIConfig;
 36import org.mobicents.protocols.smpp.util.APIConfigFactory;
 37import org.mobicents.protocols.smpp.util.PacketFactory;
 38
 39/**
 40 * Receiver thread for the connection.
 41 * @author amit bhayani
 42 * @version $Id: ReceiverThread.java 456 2009-01-15 17:15:38Z orank $
 43 */
 44public class ReceiverThread implements Receiver, Runnable {
 45    private static final Logger LOG = LoggerFactory.getLogger(ReceiverThread.class);
 46
 47    private Thread thread;
 48    private boolean running;
 49    private Session session;
 50    private PacketFactory packetFactory = new PacketFactory();
 51
 52    public ReceiverThread() {
 53        thread = new Thread(this);
 54        thread.setDaemon(true);
 55    }
 56    
 57    public ReceiverThread(Session session) {
 58        this();
 59        this.session = session;
 60    }
 61    
 62    public PacketFactory getPacketFactory() {
 63        return packetFactory;
 64    }
 65
 66    public void setPacketFactory(PacketFactory packetFactory) {
 67        this.packetFactory = packetFactory;
 68    }
 69
 70    public Session getSession() {
 71        return session;
 72    }
 73    
 74    public void setSession(Session session) {
 75        this.session = session;
 76    }
 77    
 78    public String getName() {
 79        return thread.getName();
 80    }
 81    
 82    public void setName(String name) {
 83        thread.setName(name);
 84    }
 85    
 86    public void run() {
 87        LOG.debug("Receiver thread starting.");
 88        SMPPEvent exitEvent = null;
 89        try {
 90            running = true;
 91            exitEvent = processPackets();
 92        } catch (Exception x) {
 93        	LOG.error("Error in receiver thread", x);
 94            exitEvent = new ReceiverExitEvent(session, x);
 95        }
 96        session.getEventDispatcher().notifyObservers(session, exitEvent);
 97        LOG.debug("Destroying event dispatcher.");
 98        session.getEventDispatcher().destroy();
 99        LOG.debug("Receiver thread exiting.");
100    }
101
102    public boolean isStarted() {
103        return thread.isAlive();
104    }
105
106    public void start() {
107        thread.start();
108    }
109    
110    public void stop() {
111        running = false;
112    }
113    
114    private ReceiverExitEvent processPackets() throws Exception {
115        ReceiverExitEvent exitEvent = null;
116        ReceiverExceptionEvent excpEvent = null;
117        int ioExceptions = 0;
118        APIConfig config = APIConfigFactory.getConfig();
119        final int ioExceptionLimit =
120            config.getInt(APIConfig.TOO_MANY_IO_EXCEPTIONS, 5);
121        
122        SMPPPacket packet = null;
123        while (running && session.getState() != SessionState.UNBOUND) {
124            try {
125                packet = readNextPacket();
126                if (packet == null) {
127                    continue;
128                }
129                session.processReceivedPacket(packet);
130                session.getEventDispatcher().notifyObservers(session, packet);
131                ioExceptions = 0;
132            } catch (ReadTimeoutException x) {
133                SessionState state = session.getState();
134                
135                if (state == SessionState.BINDING) {
136                    LOG.debug("Bind timeout occurred.");
137                    exitEvent = new ReceiverExitEvent(session, null, state);
138                    exitEvent.setReason(ReceiverExitEvent.BIND_TIMEOUT);
139                    break;
140                }
141                
142                if (state == SessionState.BOUND) {
143                	LOG.debug("Read timeout occurred.");
144                	excpEvent = new ReceiverExceptionEvent(session, x);
145                	session.getEventDispatcher().notifyObservers(session, excpEvent);
146                }
147            } catch (IOException x) {
148                LOG.debug("Exception in receiver", x);
149                ioExceptions++;
150                if (ioExceptions >= ioExceptionLimit) {
151                    SessionState state = session.getState();
152                    exitEvent = new ReceiverExitEvent(session, x, state);
153                    exitEvent.setReason(ReceiverExitEvent.EXCEPTION);
154                    break;
155                }
156            }
157        }
158        if (exitEvent == null) {
159            exitEvent = new ReceiverExitEvent(session);
160        }
161        return exitEvent;
162    }
163    
164    private SMPPPacket readNextPacket() throws IOException {
165        return session.getSmscLink().read();
166    }
167}