author | dadubrow |
Wed, 13 Jan 2010 11:50:49 -0600 | |
changeset 762 | 360ece94cbb4 |
parent 719 | a6f7bb38d188 |
child 850 | cd20cfc0713f |
permissions | -rw-r--r-- |
719 | 1 |
/** |
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 |
* All rights reserved. |
|
4 |
* This component and the accompanying materials are made available |
|
5 |
* under the terms of the License "Eclipse Public License v1.0" |
|
6 |
* which accompanies this distribution, and is available |
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 |
* |
|
9 |
* Initial Contributors: |
|
10 |
* Nokia Corporation - initial contribution. |
|
11 |
* |
|
12 |
* Contributors: |
|
13 |
* |
|
14 |
* Description: |
|
15 |
* |
|
16 |
*/ |
|
17 |
||
18 |
package com.nokia.carbide.remoteconnections.tests.discovery; |
|
19 |
||
20 |
import java.net.URL; |
|
21 |
import java.util.HashSet; |
|
22 |
import java.util.Map; |
|
23 |
import java.util.Random; |
|
24 |
import java.util.Set; |
|
25 |
||
26 |
import org.eclipse.core.runtime.CoreException; |
|
27 |
||
28 |
import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator; |
|
29 |
import com.nokia.carbide.remoteconnections.interfaces.IConnectionFactory; |
|
30 |
import com.nokia.carbide.remoteconnections.interfaces.IConnectionType; |
|
31 |
import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager; |
|
32 |
import com.nokia.carbide.remoteconnections.internal.api.IConnection2; |
|
33 |
import com.nokia.carbide.remoteconnections.internal.api.IDeviceDiscoveryAgent; |
|
34 |
import com.nokia.carbide.remoteconnections.tests.extensions.IntervalConnectionType; |
|
35 |
import com.nokia.carbide.remoteconnections.tests.extensions.TestFilter; |
|
36 |
||
37 |
public class RandomDiscoveryAgent implements IDeviceDiscoveryAgent { |
|
38 |
private static final String CONNECTION_TYPE = |
|
39 |
"com.nokia.carbide.remoteconnections.tests.extensions.IntervalConnectionType"; |
|
40 |
private Random random = new Random(); |
|
41 |
private Set<IConnection2> connections = new HashSet<IConnection2>(); |
|
42 |
private IConnectionsManager manager = RemoteConnectionsActivator.getConnectionsManager(); |
|
43 |
||
44 |
private final class DiscoveryThread extends Thread { |
|
45 |
private static final int MAX = 60000; |
|
46 |
private static final int MIN = 10000; |
|
47 |
private volatile boolean keepRunning; |
|
48 |
||
49 |
public void run() { |
|
50 |
keepRunning = true; |
|
51 |
while (keepRunning) { |
|
52 |
try { |
|
53 |
sleep(getRandomCreationIntervalMs()); |
|
54 |
} catch (InterruptedException e) { |
|
55 |
keepRunning = false; |
|
56 |
} |
|
57 |
if (getRandomIntBetween(0, connections.size() + 1) == 0) { |
|
58 |
createNewConnection(); |
|
59 |
} |
|
60 |
else if (!connections.isEmpty()) { |
|
61 |
IConnection2 connection = connections.iterator().next(); |
|
62 |
connections.remove(connection); |
|
63 |
manager.disconnect(connection); |
|
64 |
} |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
private int getRandomCreationIntervalMs() { |
|
69 |
return getRandomIntBetween(MIN, MAX); |
|
70 |
} |
|
71 |
||
72 |
public void stopRunning() { |
|
73 |
keepRunning = false; |
|
74 |
} |
|
75 |
} |
|
76 |
||
77 |
private DiscoveryThread thread = new DiscoveryThread(); |
|
78 |
||
79 |
public URL getInformation() { |
|
80 |
return null; |
|
81 |
} |
|
82 |
||
83 |
private void createNewConnection() { |
|
84 |
IConnectionType connectionType = |
|
85 |
RemoteConnectionsActivator.getConnectionTypeProvider().getConnectionType(CONNECTION_TYPE); |
|
86 |
IConnectionFactory factory = connectionType.getConnectionFactory(); |
|
87 |
Map<String, String> settings = factory.getSettingsFromUI(); |
|
88 |
String val = getRandomIntervalString(); |
|
89 |
settings.put(IntervalConnectionType.KEY, val); |
|
90 |
IConnection2 connection = (IConnection2) factory.createConnection(settings); |
|
91 |
connection.setDisplayName(connection.getConnectionType().getDisplayName() + " " + val + " ms"); |
|
92 |
connection.setDynamic(true); |
|
93 |
connections.add(connection); |
|
94 |
manager.addConnection(connection); |
|
762
360ece94cbb4
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
719
diff
changeset
|
95 |
manager.setCurrentConnection(connection); |
719 | 96 |
} |
97 |
||
98 |
private String getRandomIntervalString() { |
|
99 |
int r = getRandomIntBetween(1000, 30000); |
|
100 |
return Integer.toString(r); |
|
101 |
} |
|
102 |
||
103 |
private int getRandomIntBetween(int min, int max) { |
|
104 |
return (Math.abs(random.nextInt()) % (max - min)) + min; |
|
105 |
} |
|
106 |
||
107 |
public void start() throws CoreException { |
|
108 |
if (TestFilter.isTest) |
|
109 |
thread.start(); |
|
110 |
} |
|
111 |
||
112 |
public void stop() throws CoreException { |
|
113 |
thread.stopRunning(); |
|
114 |
} |
|
115 |
||
116 |
} |