Search in sources :

Example 11 with AERunnable

use of com.biglybt.core.util.AERunnable in project BiglyBT by BiglySoftware.

the class UISWTInstanceImpl method removeViews.

// TODO: Remove views from PeersView, etc
@Override
public void removeViews(String sParentID, final String sViewID) {
    Map<String, UISWTViewEventListenerHolder> subViews = views.get(sParentID);
    if (subViews == null)
        return;
    if (sParentID.equals(UISWTInstance.VIEW_MAIN)) {
        Utils.execSWTThread(new AERunnable() {

            @Override
            public void runSupport() {
                try {
                    if (uiFunctions != null) {
                        uiFunctions.removePluginView(sViewID);
                    }
                } catch (Throwable e) {
                // SWT not available prolly
                }
            }
        });
    }
    SWTViewListener[] viewListeners = listSWTViewListeners.toArray(new SWTViewListener[0]);
    for (UISWTViewEventListener holder : subViews.values()) {
        for (SWTViewListener l : viewListeners) {
            l.setViewRemoved(sParentID, sViewID, holder);
        }
    }
    subViews.remove(sViewID);
}
Also used : AERunnable(com.biglybt.core.util.AERunnable)

Example 12 with AERunnable

use of com.biglybt.core.util.AERunnable in project BiglyBT by BiglySoftware.

the class DownloadActivityView method dataSourceChanged.

public void dataSourceChanged(Object newDataSource) {
    if (!comp_focused) {
        focus_pending_ds = newDataSource;
        return;
    }
    DownloadManager newManager = ViewUtils.getDownloadManagerFromDataSource(newDataSource);
    if (newManager == manager) {
        return;
    }
    manager = newManager;
    Utils.execSWTThread(new AERunnable() {

        @Override
        public void runSupport() {
            if (panel == null || panel.isDisposed()) {
                return;
            }
            Utils.disposeComposite(panel, false);
            if (manager != null) {
                fillPanel();
                parent.layout(true, true);
            } else {
                ViewUtils.setViewRequiresOneDownload(panel);
            }
        }
    });
    if (manager == null) {
        mpg.setActive(false);
        mpg.reset(new int[5][0]);
    } else {
        DownloadManagerStats stats = manager.getStats();
        stats.setRecentHistoryRetention(true);
        int[][] _history = stats.getRecentHistory();
        // reconstitute the smoothed values to the best of our ability (good enough unless we decide we want
        // to throw more memory at remembering this more accurately...)
        int[] send_history = _history[0];
        int[] recv_history = _history[1];
        int history_secs = send_history.length;
        int[] smoothed_send = new int[history_secs];
        int[] smoothed_recv = new int[history_secs];
        MovingImmediateAverage send_average = GeneralUtils.getSmoothAverage();
        MovingImmediateAverage recv_average = GeneralUtils.getSmoothAverage();
        int smooth_interval = GeneralUtils.getSmoothUpdateInterval();
        int current_smooth_send = 0;
        int current_smooth_recv = 0;
        int pending_smooth_send = 0;
        int pending_smooth_recv = 0;
        for (int i = 0; i < history_secs; i++) {
            pending_smooth_send += send_history[i];
            pending_smooth_recv += recv_history[i];
            if (i % smooth_interval == 0) {
                current_smooth_send = (int) (send_average.update(pending_smooth_send) / smooth_interval);
                current_smooth_recv = (int) (recv_average.update(pending_smooth_recv) / smooth_interval);
                pending_smooth_send = 0;
                pending_smooth_recv = 0;
            }
            smoothed_send[i] = current_smooth_send;
            smoothed_recv[i] = current_smooth_recv;
        }
        int[][] history = { send_history, smoothed_send, recv_history, smoothed_recv, _history[2] };
        mpg.reset(history);
        mpg.setActive(true);
    }
}
Also used : AERunnable(com.biglybt.core.util.AERunnable) DownloadManagerStats(com.biglybt.core.download.DownloadManagerStats) DownloadManager(com.biglybt.core.download.DownloadManager) MovingImmediateAverage(com.biglybt.core.util.average.MovingImmediateAverage)

Example 13 with AERunnable

use of com.biglybt.core.util.AERunnable in project BiglyBT by BiglySoftware.

the class FilesView method parameterChanged.

/* (non-Javadoc)
	 * @see com.biglybt.core.config.ParameterListener#parameterChanged(java.lang.String)
	 */
@Override
public void parameterChanged(String parameterName) {
    if ("FilesView.hide.dnd".equals(parameterName)) {
        hide_dnd_files = COConfigurationManager.getBooleanParameter(parameterName);
        if (btnShowDND != null && !btnShowDND.isDisposed()) {
            Utils.execSWTThread(new AERunnable() {

                @Override
                public void runSupport() {
                    if (btnShowDND != null && !btnShowDND.isDisposed()) {
                        btnShowDND.setSelection(hide_dnd_files);
                    }
                }
            });
        }
        if (tv == null || tv.isDisposed()) {
            return;
        }
        tv.refilter();
    }
}
Also used : AERunnable(com.biglybt.core.util.AERunnable)

Example 14 with AERunnable

use of com.biglybt.core.util.AERunnable in project BiglyBT by BiglySoftware.

the class GlobalManagerStatsImpl method tick.

@Override
public void tick(long mono_now, int tick_count) {
    if (tick_count % current_smoothing_interval == 0) {
        int current_window = GeneralUtils.getSmoothUpdateWindow();
        if (current_smoothing_window != current_window) {
            current_smoothing_window = current_window;
            current_smoothing_interval = GeneralUtils.getSmoothUpdateInterval();
            smoothed_receive_rate = GeneralUtils.getSmoothAverage();
            smoothed_send_rate = GeneralUtils.getSmoothAverage();
        }
        long up = total_data_bytes_sent + total_protocol_bytes_sent;
        long down = total_data_bytes_received + total_protocol_bytes_received;
        smoothed_send_rate.addValue(up - smooth_last_sent);
        smoothed_receive_rate.addValue(down - smooth_last_received);
        smooth_last_sent = up;
        smooth_last_received = down;
    }
    if (tick_count % 60 == 0) {
        stats_dispatcher.dispatch(new AERunnable() {

            @Override
            public void runSupport() {
                try {
                    InetAddress ia = NetworkAdmin.getSingleton().getDefaultPublicAddress();
                    String[] dets = PeerUtils.getCountryDetails(ia);
                    if (dets != null && dets.length > 0) {
                        country_my_cc = dets[0];
                    }
                } catch (Throwable e) {
                }
                List<DownloadManager> all_dms = manager.getDownloadManagers();
                List<AggregateStatsDownloadWrapper> dms_list = new ArrayList<>(all_dms.size() * 2);
                List<List<PEPeer>> peer_lists = new ArrayList<>(all_dms.size() * 2);
                synchronized (PEER_DATA_KEY) {
                    if (!removed_peers.isEmpty()) {
                        for (Map.Entry<DownloadManager, List<PEPeer>> entry : removed_peers.entrySet()) {
                            AggregateStatsDownloadWrapper as = (AggregateStatsDownloadWrapper) entry.getKey().getUserData(DOWNLOAD_DATA_KEY);
                            dms_list.add(as);
                            peer_lists.add(entry.getValue());
                        }
                        removed_peers.clear();
                    }
                }
                List<AggregateStatsDownloadWrapper> dms_complete = new ArrayList<>(all_dms.size());
                for (DownloadManager dm : all_dms) {
                    AggregateStatsDownloadWrapper as = (AggregateStatsDownloadWrapper) dm.getUserData(DOWNLOAD_DATA_KEY);
                    if (as != null) {
                        dms_complete.add(as);
                    }
                    PEPeerManager pm = dm.getPeerManager();
                    if (pm != null) {
                        List<PEPeer> peers = pm.getPeers();
                        if (!peers.isEmpty()) {
                            dms_list.add(as);
                            peer_lists.add(peers);
                        }
                    }
                }
                // single threaded here remember
                Set<CountryDetailsImpl> updated = new HashSet<>();
                Map<String, long[]> updates = new HashMap<>();
                for (int i = 0; i < dms_list.size(); i++) {
                    AggregateStatsDownloadWrapper dms = dms_list.get(i);
                    List<PEPeer> peers = peer_lists.get(i);
                    for (PEPeer peer : peers) {
                        int peer_state = peer.getPeerState();
                        if (peer_state != PEPeer.TRANSFERING && peer_state != PEPeer.CLOSING && peer_state != PEPeer.DISCONNECTED) {
                            continue;
                        }
                        if (peer.isLANLocal()) {
                            if (!AddressUtils.isExplicitLANRateLimitAddress(peer.getIp())) {
                                continue;
                            }
                        }
                        PEPeerStats stats = peer.getStats();
                        long sent = stats.getTotalDataBytesSent();
                        long recv = stats.getTotalDataBytesReceived();
                        if (sent + recv > 0) {
                            PeerDetails details = (PeerDetails) peer.getUserData(PEER_DATA_KEY);
                            if (details == null) {
                                String[] dets = PeerUtils.getCountryDetails(peer);
                                details = new PeerDetails(dets == null || dets.length < 1 ? PeerUtils.CC_UNKNOWN : dets[0]);
                                long[] init_data = (long[]) peer.getUserData(PEER_DATA_INIT_KEY);
                                if (init_data != null) {
                                    details.sent = init_data[0];
                                    details.recv = init_data[1];
                                }
                                peer.setUserData(PEER_DATA_KEY, details);
                            }
                            long[] final_data = (long[]) peer.getUserData(PEER_DATA_FINAL_KEY);
                            if (final_data != null) {
                                sent = final_data[0];
                                recv = final_data[1];
                            }
                            long diff_sent = sent - details.sent;
                            long diff_recv = recv - details.recv;
                            if (diff_sent + diff_recv > 0) {
                                String cc = details.cc;
                                {
                                    // global
                                    long[] totals = updates.get(cc);
                                    if (totals == null) {
                                        totals = new long[] { diff_sent, diff_recv };
                                        updates.put(cc, totals);
                                    } else {
                                        totals[0] += diff_sent;
                                        totals[1] += diff_recv;
                                    }
                                }
                                if (dms != null) {
                                    // download specific
                                    long[] totals = dms.updates.get(cc);
                                    if (totals == null) {
                                        totals = new long[] { diff_sent, diff_recv };
                                        dms.updates.put(cc, totals);
                                    } else {
                                        totals[0] += diff_sent;
                                        totals[1] += diff_recv;
                                    }
                                }
                            }
                            details.sent = sent;
                            details.recv = recv;
                        }
                    }
                }
                for (AggregateStatsDownloadWrapper dms : dms_complete) {
                    dms.updateComplete();
                }
                long total_diff_sent = 0;
                long total_diff_recv = 0;
                for (Map.Entry<String, long[]> entry : updates.entrySet()) {
                    String cc = entry.getKey();
                    long[] totals = entry.getValue();
                    long diff_sent = totals[0];
                    long diff_recv = totals[1];
                    CountryDetailsImpl cd = (CountryDetailsImpl) country_details.get(cc);
                    if (cd == null) {
                        cd = new CountryDetailsImpl(cc);
                        country_details.put(cc, cd);
                    }
                    updated.add(cd);
                    if (diff_sent > 0) {
                        cd.last_sent = diff_sent;
                        cd.total_sent += diff_sent;
                        cd.sent_average.update(diff_sent);
                        total_diff_sent += diff_sent;
                    } else {
                        cd.last_sent = 0;
                        cd.sent_average.update(diff_sent);
                    }
                    if (diff_recv > 0) {
                        cd.last_recv = diff_recv;
                        cd.total_recv += diff_recv;
                        cd.recv_average.update(diff_recv);
                        total_diff_recv += diff_recv;
                    } else {
                        cd.last_recv = 0;
                        cd.recv_average.update(diff_recv);
                    }
                }
                updated.add(country_total);
                if (total_diff_sent > 0) {
                    country_total.last_sent = total_diff_sent;
                    country_total.total_sent += total_diff_sent;
                    country_total.sent_average.update(total_diff_sent);
                } else {
                    country_total.last_sent = 0;
                    country_total.sent_average.update(0);
                }
                if (total_diff_recv > 0) {
                    country_total.last_recv = total_diff_recv;
                    country_total.total_recv += total_diff_recv;
                    country_total.recv_average.update(total_diff_recv);
                } else {
                    country_total.last_recv = 0;
                    country_total.recv_average.update(0);
                }
                for (CountryDetails cd : country_details.values()) {
                    if (!updated.contains(cd)) {
                        CountryDetailsImpl cdi = (CountryDetailsImpl) cd;
                        cdi.last_recv = 0;
                        cdi.last_sent = 0;
                        cdi.recv_average.update(0);
                        cdi.sent_average.update(0);
                    }
                }
                country_details_seq.incrementAndGet();
            }
        });
    }
    if (tick_count % 10 == 0) {
        stats_dispatcher.dispatch(new AERunnable() {

            @Override
            public void runSupport() {
                for (Iterator<RemoteStats> it = pending_stats.values().iterator(); it.hasNext(); ) {
                    RemoteStats stats = it.next();
                    it.remove();
                    addRemoteStats(stats);
                }
            }
        });
    }
/* test load
		if ( tick_count % 30 == 0 ){
			
			RemoteStats	stats = 
				new RemoteStats()
				{
					final String[] CCS = { "US", "GB", "FR", "IT", "AU", "DE" };
										
					RemoteCountryStats[]	stats = new RemoteCountryStats[Math.abs(RandomUtils.nextInt(4))+1];
					
					{
						for ( int i=0; i<stats.length;i++){
					
							String cc = CCS[RandomUtils.nextAbsoluteInt() % CCS.length ];

							stats[i] = 
								new RemoteCountryStats()
								{
									public String 
									getCC()
									{
										return( cc );
									}
									
									public long
									getAverageReceivedBytes()
									{
										return( 100*1024 );
									}
								};
						}
					}
					
					public InetAddress
					getRemoteAddress()
					{
						byte[] bytes = new byte[4];
						
						RandomUtils.nextBytes( bytes );
						
						try{
							return( InetAddress.getByAddress( bytes ));
							
						}catch( Throwable e ){
							
							return( null );
						}
					}
					
					public long
					getMonoTime()
					{
						return( SystemTime.getMonotonousTime());
					}
					
					public RemoteCountryStats[]
					getStats()		
					{
						return( stats );
					}
				};
				
			receiveRemoteStats( stats );
		}
		*/
}
Also used : AERunnable(com.biglybt.core.util.AERunnable) PEPeer(com.biglybt.core.peer.PEPeer) HashSet(java.util.HashSet) Set(java.util.Set) PEPeerStats(com.biglybt.core.peer.PEPeerStats) DownloadManager(com.biglybt.core.download.DownloadManager) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) PEPeerManager(com.biglybt.core.peer.PEPeerManager) InetAddress(java.net.InetAddress) HashMap(java.util.HashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 15 with AERunnable

use of com.biglybt.core.util.AERunnable in project BiglyBT by BiglySoftware.

the class DeviceDriveManager method driveDetected.

@Override
public void driveDetected(final DriveDetectedInfo info) {
    // System.out.println("DD " + info.getLocation() + " via " + Debug.getCompressedStackTrace());
    async_dispatcher.dispatch(new AERunnable() {

        @Override
        public void runSupport() {
            Map<String, Object> infoMap = info.getInfoMap();
            boolean isWritableUSB = MapUtils.getMapBoolean(infoMap, "isWritableUSB", false);
            File root = info.getLocation();
            String sProdID = MapUtils.getMapString(infoMap, "ProductID", MapUtils.getMapString(infoMap, "Product Name", "")).trim();
            String sVendor = MapUtils.getMapString(infoMap, "VendorID", MapUtils.getMapString(infoMap, "Vendor Name", "")).trim();
            // models anyway, so including that wouldn't have helped anyway
            if ((sVendor.equalsIgnoreCase("htc") && sProdID.equalsIgnoreCase("android phone")) || (sVendor.toLowerCase().contains("motorola") && sProdID.length() > 0) || sVendor.equalsIgnoreCase("samsung")) {
                if (isWritableUSB && sVendor.equalsIgnoreCase("samsung")) {
                    // Samsungs that start with Y are MP3 players
                    // Samsungs that don't have a dash aren't smart phones (none that we know of anyway..)
                    // Fake not writable so we remove the device instead of adding it
                    isWritableUSB = !sProdID.startsWith("Y") && sProdID.matches(".*[A-Z]-.*");
                }
                String name = sProdID.startsWith(sVendor) ? "" : sVendor;
                if (sVendor.length() > 0) {
                    name += " ";
                }
                name += sProdID;
                String id = "android.";
                id += sProdID.replaceAll(" ", ".").toLowerCase();
                if (sVendor.length() > 0) {
                    id += "." + sVendor.replaceAll(" ", ".").toLowerCase();
                }
                if (isWritableUSB) {
                    addDevice(name, id, root, new File(root, "videos"), true);
                } else {
                    // Fixup old bug where we were adding Samsung hard drives as devices
                    Device existingDevice = getDeviceMediaRendererByClassification(id);
                    if (existingDevice != null) {
                        existingDevice.remove();
                    }
                }
                return;
            } else if (isWritableUSB && sVendor.toLowerCase().equals("rim")) {
                String name = sVendor;
                if (name.length() > 0) {
                    name += " ";
                }
                name += sProdID;
                String id = "";
                id += sProdID.replaceAll(" ", ".").toLowerCase();
                if (sVendor.length() > 0) {
                    id += "." + sVendor.replaceAll(" ", ".").toLowerCase();
                }
                DeviceMediaRendererManual device = addDevice(name, id, root, new File(root, "videos"), false);
                if (device != null) {
                    device.setImageID("bb");
                }
                return;
            }
            if (!isWritableUSB) {
                return;
            }
            if (root.exists()) {
                File[] folders = root.listFiles();
                if (folders != null) {
                    Set<String> names = new HashSet<>();
                    for (File file : folders) {
                        names.add(file.getName().toLowerCase());
                    }
                    if (names.contains("psp") && names.contains("video")) {
                        addDevice("PSP", "sony.PSP", root, new File(root, "VIDEO"), false);
                        return;
                    }
                }
            }
            String pid = MapUtils.getMapString(infoMap, "PID", null);
            String vid = MapUtils.getMapString(infoMap, "VID", null);
            if (pid != null && vid != null) {
                String name = sProdID.startsWith(sVendor) ? "" : sVendor;
                if (name.length() > 0) {
                    name += " ";
                }
                name += sProdID;
                String id = "";
                id += sProdID.replaceAll(" ", ".").toLowerCase();
                id += "." + pid.toLowerCase();
                if (sVendor.length() > 0) {
                    id += "." + sVendor.replaceAll(" ", ".").toLowerCase();
                }
                id += "." + vid.toLowerCase();
                // cheap hack to detect the PSP when it has no psp or video dir
                if (id.equals("\"psp\".ms.02d2.sony.054c") || id.equals("\"psp\".ms.0381.sony.054c")) {
                    if (addDevice("PSP", "sony.PSP", root, new File(root, "VIDEO"), false) != null) {
                        return;
                    }
                }
                addDevice(name, id, root, new File(root, "video"), true);
            }
        }
    });
}
Also used : AERunnable(com.biglybt.core.util.AERunnable) Device(com.biglybt.core.devices.Device) File(java.io.File)

Aggregations

AERunnable (com.biglybt.core.util.AERunnable)92 GridLayout (org.eclipse.swt.layout.GridLayout)12 DownloadManager (com.biglybt.core.download.DownloadManager)11 Image (org.eclipse.swt.graphics.Image)10 ArrayList (java.util.ArrayList)9 GridData (org.eclipse.swt.layout.GridData)9 CoreRunningListener (com.biglybt.core.CoreRunningListener)7 File (java.io.File)7 PEPeer (com.biglybt.core.peer.PEPeer)6 Shell (org.eclipse.swt.widgets.Shell)6 Core (com.biglybt.core.Core)5 ImageLoader (com.biglybt.ui.swt.imageloader.ImageLoader)5 Point (org.eclipse.swt.graphics.Point)5 PEPeerManager (com.biglybt.core.peer.PEPeerManager)4 Subscription (com.biglybt.core.subs.Subscription)3 TableColumnCore (com.biglybt.ui.common.table.TableColumnCore)3 UISWTViewEvent (com.biglybt.ui.swt.pif.UISWTViewEvent)3 MessageBoxShell (com.biglybt.ui.swt.shells.MessageBoxShell)3 MouseAdapter (org.eclipse.swt.events.MouseAdapter)3 MouseEvent (org.eclipse.swt.events.MouseEvent)3