synchronized void start() { if (queue.closed()) queue.reset(); if (thread == null || !thread.isAlive()) { thread = getThreadFactory().newThread(this, "ViewHandler"); thread.setDaemon( false); // thread cannot terminate if we have tasks left, e.g. when we as coord leave thread.start(); } }
private void stopEventHandlerThread() { if (evt_thread != null) { event_queue.close(false); event_queue = null; evt_thread = null; return; } if (event_queue != null) { event_queue.close(false); event_queue = null; } }
/** * <b>Callback</b>. Called by superclass when event may be handled. * * <p><b>Do not use <code>PassUp</code> in this method as the event is passed up by default by the * superclass after this method returns !</b> * * @return boolean Defaults to true. If false, event will not be passed up the stack. */ public boolean handleUpEvent(Event evt) { switch (evt.getType()) { case Event.CONNECT_OK: // sent by someone else, but WE are responsible for sending this ! case Event.DISCONNECT_OK: // dito (e.g. sent by UDP layer) return false; case Event.SET_LOCAL_ADDRESS: local_addr = (Address) evt.getArg(); if (print_local_addr) { System.out.println( "\n-------------------------------------------------------\n" + "2.1GMS: address is " + local_addr + "\n-------------------------------------------------------"); } return true; // pass up case Event.SUSPECT: try { event_queue.add(evt); } catch (Exception e) { } return true; // pass up case Event.MERGE: try { event_queue.add(evt); } catch (Exception e) { } return false; // don't pass up case Event.FLUSH_OK: synchronized (flush_mutex) { flush_rsp = (FlushRsp) evt.getArg(); flush_mutex.notify(); } return false; // don't pass up case Event.REBROADCAST_MSGS_OK: synchronized (rebroadcast_mutex) { rebroadcast_mutex.notify(); } return false; // don't pass up } return impl.handleUpEvent(evt); }
public void run() { Event evt; while (evt_thread != null && event_queue != null) { try { evt = (Event) event_queue.remove(); switch (evt.getType()) { case Event.SUSPECT: impl.suspect((Address) evt.getArg()); break; case Event.MERGE: impl.merge((Vector) evt.getArg()); break; default: Trace.error( "GMS.run()", "event handler thread encountered event of type " + Event.type2String(evt.getType()) + ": not handled by me !"); break; } } catch (QueueClosedException closed) { break; } catch (Exception ex) { Trace.warn("GMS.run()", "exception=" + ex); } } }
public String dumpQueue() { StringBuilder sb = new StringBuilder(); List v = queue.values(); for (Iterator it = v.iterator(); it.hasNext(); ) { sb.append(it.next() + "\n"); } return sb.toString(); }
/** * Waits until the current requests in the queue have been processed, then clears the queue and * discards new requests from now on */ public synchronized void suspend() { if (!suspended) { suspended = true; queue.clear(); waitUntilCompleted(MAX_COMPLETION_TIME); queue.close(true); resumer = timer.schedule( new Runnable() { public void run() { resume(); } }, resume_task_timeout, TimeUnit.MILLISECONDS); } }
/** Send a message to the address specified in dest */ void sendUdpMessage(Message msg) throws Exception { IpAddress dest; Message copy; Event evt; dest = (IpAddress) msg.getDest(); // guaranteed not to be null setSourceAddress(msg); if (Trace.debug) { Trace.debug( "UDP.sendUdpMessage()", "sending message to " + msg.getDest() + " (src=" + msg.getSrc() + "), headers are " + msg.getHeaders()); // Don't send if destination is local address. Instead, switch dst and src and put in // up_queue. // If multicast message, loopback a copy directly to us (but still multicast). Once we receive // this, // we will discard our own multicast message } if (loopback && (dest.equals(local_addr) || dest.isMulticastAddress())) { copy = msg.copy(); copy.removeHeader(name); copy.setSrc(local_addr); copy.setDest(dest); evt = new Event(Event.MSG, copy); /* Because Protocol.up() is never called by this bottommost layer, we call up() directly in the observer. This allows e.g. PerfObserver to get the time of reception of a message */ if (observer != null) { observer.up(evt, up_queue.size()); } if (Trace.debug) { Trace.info("UDP.sendUdpMessage()", "looped back local message " + copy); } passUp(evt); if (!dest.isMulticastAddress()) { return; } } if (use_outgoing_packet_handler) { outgoing_queue.add(msg); return; } send(msg); }
synchronized void add(Request req) { if (suspended) { if (log.isTraceEnabled()) log.trace(local_addr + ": queue is suspended; request " + req + " is discarded"); return; } start(); try { queue.add(req); history.add(new Date() + ": " + req.toString()); } catch (QueueClosedException e) { if (log.isTraceEnabled()) log.trace("queue is closed; request " + req + " is discarded"); } }
public void run() { long end_time, wait_time; List<Request> requests = new LinkedList<Request>(); while (Thread.currentThread().equals(thread) && !suspended) { try { boolean keepGoing = false; end_time = System.currentTimeMillis() + max_bundling_time; do { Request firstRequest = (Request) queue.remove(INTERVAL); // throws a TimeoutException if it runs into timeout requests.add(firstRequest); if (!view_bundling) break; if (queue.size() > 0) { Request nextReq = (Request) queue.peek(); keepGoing = view_bundling && firstRequest.canBeProcessedTogether(nextReq); } else { wait_time = end_time - System.currentTimeMillis(); if (wait_time > 0) queue.waitUntilClosed( wait_time); // misnomer: waits until element has been added or q closed keepGoing = queue.size() > 0 && firstRequest.canBeProcessedTogether((Request) queue.peek()); } } while (keepGoing && System.currentTimeMillis() < end_time); try { process(requests); } finally { requests.clear(); } } catch (QueueClosedException e) { break; } catch (TimeoutException e) { break; } catch (Throwable catchall) { Util.sleep(50); } } }
public void run() { DatagramPacket packet; byte receive_buf[] = new byte[65535]; int len; byte[] tmp, data; // moved out of loop to avoid excessive object creations (bela March 8 2001) packet = new DatagramPacket(receive_buf, receive_buf.length); while (mcast_receiver != null && mcast_sock != null) { try { packet.setData(receive_buf, 0, receive_buf.length); mcast_sock.receive(packet); len = packet.getLength(); data = packet.getData(); if (len == 1 && data[0] == 0) { if (Trace.debug) { Trace.info("UDP.run()", "received dummy packet"); } continue; } if (len == 4) { // received a diagnostics probe if (data[0] == 'd' && data[1] == 'i' && data[2] == 'a' && data[3] == 'g') { handleDiagnosticProbe(packet.getAddress(), packet.getPort()); continue; } } if (Trace.debug) { Trace.info( "UDP.receive()", "received (mcast) " + packet.getLength() + " bytes from " + packet.getAddress() + ":" + packet.getPort() + " (size=" + len + " bytes)"); } if (len > receive_buf.length) { Trace.error( "UDP.run()", "size of the received packet (" + len + ") is bigger than " + "allocated buffer (" + receive_buf.length + "): will not be able to handle packet. " + "Use the FRAG protocol and make its frag_size lower than " + receive_buf.length); } if (Version.compareTo(data) == false) { Trace.warn( "UDP.run()", "packet from " + packet.getAddress() + ":" + packet.getPort() + " has different version (" + Version.printVersionId(data, Version.version_id.length) + ") from ours (" + Version.printVersionId(Version.version_id) + "). This may cause problems"); } if (use_incoming_packet_handler) { tmp = new byte[len]; System.arraycopy(data, 0, tmp, 0, len); incoming_queue.add(tmp); } else { handleIncomingUdpPacket(data); } } catch (SocketException sock_ex) { if (Trace.trace) { Trace.info("UDP.run()", "multicast socket is closed, exception=" + sock_ex); } break; } catch (InterruptedIOException io_ex) { // thread was interrupted ; // go back to top of loop, where we will terminate loop } catch (Throwable ex) { Trace.error("UDP.run()", "exception=" + ex + ", stack trace=" + Util.printStackTrace(ex)); Util.sleep(300); // so we don't get into 100% cpu spinning (should NEVER happen !) } } if (Trace.trace) { Trace.info("UDP.run()", "multicast thread terminated"); } }
public int size() { return queue.size(); }
public synchronized void resumeForce() { if (queue.closed()) queue.reset(); suspended = false; }
synchronized void stop(boolean flush) { queue.close(flush); if (resumer != null) resumer.cancel(false); }