/** * An event is to be sent down the stack. The layer may want to examine its type and perform some * action on it, depending on the event's type. If the event is a message MSG, then the layer may * need to add a header to it (or do nothing at all) before sending it down the stack using <code> * PassDown</code>. In case of a GET_ADDRESS event (which tries to retrieve the stack's address * from one of the bottom layers), the layer may need to send a new response event back up the * stack using <code>up_prot.up()</code>. The PING protocol is interested in several different * down events, Event.FIND_INITIAL_MBRS - sent by the GMS layer and expecting a GET_MBRS_OK * Event.TMP_VIEW and Event.VIEW_CHANGE - a view change event Event.BECOME_SERVER - called after * client has joined and is fully working group member Event.CONNECT, Event.DISCONNECT. */ @SuppressWarnings("unchecked") public Object down(Event evt) { switch (evt.getType()) { case Event.FIND_INITIAL_MBRS: // sent by GMS layer case Event.FIND_ALL_VIEWS: // sends the GET_MBRS_REQ to all members, waits 'timeout' ms or until 'num_initial_members' // have been retrieved long start = System.currentTimeMillis(); boolean find_all_views = evt.getType() == Event.FIND_ALL_VIEWS; Promise<JoinRsp> promise = (Promise<JoinRsp>) evt.getArg(); List<PingData> rsps = find_all_views ? findAllViews(promise) : findInitialMembers(promise); long diff = System.currentTimeMillis() - start; if (log.isTraceEnabled()) log.trace("discovery took " + diff + " ms: responses: " + Util.printPingData(rsps)); return rsps; case Event.TMP_VIEW: case Event.VIEW_CHANGE: List<Address> tmp; view = (View) evt.getArg(); if ((tmp = view.getMembers()) != null) { synchronized (members) { members.clear(); members.addAll(tmp); } } current_coord = !members.isEmpty() ? members.get(0) : null; is_coord = current_coord != null && local_addr != null && current_coord.equals(local_addr); return down_prot.down(evt); case Event.BECOME_SERVER: // called after client has joined and is fully working group member down_prot.down(evt); is_server = true; return null; case Event.SET_LOCAL_ADDRESS: local_addr = (Address) evt.getArg(); return down_prot.down(evt); case Event.CONNECT: case Event.CONNECT_WITH_STATE_TRANSFER: case Event.CONNECT_USE_FLUSH: case Event.CONNECT_WITH_STATE_TRANSFER_USE_FLUSH: is_leaving = false; group_addr = (String) evt.getArg(); Object ret = down_prot.down(evt); handleConnect(); return ret; case Event.DISCONNECT: is_leaving = true; handleDisconnect(); return down_prot.down(evt); default: return down_prot.down(evt); // Pass on to the layer below us } }
public List<PingData> get(long timeout) throws InterruptedException { long start_time = System.currentTimeMillis(), time_to_wait = timeout; promise.getLock().lock(); try { while (time_to_wait > 0 && !promise.hasResult()) { // if num_expected_srv_rsps > 0, then it overrides num_expected_rsps if (num_expected_srv_rsps > 0) { int received_srv_rsps = getNumServerResponses(ping_rsps); if (received_srv_rsps >= num_expected_srv_rsps) return new LinkedList<PingData>(ping_rsps); } else if (ping_rsps.size() >= num_expected_rsps) { return new LinkedList<PingData>(ping_rsps); } if (break_on_coord_rsp && containsCoordinatorResponse(ping_rsps)) return new LinkedList<PingData>(ping_rsps); promise.getCond().await(time_to_wait, TimeUnit.MILLISECONDS); time_to_wait = timeout - (System.currentTimeMillis() - start_time); } return new LinkedList<PingData>(ping_rsps); } finally { promise.getLock().unlock(); } }
public void receive(Message msg) { byte[] buf = msg.getRawBuffer(); byte type = buf[msg.getOffset()]; switch (type) { case START: ByteBuffer tmp = ByteBuffer.wrap(buf, 1 + msg.getOffset(), Global.LONG_SIZE); num_msgs = (int) tmp.getLong(); print = num_msgs / 10; current_value.set(0); total_bytes.set(0); start = System.currentTimeMillis(); break; case DATA: long new_val = current_value.incrementAndGet(); total_bytes.addAndGet(msg.getLength() - Global.INT_SIZE); if (print > 0 && new_val % print == 0) System.out.println("received " + new_val); if (new_val >= num_msgs) { long time = System.currentTimeMillis() - start; double msgs_sec = (current_value.get() / (time / 1000.0)); double throughput = total_bytes.get() / (time / 1000.0); System.out.println( String.format( "\nreceived %d messages in %d ms (%.2f msgs/sec), throughput=%s", current_value.get(), time, msgs_sec, Util.printBytes(throughput))); break; } break; default: System.err.println("Type " + type + " is invalid"); } }
public static long sleep(long timeout) { // System.out.println("sleep()"); long start = System.currentTimeMillis(); Util.sleep(timeout); // throw new NullPointerException("boom"); return System.currentTimeMillis() - start; }
protected void waitForBridgeView( int expected_size, long timeout, long interval, JChannel... channels) { long deadline = System.currentTimeMillis() + timeout; while (System.currentTimeMillis() < deadline) { boolean views_correct = true; for (JChannel ch : channels) { RELAY2 relay = (RELAY2) ch.getProtocolStack().findProtocol(RELAY2.class); View bridge_view = relay.getBridgeView(BRIDGE_CLUSTER); if (bridge_view == null || bridge_view.size() != expected_size) { views_correct = false; break; } } if (views_correct) break; Util.sleep(interval); } System.out.println("Bridge views:\n"); for (JChannel ch : channels) { RELAY2 relay = (RELAY2) ch.getProtocolStack().findProtocol(RELAY2.class); View bridge_view = relay.getBridgeView(BRIDGE_CLUSTER); System.out.println(ch.getAddress() + ": " + bridge_view); } for (JChannel ch : channels) { RELAY2 relay = (RELAY2) ch.getProtocolStack().findProtocol(RELAY2.class); View bridge_view = relay.getBridgeView(BRIDGE_CLUSTER); assert bridge_view != null && bridge_view.size() == expected_size : ch.getAddress() + ": bridge view=" + bridge_view + ", expected=" + expected_size; } }
/** * Sends a MERGE_REQ to all coords and populates a list of MergeData (in merge_rsps). Returns * after coords.size() response have been received, or timeout msecs have elapsed (whichever is * first). * * <p>If a subgroup coordinator rejects the MERGE_REQ (e.g. because of participation in a * different merge), <em>that member will be removed from coords !</em> * * @param coords A map of coordinatgor addresses and associated membership lists * @param new_merge_id The new merge id * @param timeout Max number of msecs to wait for the merge responses from the subgroup coords */ private boolean getMergeDataFromSubgroupCoordinators( Map<Address, Collection<Address>> coords, MergeId new_merge_id, long timeout) { boolean gotAllResponses; long start = System.currentTimeMillis(); merge_rsps.reset(coords.keySet()); if (log.isDebugEnabled()) log.debug(gms.local_addr + ": sending MERGE_REQ to " + coords.keySet()); for (Map.Entry<Address, Collection<Address>> entry : coords.entrySet()) { Address coord = entry.getKey(); Collection<Address> mbrs = entry.getValue(); Message msg = new Message(coord, null, null); msg.setFlag(Message.OOB); GMS.GmsHeader hdr = new GMS.GmsHeader(GMS.GmsHeader.MERGE_REQ, mbrs); hdr.mbr = gms.local_addr; hdr.merge_id = new_merge_id; msg.putHeader(gms.getId(), hdr); gms.getDownProtocol().down(new Event(Event.MSG, msg)); } // wait until num_rsps_expected >= num_rsps or timeout elapsed merge_rsps.waitForAllResponses(timeout); gotAllResponses = merge_rsps.hasAllResponses(); long stop = System.currentTimeMillis(); if (log.isDebugEnabled()) log.debug( gms.local_addr + ": collected " + merge_rsps.size() + " merge response(s) in " + (stop - start) + " ms"); return gotAllResponses; }
/** * Sends the new view and digest to all subgroup coordinors in coords. Each coord will in turn * * <ol> * <li>broadcast the new view and digest to all the members of its subgroup (MergeView) * <li>on reception of the view, if it is a MergeView, each member will set the digest and * install the new view * </ol> */ private void sendMergeView( Collection<Address> coords, MergeData combined_merge_data, MergeId merge_id) { if (coords == null || combined_merge_data == null) return; View view = combined_merge_data.view; Digest digest = combined_merge_data.digest; if (view == null || digest == null) { if (log.isErrorEnabled()) log.error("view or digest is null, cannot send consolidated merge view/digest"); return; } if (log.isDebugEnabled()) log.debug( gms.local_addr + ": sending merge view " + view.getVid() + " to coordinators " + coords); gms.merge_ack_collector.reset(coords); int size = gms.merge_ack_collector.size(); long timeout = gms.view_ack_collection_timeout; long start = System.currentTimeMillis(); for (Address coord : coords) { Message msg = new Message(coord, null, null); GMS.GmsHeader hdr = new GMS.GmsHeader(GMS.GmsHeader.INSTALL_MERGE_VIEW); hdr.view = view; hdr.my_digest = digest; hdr.merge_id = merge_id; msg.putHeader(gms.getId(), hdr); gms.getDownProtocol().down(new Event(Event.MSG, msg)); } // [JGRP-700] - FLUSH: flushing should span merge // if flush is in stack wait for acks from separated island coordinators if (gms.flushProtocolInStack) { try { gms.merge_ack_collector.waitForAllAcks(timeout); long stop = System.currentTimeMillis(); if (log.isTraceEnabled()) log.trace( "received all ACKs (" + size + ") for merge view " + view + " in " + (stop - start) + "ms"); } catch (TimeoutException e) { log.warn( gms.local_addr + ": failed to collect all ACKs for merge (" + size + ") for view " + view + " after " + timeout + "ms, missing ACKs from " + gms.merge_ack_collector.printMissing()); } } }
public Results startTest() throws Throwable { System.out.println( "invoking " + num_msgs + " RPCs of " + Util.printBytes(msg_size) + ", sync=" + sync + ", oob=" + oob); int total_gets = 0, total_puts = 0; final AtomicInteger num_msgs_sent = new AtomicInteger(0); Invoker[] invokers = new Invoker[num_threads]; for (int i = 0; i < invokers.length; i++) invokers[i] = new Invoker(members, num_msgs, num_msgs_sent); long start = System.currentTimeMillis(); for (Invoker invoker : invokers) invoker.start(); for (Invoker invoker : invokers) { invoker.join(); total_gets += invoker.numGets(); total_puts += invoker.numPuts(); } long total_time = System.currentTimeMillis() - start; System.out.println("done (in " + total_time + " ms)"); return new Results(total_gets, total_puts, total_time); }
private void measureThrougput(long size) { if ((System.currentTimeMillis() - startTimeThroughput) > oneSecond) { control.throughput.setText("" + (throughput / 1024) + " KB/sec"); startTimeThroughput = System.currentTimeMillis(); throughput = 0; } else { throughput += size; } }
public Object objectFromByteBuffer(byte[] buf, int offset, int length) throws Exception { if (buf == null || (offset == 0 && length == buf.length)) return marshaller.objectFromByteBuffer(buf); byte[] tmp = new byte[length]; System.arraycopy(buf, offset, tmp, 0, length); return marshaller.objectFromByteBuffer(tmp); }
/** Set the digest and the send the state up to the application */ protected void handleStateRsp(final Digest digest, Address sender, byte[] state) { try { if (isDigestNeeded()) { punchHoleFor(sender); closeBarrierAndSuspendStable(); // fix for https://jira.jboss.org/jira/browse/JGRP-1013 if (digest != null) down_prot.down( new Event(Event.OVERWRITE_DIGEST, digest)); // set the digest (e.g. in NAKACK) } waiting_for_state_response = false; stop = System.currentTimeMillis(); log.debug( "%s: received state, size=%s, time=%d milliseconds", local_addr, (state == null ? "0" : Util.printBytes(state.length)), stop - start); StateTransferResult result = new StateTransferResult(state); up_prot.up(new Event(Event.GET_STATE_OK, result)); down_prot.down( new Event(Event.GET_VIEW_FROM_COORD)); // https://issues.jboss.org/browse/JGRP-1751 } catch (Throwable t) { handleException(t); } finally { if (isDigestNeeded()) { closeHoleFor(sender); openBarrierAndResumeStable(); } } }
protected synchronized boolean acquireTryLock(long timeout, boolean use_timeout) throws InterruptedException { if (denied) return false; if (!acquired) { is_trylock = true; this.timeout = timeout; if (owner == null) owner = getOwner(); sendGrantLockRequest(name, owner, timeout, true); long target_time = use_timeout ? System.currentTimeMillis() + timeout : 0; boolean interrupted = false; while (!acquired && !denied) { if (use_timeout) { long wait_time = target_time - System.currentTimeMillis(); if (wait_time <= 0) break; else { this.timeout = wait_time; try { this.wait(wait_time); } catch (InterruptedException e) { // If we were interrupted and haven't received a response yet then we try to // clean up the lock request and throw the exception if (!acquired && !denied) { _unlock(true); throw e; } // In the case that we were told if we acquired or denied the lock then return that, // but // make sure we set the interrupt status interrupted = true; } } } else { try { this.wait(); } catch (InterruptedException e) { interrupted = true; } } } if (interrupted) Thread.currentThread().interrupt(); } if (!acquired || denied) _unlock(true); return acquired && !denied; }
protected long await(long nanoSeconds) throws InterruptedException { long target_nano = System.nanoTime() + nanoSeconds; if (!signaled.get()) { // We release the lock at the same time as waiting on the // condition lock.acquired = false; sendAwaitConditionRequest(lock.name, lock.owner); boolean interrupted = false; while (!signaled.get()) { long wait_nano = target_nano - System.nanoTime(); // If we waited max time break out if (wait_nano > 0) { parker.set(Thread.currentThread()); LockSupport.parkNanos(this, wait_nano); if (Thread.interrupted()) { // If we were interrupted and haven't received a response yet then we try to // clean up the lock request and throw the exception if (!signaled.get()) { sendDeleteAwaitConditionRequest(lock.name, lock.owner); throw new InterruptedException(); } // In the case that we were signaled and interrupted // we want to return the signal but still interrupt // our thread interrupted = true; } } else { break; } } if (interrupted) Thread.currentThread().interrupt(); } // We set as if this signal was no released. This way if the // condition is reused again, but the client condition isn't lost // we won't think we were signaled immediately // If we weren't signaled then delete our request if (!signaled.getAndSet(false)) { sendDeleteAwaitConditionRequest(lock.name, lock.owner); } return target_nano - System.nanoTime(); }
public static void testGetProperty() { Properties props = new Properties(); props.setProperty("name", "Bela"); props.setProperty("key", "val"); System.setProperty("name", "Michelle"); System.setProperty("name2", "Nicole"); String retval; retval = Util.getProperty(new String[] {"name", "name2"}, props, "name", "Jeannette"); Assert.assertEquals("Bela", retval); props.setProperty("name", "Bela"); props.setProperty("key", "val"); retval = Util.getProperty(new String[] {"name2", "name"}, props, "name", "Jeannette"); Assert.assertEquals("Bela", retval); props.setProperty("name", "Bela"); props.setProperty("key", "val"); retval = Util.getProperty(new String[] {"name3", "name"}, props, "name", "Jeannette"); Assert.assertEquals("Bela", retval); props.setProperty("name", "Bela"); props.setProperty("key", "val"); retval = Util.getProperty(new String[] {"name3", "name4"}, props, "name", "Jeannette"); Assert.assertEquals("Bela", retval); props.setProperty("name", "Bela"); props.setProperty("key", "val"); retval = Util.getProperty(new String[] {"name2", "name"}, props, "name", "Jeannette"); Assert.assertEquals("Bela", retval); props.setProperty("name", "Bela"); props.setProperty("key", "val"); retval = Util.getProperty(new String[] {"name2", "name"}, props, "name2", "Jeannette"); Assert.assertEquals("Nicole", retval); props.setProperty("name", "Bela"); props.setProperty("key", "val"); retval = Util.getProperty(new String[] {"name2", "name"}, props, "name2", null); Assert.assertEquals("Nicole", retval); props.setProperty("name", "Bela"); props.setProperty("key", "val"); }
public Object down(Event evt) { switch (evt.getType()) { case Event.TMP_VIEW: case Event.VIEW_CHANGE: handleViewChange((View) evt.getArg()); break; case Event.GET_STATE: Address target; StateTransferInfo info = (StateTransferInfo) evt.getArg(); if (info.target == null) { target = determineCoordinator(); } else { target = info.target; if (target.equals(local_addr)) { log.error("%s: cannot fetch state from myself", local_addr); target = null; } } if (target == null) { log.debug("%s: first member (no state)", local_addr); up_prot.up(new Event(Event.GET_STATE_OK, new StateTransferInfo())); } else { Message state_req = new Message(target) .putHeader(this.id, new StateHeader(StateHeader.STATE_REQ)) .setFlag(Message.Flag.DONT_BUNDLE, Message.Flag.OOB, Message.Flag.SKIP_BARRIER); log.debug("%s: asking %s for state", local_addr, target); // suspend sending and handling of message garbage collection gossip messages, // fixes bugs #943480 and #938584). Wake up when state has been received /*if(log.isDebugEnabled()) log.debug("passing down a SUSPEND_STABLE event"); down_prot.down(new Event(Event.SUSPEND_STABLE, new Long(info.timeout)));*/ waiting_for_state_response = true; start = System.currentTimeMillis(); down_prot.down(new Event(Event.MSG, state_req)); } return null; // don't pass down any further ! case Event.CONFIG: Map<String, Object> config = (Map<String, Object>) evt.getArg(); if (config != null && config.containsKey("flush_supported")) { flushProtocolInStack = true; } break; case Event.SET_LOCAL_ADDRESS: local_addr = (Address) evt.getArg(); break; } return down_prot.down(evt); // pass on to the layer below us }
@Override public long awaitNanos(long nanosTimeout) throws InterruptedException { long beforeLock; InterruptedException ex = null; try { beforeLock = await(nanosTimeout) + System.nanoTime(); } catch (InterruptedException e) { ex = e; throw ex; } finally { lock.lock(); // If we are throwing an InterruptedException // then clear the interrupt state as well. if (ex != null) { Thread.interrupted(); } } return beforeLock - System.nanoTime(); }
@Override public boolean awaitUntil(Date deadline) throws InterruptedException { long waitUntilTime = deadline.getTime(); long currentTime = System.currentTimeMillis(); long waitTime = waitUntilTime - currentTime; if (waitTime > 0) { return await(waitTime, TimeUnit.MILLISECONDS); } else { return false; } }
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); } } }
protected void waitUntilStatus( String site_name, RELAY2.RouteStatus expected_status, long timeout, long interval, JChannel ch) throws Exception { RELAY2 relay = (RELAY2) ch.getProtocolStack().findProtocol(RELAY2.class); if (relay == null) throw new IllegalArgumentException("Protocol RELAY2 not found"); Relayer.Route route = null; long deadline = System.currentTimeMillis() + timeout; while (System.currentTimeMillis() < deadline) { route = relay.getRoute(site_name); if (route != null && route.status() == expected_status) break; Util.sleep(interval); } assert route.status() == expected_status : "status=" + (route != null ? route.status() : "n/a") + ", expected status=" + expected_status; }
public static void testGetProperty2() { String input = "foo, bar, foobar: 1000"; String result = Util.getProperty(input); assert result != null && result.equals("1000"); input = "foo, bar, foobar"; result = Util.getProperty(input); assert result == null; System.setProperty("foobar", "900"); input = "foo, bar, foobar: 1000"; result = Util.getProperty(input); assert result != null && result.equals("900"); input = "foo, bar, foobar"; result = Util.getProperty(input); assert result != null && result.equals("900"); System.setProperty("bar", "500"); input = "foo, bar, foobar: 1000"; result = Util.getProperty(input); assert result != null && result.equals("500"); input = "foo, bar, foobar"; result = Util.getProperty(input); assert result != null && result.equals("500"); System.setProperty("foo", "200"); input = "foo, bar, foobar: 1000"; result = Util.getProperty(input); assert result != null && result.equals("200"); input = "foo, bar, foobar"; result = Util.getProperty(input); assert result != null && result.equals("200"); }
public void run() { // 1. Generate merge_id final MergeId new_merge_id = MergeId.create(gms.local_addr); final Collection<Address> coordsCopy = new ArrayList<Address>(coords.keySet()); long start = System.currentTimeMillis(); try { _run(new_merge_id, coordsCopy); // might remove members from coordsCopy } catch (Throwable ex) { if (log.isWarnEnabled()) log.warn(gms.local_addr + ": " + ex + ", merge is cancelled"); sendMergeCancelledMessage(coordsCopy, new_merge_id); cancelMerge( new_merge_id); // the message above cancels the merge, too, but this is a 2nd line of // defense } finally { /* 5. if flush is in stack stop the flush for entire cluster [JGRP-700] - FLUSH: flushing should span merge */ if (gms.flushProtocolInStack) gms.stopFlush(); thread = null; } long diff = System.currentTimeMillis() - start; if (log.isDebugEnabled()) log.debug(gms.local_addr + ": merge " + new_merge_id + " took " + diff + " ms"); }
void runServer() throws Exception { System.setProperty("jgroups.bind_addr", host); if (port > 0) { Protocol transport = ch.getProtocolStack().getTransport(); if (transport instanceof TP) { ((TP) transport).setBindPort(port); } } ch.connect(null); // this makes it a unicast channel System.out.println( "server started at " + new java.util.Date() + ", listening on " + ch.getAddress()); while (ch.isOpen()) { Util.sleep(10000); } }
public Responses findMembers( final List<Address> members, final boolean initial_discovery, boolean async) { num_discovery_requests++; int num_expected = members != null ? members.size() : 0; int capacity = members != null ? members.size() : 16; final Responses rsps = new Responses(num_expected, initial_discovery && break_on_coord_rsp, capacity); synchronized (ping_responses) { ping_responses.put(System.nanoTime(), rsps); } if (async || async_discovery) timer.execute(() -> findMembers(members, initial_discovery, rsps)); else findMembers(members, initial_discovery, rsps); weedOutCompletedDiscoveryResponses(); return rsps; }
protected void addResponse(PingData rsp, boolean overwrite) { synchronized (ping_responses) { for (Iterator<Map.Entry<Long, Responses>> it = ping_responses.entrySet().iterator(); it.hasNext(); ) { Map.Entry<Long, Responses> entry = it.next(); long timestamp = entry.getKey(); Responses rsps = entry.getValue(); rsps.addResponse(rsp, overwrite); if (rsps.isDone() || TimeUnit.MILLISECONDS.convert(System.nanoTime() - timestamp, TimeUnit.NANOSECONDS) > discovery_rsp_expiry_time) { it.remove(); rsps.done(); } } } }
/** * Removes responses which are done or whose timeout has expired (in the latter case, an expired * response is marked as done) */ @ManagedOperation(description = "Removes expired or completed responses") public void weedOutCompletedDiscoveryResponses() { synchronized (ping_responses) { for (Iterator<Map.Entry<Long, Responses>> it = ping_responses.entrySet().iterator(); it.hasNext(); ) { Map.Entry<Long, Responses> entry = it.next(); long timestamp = entry.getKey(); Responses rsps = entry.getValue(); if (rsps.isDone() || TimeUnit.MILLISECONDS.convert(System.nanoTime() - timestamp, TimeUnit.NANOSECONDS) > discovery_rsp_expiry_time) { it.remove(); rsps.done(); } } } }
private class ReceiverThread extends Thread { volatile boolean running = true; Thread nullifier = null; private long startTimeThroughput = System.currentTimeMillis(); private final long oneSecond = 1000; private long throughput = 1; public ReceiverThread() { nullifier = new Thread( new Runnable() { public void run() { // nullifies throughput display while (running) { Util.sleep(2000); if ((System.currentTimeMillis() - startTimeThroughput) > 2000) { control.throughput.setText("0 KB/sec"); } } } }); nullifier.start(); } public void shutDown() { running = false; } private void measureThrougput(long size) { if ((System.currentTimeMillis() - startTimeThroughput) > oneSecond) { control.throughput.setText("" + (throughput / 1024) + " KB/sec"); startTimeThroughput = System.currentTimeMillis(); throughput = 0; } else { throughput += size; } } public void run() { Object tmp; Message msg = null; int counter = 0; Vector v = new Vector(); while (running) { Util.sleep(10); try { tmp = channel.receive(0); if (tmp == null) continue; if (tmp instanceof View) { View vw = (View) tmp; control.viewNumber.setText(String.valueOf(vw.getVid().getId())); control.numMessagesInLastView.setText(String.valueOf(counter)); counter = 0; v.clear(); continue; } if (!(tmp instanceof Message)) continue; msg = (Message) tmp; measureThrougput(msg.size()); TotalPayload p = null; p = (TotalPayload) msg.getObject(); v.addElement(new Integer(p.getRandomSequence())); int size = v.size(); if (size % 50 == 0) { int value = 0; int i = 0; Iterator iter = v.iterator(); while (iter.hasNext()) { i++; int seq = ((Integer) iter.next()).intValue(); if (i % 2 == 0) { value *= seq; } else if (i % 3 == 0) { value -= seq; } else value += seq; } v.clear(); value = Math.abs(value); int r = value % 85; int g = value % 170; int b = value % 255; colorPanel.setSeq(r, g, b); } counter++; } catch (ChannelNotConnectedException e) { e.printStackTrace(); } catch (ChannelClosedException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); } } } }
/** * Setup the Protocol instance acording to the configuration string The following properties are * being read by the UDP protocol param mcast_addr - the multicast address to use default is * 224.0.0.200 param mcast_port - (int) the port that the multicast is sent on default is 7500 * param ip_mcast - (boolean) flag whether to use IP multicast - default is true param ip_ttl - * Set the default time-to-live for multicast packets sent out on this socket. default is 32 * * @return true if no other properties are left. false if the properties still have data in them, * ie , properties are left over and not handled by the protocol stack */ public boolean setProperties(Properties props) { String str, tmp; tmp = System.getProperty("UDP.bind_addr"); if (tmp != null) { str = tmp; } else { str = props.getProperty("bind_addr"); } if (str != null) { try { bind_addr = InetAddress.getByName(str); } catch (UnknownHostException unknown) { Trace.fatal("UDP.setProperties()", "(bind_addr): host " + str + " not known"); return false; } props.remove("bind_addr"); } str = props.getProperty("bind_port"); if (str != null) { bind_port = new Integer(str).intValue(); props.remove("bind_port"); } str = props.getProperty("start_port"); if (str != null) { bind_port = new Integer(str).intValue(); props.remove("start_port"); } str = props.getProperty("port_range"); if (str != null) { port_range = new Integer(str).intValue(); props.remove("port_range"); } str = props.getProperty("mcast_addr"); if (str != null) { mcast_addr_name = new String(str); props.remove("mcast_addr"); } str = props.getProperty("mcast_port"); if (str != null) { mcast_port = new Integer(str).intValue(); props.remove("mcast_port"); } str = props.getProperty("ip_mcast"); if (str != null) { ip_mcast = new Boolean(str).booleanValue(); props.remove("ip_mcast"); } str = props.getProperty("ip_ttl"); if (str != null) { ip_ttl = new Integer(str).intValue(); props.remove("ip_ttl"); } str = props.getProperty("mcast_send_buf_size"); if (str != null) { mcast_send_buf_size = Integer.parseInt(str); props.remove("mcast_send_buf_size"); } str = props.getProperty("mcast_recv_buf_size"); if (str != null) { mcast_recv_buf_size = Integer.parseInt(str); props.remove("mcast_recv_buf_size"); } str = props.getProperty("ucast_send_buf_size"); if (str != null) { ucast_send_buf_size = Integer.parseInt(str); props.remove("ucast_send_buf_size"); } str = props.getProperty("ucast_recv_buf_size"); if (str != null) { ucast_recv_buf_size = Integer.parseInt(str); props.remove("ucast_recv_buf_size"); } str = props.getProperty("loopback"); if (str != null) { loopback = new Boolean(str).booleanValue(); props.remove("loopback"); } // this is deprecated, just left for compatibility (use use_incoming_packet_handler) str = props.getProperty("use_packet_handler"); if (str != null) { use_incoming_packet_handler = new Boolean(str).booleanValue(); props.remove("use_packet_handler"); Trace.warn( "UDP.setProperties()", "'use_packet_handler' is deprecated; use 'use_incoming_packet_handler' instead"); } str = props.getProperty("use_incoming_packet_handler"); if (str != null) { use_incoming_packet_handler = new Boolean(str).booleanValue(); props.remove("use_incoming_packet_handler"); } str = props.getProperty("use_outgoing_packet_handler"); if (str != null) { use_outgoing_packet_handler = new Boolean(str).booleanValue(); props.remove("use_outgoing_packet_handler"); } str = props.getProperty("max_bundle_size"); if (str != null) { int bundle_size = Integer.parseInt(str); if (bundle_size > max_bundle_size) { Trace.error( "UDP.setProperties()", "max_bundle_size (" + bundle_size + ") is greater than largest UDP fragmentation size (" + max_bundle_size + ")"); return false; } if (bundle_size <= 0) { Trace.error("UDP.setProperties()", "max_bundle_size (" + bundle_size + ") is <= 0"); return false; } max_bundle_size = bundle_size; props.remove("max_bundle_size"); } str = props.getProperty("max_bundle_timeout"); if (str != null) { max_bundle_timeout = Long.parseLong(str); if (max_bundle_timeout <= 0) { Trace.error( "UDP.setProperties()", "max_bundle_timeout of " + max_bundle_timeout + " is invalid"); return false; } props.remove("max_bundle_timeout"); } str = props.getProperty("enable_bundling"); if (str != null) { enable_bundling = new Boolean(str).booleanValue(); props.remove("enable_bundling"); } if (props.size() > 0) { System.err.println("UDP.setProperties(): the following properties are not recognized:"); props.list(System.out); return false; } if (enable_bundling) { if (use_outgoing_packet_handler == false) { Trace.warn( "UDP.setProperties()", "enable_bundling is true; setting use_outgoing_packet_handler=true"); } use_outgoing_packet_handler = true; } return true; }
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"); } }
long age() { return System.currentTimeMillis() - timestamp; }
void update() { timestamp = System.currentTimeMillis(); }