Reactive approach to simple imperative task - system.reactive

Application requirements:
subscribe to two event streams A and B
for each A event there should be corresponding B event some time later
the application monitors A events and alerts if no corresponding B arrives (in time)
B events can arrive in a different order from the A events (but only after), late, or not at all
This is simple in a traditional approach:
record each A event in a collection
remove the A event when a corresponding B event arrives
monitor the collection for A events that don't receive a B within timeout, and generate alerts
I would like to try a reactive solution but don't know which operators would express this best. I visualise event streams (Observables):
stream of A
stream of B
perhaps also a stream of timer ticks (unless the timing operators cater for this)
with the final output being a set of alerts:
someObservable.
...incantations, other observables...
.subscribe ((EventA a) -> raiseAlertForMissingB (a));
Is there an elegant reactive approach, or is this just not a good fit for rx?
(subsequent addition to original question)
The ascii art marble diagram could look like this
A stream A1----A2----------A3------------------A4--------------A5---------
B stream ------------B1------------B3---------------B4------------B5-----
(A2 TIMEOUT)
merged ------------A1B1----------A3B3------A2??---A4B4----------A5B5---
The subscriber receives tuples from the merged stream. If the tuple is a matching A and B event, it gets logged, but if the tuple is an A event without matching B event (shown as A2?? in the marble diagram) the the subscriber raises an alert. Viola!
But how to trigger the timed wait for a matching B event for each A event?
(Another addition)
To illustrate the 4th requirement dot point section "B events can arrive in a different order from the A events (but only after)..."
A stream A6----A7----------A8----------------------
B stream ------------B7------------B6-------B8-----
merged ------------A7B7----------A6B6-----A8B8---
Update 23/2/2016
I am testing the proposed solutions ported to Java.
Solution: Enigmativity
public class Test06Enigmativity {
private static final long A_PERIOD = 400;
private static final long B_PERIOD = 500;
private static final int TIMEOUT = 4_000;
private static final int[] bOrder = {
0, 1, 2,
4,
3, // out of order
6,
5, // out of order
7, 8,
10, 11, 12, 13, 14,
9, // out of order
15
};
private final long startTime = System.currentTimeMillis ();
public static void main (final String[] args) {
Test06Enigmativity app = new Test06Enigmativity ();
app.runEnigmativity ();
}
private void runEnigmativity () {
Observable<Long> aStream =
Observable.interval (A_PERIOD, TimeUnit.MILLISECONDS)
.doOnNext (seq -> {
output (" A%s", seq);
}).take (bOrder.length);
Observable<Long> bStream =
Observable.interval (B_PERIOD, TimeUnit.MILLISECONDS)
.map (seq -> {
long aId = (long) bOrder[seq.intValue ()];
output (" B%s", aId);
return aId;
})
.take (bOrder.length);
monitorEnigmativity (aStream, bStream, TIMEOUT)
.subscribe (this::output);
try {
Thread.sleep (60_000);
} catch (InterruptedException e) {
}
}
private Observable<String> monitorEnigmativity (Observable<Long> aStream, Observable<Long> bStream, int thresholdMsec) {
return Observable.create (subscriber ->
bStream.publish (pb ->
Observable.merge (
aStream.map (ax ->
pb
.filter (pbx -> pbx.equals (ax))
.take (1)
.timeout (thresholdMsec, TimeUnit.MILLISECONDS, Observable.defer (
() -> {
output (" timeout on B%s", ax);
return Observable.just (-1L);
}
)).map (pbx -> String.format ("%s,%s", ax, pbx))
)
)
).subscribe (subscriber::onNext)
);
}
private void output (String format, Object... args) {
System.out.printf ("tid:%3d %4dms %s%n", Thread.currentThread ().getId (), System.currentTimeMillis () - startTime,
String.format (format, args));
}
}
This works fine with:
private static final long A_PERIOD = 400;
private static final long B_PERIOD = 500;
private static final int TIMEOUT = 4_000;
tid: 12 511ms A0
tid: 15 626ms B0
tid: 15 626ms 0,0
tid: 12 907ms A1
tid: 15 1126ms B1
tid: 15 1126ms 1,1
tid: 12 1307ms A2
tid: 15 1626ms B2
tid: 15 1626ms 2,2
tid: 12 1707ms A3
tid: 12 2107ms A4
tid: 15 2126ms B4
tid: 15 2126ms 4,4
tid: 12 2507ms A5
tid: 15 2625ms B3
tid: 15 2625ms 3,3
tid: 12 2907ms A6
tid: 15 3126ms B6
tid: 15 3126ms 6,6
tid: 12 3307ms A7
tid: 15 3626ms B5
tid: 15 3626ms 5,5
tid: 12 3707ms A8
tid: 12 4107ms A9
tid: 15 4126ms B7
tid: 15 4126ms 7,7
tid: 12 4507ms A10
tid: 15 4626ms B8
tid: 15 4626ms 8,8
tid: 12 4908ms A11
tid: 15 5127ms B10
tid: 15 5128ms 10,10
tid: 12 5307ms A12
tid: 15 5626ms B11
tid: 15 5626ms 11,11
tid: 12 5707ms A13
tid: 12 6107ms A14
tid: 15 6126ms B12
tid: 15 6126ms 12,12
tid: 12 6507ms A15
tid: 15 6626ms B13
tid: 15 6626ms 13,13
tid: 13 7109ms timeout on B9
tid: 13 7114ms 9,-1
tid: 15 7126ms B14
tid: 15 7126ms 14,14
tid: 15 7625ms B9
tid: 15 8126ms B15
tid: 15 8126ms 15,15
But with B's arriving close to the A's, events can be missed:
private static final long A_PERIOD = 400;
private static final long B_PERIOD = 410;
private static final int TIMEOUT = 2_000;
tid: 12 509ms A0
tid: 15 538ms B0
tid: 12 905ms A1
tid: 15 948ms B1
tid: 15 948ms 1,1
tid: 12 1305ms A2
tid: 15 1358ms B2
tid: 15 1358ms 2,2
tid: 12 1706ms A3
tid: 15 1768ms B4
tid: 12 2105ms A4
tid: 15 2178ms B3
tid: 15 2178ms 3,3
tid: 12 2505ms A5
tid: 16 2538ms timeout on B0
tid: 16 2544ms 0,-1
tid: 15 2588ms B6
tid: 12 2905ms A6
tid: 15 2998ms B5
tid: 15 2998ms 5,5
tid: 12 3305ms A7
tid: 15 3408ms B7
tid: 15 3408ms 7,7
tid: 12 3705ms A8
tid: 15 3817ms B8
tid: 15 3817ms 8,8
tid: 12 4105ms A9
tid: 14 4106ms timeout on B4
tid: 14 4106ms 4,-1
tid: 15 4228ms B10
tid: 12 4505ms A10
tid: 15 4638ms B11
tid: 12 4905ms A11
tid: 16 4906ms timeout on B6
tid: 16 4906ms 6,-1
tid: 15 5048ms B12
tid: 12 5305ms A12
tid: 15 5457ms B13
tid: 12 5705ms A13
tid: 15 5868ms B14
tid: 12 6106ms A14
tid: 13 6107ms timeout on B9
tid: 13 6107ms 9,-1
tid: 15 6279ms B9
tid: 14 6510ms timeout on B10
tid: 12 6510ms A15
tid: 14 6510ms 10,-1
tid: 15 6688ms B15
tid: 15 6688ms 15,15
B0 arrived but yielded a timeout instead of a match. This is because the B0 event arrived before the A-stream's observer had a change to subscribe to the B-stream. I think with a hot B-stream, the prevailing approach of nested subscription is flawed. What is needed is some kind of limited-recent-replay stream - an application for a Subject?
Solution: Supertopi
package test;
import rx.Observable;
import java.util.concurrent.TimeUnit;
public class Test06Supertopi {
private static final long A_PERIOD = 400;
private static final long B_PERIOD = 500;
private static final int TIMEOUT = 3_000;
private static final int[] bOrder = {
0, 1, 2,
4,
3, // out of order
6,
5, // out of order
7, 8,
10, 11, 12, 13, 14,
9, // out of order
15
};
private final long startTime = System.currentTimeMillis ();
public static void main (final String[] args) {
Test06Supertopi app = new Test06Supertopi ();
app.runSupertopi ();
}
private void runSupertopi () {
Observable<Long> aStream =
Observable.interval (A_PERIOD, TimeUnit.MILLISECONDS)
.doOnNext (seq -> {
output (" A%s", seq);
}).take (bOrder.length);
Observable<Long> bStream =
Observable.interval (B_PERIOD, TimeUnit.MILLISECONDS)
.map (seq -> {
long aId = (long) bOrder[seq.intValue ()];
output (" B%s", aId);
return aId;
})
.take (bOrder.length);
monitorSupertopi (aStream, bStream, TIMEOUT)
.subscribe (this::output);
try {
Thread.sleep (60_000);
} catch (InterruptedException e) {
}
}
private Observable<String> monitorSupertopi (Observable<Long> aStream, Observable<Long> bStream, int thresholdMsec) {
return Observable.create (subscriber -> {
Observable<Long> a = aStream.publish ().refCount ();
Observable<Long> b = bStream.publish ().refCount ();
a.subscribe ((Long aId) -> {
Observable.merge (
Observable.timer (thresholdMsec, TimeUnit.MILLISECONDS)
.doOnNext (x -> {
output (" timeout on B%s", aId);
})
.map (x -> String.format ("%s,%s", aId, -1L)),
b.filter ((Long j) -> j.equals (aId))
.map ((Long pbx) -> String.format ("%s,%s", aId, pbx))
).take (1)
.subscribe (subscriber::onNext);
});
});
}
private void output (String format, Object... args) {
System.out.printf ("tid:%3d %4dms %s%n", Thread.currentThread ().getId (), System.currentTimeMillis () - startTime,
String.format (format, args));
}
}
Works fine:
private static final long A_PERIOD = 400;
private static final long B_PERIOD = 500;
private static final int TIMEOUT = 4_000;
tid: 14 522ms A0
tid: 14 922ms A1
tid: 16 1054ms B0
tid: 16 1055ms 0,0
tid: 14 1322ms A2
tid: 16 1555ms B1
tid: 16 1555ms 1,1
tid: 14 1721ms A3
tid: 16 2055ms B2
tid: 16 2055ms 2,2
tid: 14 2122ms A4
tid: 14 2522ms A5
tid: 16 2555ms B4
tid: 16 2555ms 4,4
tid: 14 2922ms A6
tid: 16 3055ms B3
tid: 16 3055ms 3,3
tid: 14 3322ms A7
tid: 16 3555ms B6
tid: 16 3555ms 6,6
tid: 14 3722ms A8
tid: 16 4055ms B5
tid: 16 4055ms 5,5
tid: 14 4122ms A9
tid: 14 4522ms A10
tid: 16 4555ms B7
tid: 16 4555ms 7,7
tid: 14 4922ms A11
tid: 16 5055ms B8
tid: 16 5055ms 8,8
tid: 14 5322ms A12
tid: 16 5555ms B10
tid: 16 5556ms 10,10
tid: 14 5723ms A13
tid: 16 6056ms B11
tid: 16 6057ms 11,11
tid: 14 6122ms A14
tid: 14 6522ms A15
tid: 16 6555ms B12
tid: 16 6555ms 12,12
tid: 16 7055ms B13
tid: 16 7055ms 13,13
tid: 13 7125ms timeout on B9
tid: 13 7125ms 9,-1
tid: 16 7555ms B14
tid: 16 7555ms 14,14
tid: 16 8055ms B9
tid: 16 8555ms B15
tid: 16 8555ms 15,15
But with closer timings, the initial B events are received twice. I am still investigating this.
private static final long A_PERIOD = 400;
private static final long B_PERIOD = 410;
private static final int TIMEOUT = 2_000;
tid: 14 539ms A0
tid: 14 939ms A1
tid: 16 983ms B0
tid: 16 983ms 0,0
tid: 14 1339ms A2
tid: 16 1393ms B1
tid: 16 1393ms 1,1
tid: 14 1739ms A3
tid: 16 1803ms B2
tid: 16 1803ms 2,2
tid: 14 2139ms A4
tid: 16 2213ms B4
tid: 16 2213ms 4,4
tid: 14 2539ms A5
tid: 16 2623ms B3
tid: 16 2623ms 3,3
tid: 14 2939ms A6
tid: 16 3032ms B6
tid: 16 3032ms 6,6
tid: 14 3339ms A7
tid: 16 3443ms B5
tid: 16 3443ms 5,5
tid: 14 3739ms A8
tid: 16 3852ms B7
tid: 16 3852ms 7,7
tid: 14 4139ms A9
tid: 16 4263ms B8
tid: 16 4263ms 8,8
tid: 14 4539ms A10
tid: 16 4672ms B10
tid: 16 4672ms 10,10
tid: 14 4939ms A11
tid: 16 5083ms B11
tid: 16 5083ms 11,11
tid: 14 5339ms A12
tid: 16 5493ms B12
tid: 16 5493ms 12,12
tid: 14 5739ms A13
tid: 16 5903ms B13
tid: 16 5903ms 13,13
tid: 14 6139ms A14
tid: 13 6140ms timeout on B9
tid: 13 6140ms 9,-1
tid: 16 6313ms B14
tid: 16 6313ms 14,14
tid: 14 6539ms A15
tid: 14 6950ms B0
tid: 14 7360ms B1
tid: 14 7770ms B2
tid: 14 8180ms B4
tid: 13 8540ms timeout on B15
tid: 13 8540ms 15,-1
24/2/2016
Solution: Lee Campbell
package test;
import rx.Observable;
import rx.subjects.PublishSubject;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class Test06LeeCampbell {
private static final int TIMEOUT = 500;
class ScheduledEvent {
final String type;
final long aId;
final long atMsec;
volatile boolean expired;
public ScheduledEvent (long atMsec, String type, long aId) {
this.atMsec = atMsec;
this.type = type;
this.aId = aId;
}
}
ScheduledEvent[] scheduledEvents = {
new ScheduledEvent (10, "A", 0),
new ScheduledEvent (90, "B", 0),
new ScheduledEvent (110, "A", 1),
new ScheduledEvent (140, "B", 1),
new ScheduledEvent (190, "A", 2),
new ScheduledEvent (270, "B", 2),
new ScheduledEvent (310, "A", 3),
new ScheduledEvent (410, "A", 4),
new ScheduledEvent (440, "B", 4),
new ScheduledEvent (480, "B", 3),
new ScheduledEvent (510, "A", 5),
new ScheduledEvent (610, "A", 6),
//new ScheduledEvent (670, "B", 6),
new ScheduledEvent (710, "A", 7),
new ScheduledEvent (810, "A", 8),
new ScheduledEvent (860, "B", 7),
new ScheduledEvent (880, "B", 8),
new ScheduledEvent (910, "A", 9),
new ScheduledEvent (1100, "A", 10),
new ScheduledEvent (1110, "A", 11),
new ScheduledEvent (1120, "A", 12),
new ScheduledEvent (1130, "A", 13),
new ScheduledEvent (1140, "A", 14),
//new ScheduledEvent (1200, "B", 10),
//new ScheduledEvent (1210, "B", 11),
//new ScheduledEvent (1220, "B", 12),
new ScheduledEvent (1230, "B", 13),
new ScheduledEvent (1240, "B", 14),
new ScheduledEvent (1390, "B", 9),
new ScheduledEvent (1450, "A", 15),
new ScheduledEvent (3290, "B", 5),
new ScheduledEvent (3350, "B", 15)
};
private final long startTime = System.currentTimeMillis ();
public static void main (final String[] args) {
Test06LeeCampbell app = new Test06LeeCampbell ();
app.runLeeCampbell ();
}
private void runLeeCampbell () {
Observable<Long> aStream =
getCrudeSequencer ("A")
.doOnNext (seq -> {
output (" A%s", seq);
});
Observable<Long> bStreamCold =
getCrudeSequencer ("B")
.doOnNext (seq -> {
output (" B%s", seq);
});
PublishSubject<Long> bStream = PublishSubject.create ();
bStreamCold.subscribe (bStream);
monitorLeeCampbell (aStream, bStream, TIMEOUT)
.subscribe (this::output);
pause (10_000);
}
private Observable<String> monitorLeeCampbell (Observable<Long> aStream, Observable<Long> bStream, int thresholdMsec) {
return aStream.flatMap (a ->
bStream.filter (b -> b.equals (a))
.map (b -> String.format ("%s,%s", a, b))
.take (1)
.timeout (thresholdMsec, TimeUnit.MILLISECONDS)
.onErrorResumeNext (
throwable -> {
output (" timeout on B%s", a);
if (!(throwable instanceof TimeoutException)) {
throw new RuntimeException (throwable);
}
return Observable.just (String.format ("%s,%s", a, -1L));
}
)
);
}
private void output (String format, Object... args) {
System.out.printf ("tid:%3d %4dms %s%n", Thread.currentThread ().getId (), getElapsedMsec (),
String.format (format, args));
}
private long getElapsedMsec () {
return System.currentTimeMillis () - startTime;
}
private Observable<Long> getCrudeSequencer (String name) {
return Observable.create (subscriber ->
new Thread (() -> {
for (ScheduledEvent se : scheduledEvents) {
if (se.type.equals (name)) {
while (getElapsedMsec () < se.atMsec) {
pause (1);
}
subscriber.onNext (Long.valueOf (se.aId));
se.expired = true;
} else {
// Timing is not reliable for sequencing two threads
while (!se.expired) {
pause (1);
}
}
}
subscriber.onCompleted ();
}).start ()
);
}
private static void pause (final int millis) {
try {
Thread.sleep (millis);
} catch (InterruptedException e) {
}
}
}
It worked
tid: 12 90ms A0
tid: 11 157ms B0
tid: 11 157ms 0,0
tid: 12 159ms A1
tid: 11 160ms B1
tid: 11 160ms 1,1
tid: 12 190ms A2
tid: 11 270ms B2
tid: 11 270ms 2,2
tid: 12 310ms A3
tid: 12 410ms A4
tid: 11 440ms B4
tid: 11 440ms 4,4
tid: 11 480ms B3
tid: 11 480ms 3,3
tid: 12 510ms A5
tid: 12 610ms A6
tid: 12 710ms A7
tid: 12 810ms A8
tid: 11 860ms B7
tid: 11 860ms 7,7
tid: 11 880ms B8
tid: 11 880ms 8,8
tid: 12 910ms A9
tid: 15 1011ms timeout on B5
tid: 15 1012ms 5,-1
tid: 12 1100ms A10
tid: 12 1110ms A11
tid: 16 1111ms timeout on B6
tid: 16 1111ms 6,-1
tid: 12 1120ms A12
tid: 12 1130ms A13
tid: 12 1140ms A14
tid: 11 1230ms B13
tid: 11 1230ms 13,13
tid: 11 1240ms B14
tid: 11 1240ms 14,14
tid: 11 1390ms B9
tid: 11 1390ms 9,9
tid: 12 1450ms A15
tid: 14 1601ms timeout on B10
tid: 14 1601ms 10,-1
tid: 15 1611ms timeout on B11
tid: 15 1611ms 11,-1
tid: 16 1621ms timeout on B12
tid: 16 1621ms 12,-1
tid: 19 1951ms timeout on B15
tid: 19 1951ms 15,-1
tid: 11 3290ms B5
tid: 11 3350ms B15

Assumptions:
the B stream is hot,
You have a return type that can represent a Match or a Mismatch (instead of using OnError/Timeout which will terminate the subscription)
Then this would be fine.
AStream.SelectMany(a =>
BStream.Where(b => b == a)
.Select(b => new MatchMade(a, b))
.Take(1)
.Timeout(matchTimeout)
.Catch<TimeoutException>(ex=>Observable.Return(new NoMatchMade(a)))
)

This should work. The types differ, I didn't want to guess your probably abstract datatypes. You can apply them pretty easily (function parameters, key comparison and Select statements)
Idea is that for every emited value from a, we take the first value emited either by b.Where(keys match) or timeout (presented by Observable.Timer) and make our Select based on this information.
I assumed that in timeout cases you also want on OnNext notification with some error provisioning mechanism.
private IObservable<string> MonitorAB(IObservable<long> a, IObservable<long> b,
TimeSpan threshold)
{
return Observable.Create<string>((obs) =>
{
a = a.Publish().RefCount();
b = b.Publish().RefCount();
return a.Subscribe(i =>
{
Observable.Merge(Observable.Timer(threshold).Select(_ => $"Timeout for A{i}"),
b.Where(j => j == i).Select(_ => $"Got matching B for A{i}"))
.Take(1)
.Subscribe(obs.OnNext);
});
});
}
I tested it like this.
private void Test()
{
var a = Observable.Interval(TimeSpan.FromSeconds(2)).Take(5);
var b = Observable.Interval(TimeSpan.FromSeconds(5)).Take(5);
MonitorAB( a, b, TimeSpan.FromSeconds(13)).Subscribe(Console.WriteLine);
}
EDIT:
to test the out of order case, you can flip the B stream e.g.
var b = Observable.Interval(TimeSpan.FromSeconds(2)).Select(i => 4 - i).Take(5);

I think this does what you want, albeit it is C# rather than Java - I'm sure you can convert easily enough.
private IObservable<string> MonitorAB(
IObservable<long> a, IObservable<long> b, TimeSpan threshold)
{
return Observable.Create<string>(o =>
b.Publish(pb =>
a.Select(ax =>
pb.Where(pbx => pbx == ax)
.Take(1)
.Timeout(threshold, Observable.Return(-1L))
.Select(pbx => String.Format("{0},{1}", ax, pbx)))
.Merge())
.Subscribe(o));
}
So this simply uses the inline .Publish to make sure b is hot within the query. The outer .Select filters the published pb observable for those that match the value from a (matching a & b), takes only one cause we only want one, and then does a .Timeout for the threshold time and returns a -1L (long) if the timeout is reached. The inner .Select just turns the two long values into a single string. At this point the query is an IObservable<IObservable<string>> so the .Merge flattens it out.

Related

AutoHotkey - limiting the number and type of characters taken from the string

I have an ahk script for an IRC client which after entering nick!ident#host into the text field and pressing F4 decrypts the ident which is the encrypted form of the IP address:
F4::
Clipboard =
Send ^a^x
ClipWait, 0
If ErrorLevel
MsgBox, 48, Error, An error occurred while waiting for the clipboard. Aborting.
Else Clipboard := decode(SubStr(Clipboard, -15, -8))
Return
decode(str) {
Static code := " " "
( LTrim Join`s
00 0x 02 03 04 0z 06 01 08 09 0B 0b 0c 0d 0e 0H x0 xx x2 x3 x4 xz x6 x1 x8 x9 xB xb xc xd xe xH 20 2x
22 23 24 2z 26 21 28 29 2B 2b 2c 2d 2e 2H 30 3x 32 33 34 3z 36 31 38 39 3B 3b 3c 3d 3e 3H 40 4x 42 43
44 4z 46 41 48 49 4B 4b 4c 4d 4e 4H z0 zx z2 z3 z4 zz z6 z1 z8 z9 zB zb zc zd ze zH 60 6x 62 63 64 6z
66 61 68 69 6B 6b 6c 6d 6e 6H 10 1x 12 13 14 1z 16 11 18 19 1B 1b 1c 1d 1e 1H 80 8x 82 83 84 8z 86 81
88 89 8B 8b 8c 8d 8e 8H 90 9x 92 93 94 9z 96 91 98 99 9B 9b 9c 9d 9e 9H B0 Bx B2 B3 B4 Bz B6 B1 B8 B9
BB Bb Bc Bd Be BH b0 bx b2 b3 b4 bz b6 b1 b8 b9 bB bb bc bd be bH c0 cx c2 c3 c4 cz c6 c1 c8 c9 cB cb
cc cd ce cH d0 dx d2 d3 d4 dz d6 d1 d8 d9 dB db dc dd de dH e0 ex e2 e3 e4 ez e6 e1 e8 e9 eB eb ec ed
ee eH H0 Hx H2 H3 H4 Hz H6 H1 H8 H9 HB Hb Hc Hd He HH
)"
Loop, % StrLen(str) / 2
new .= "." Round((Instr(code, " " SubStr(str, 2 * A_Index - 1, 2), True) - 1) / 3)
Return SubStr(new, 2)
}
Decryption is performed according to the following key:
https://pastebin.com/raw/P8cQtH2v
For example, for user data asdf!~z3040d4B#webchat will decrypt the ident from z3040d4B as 83.4.13.75 and copies this value to the clipboard.
But there are cases when the encoded form of the IP (ident) is longer or shorter than 8 characters or contains characters that aren't in the decryption key. Then it's impossible to decode the IP correctly. So I would like the script to copy the decryption result to the clipboard only if the retrieved string (between ! and #, omitting the ~ sign if present) is 8 characters long and contains the characters contained in the key I entered. Otherwise, the script should clear the clipboard. How to do it?
A regex match approach with e.g the regex !~?[A-z\d]{8}# is surely most convenient:
F4::
Clipboard := ""
SendInput, ^a^x
ClipWait, 0
if (ErrorLevel)
MsgBox, 48, Error, An error occurred while waiting for the clipboard. Aborting.
else if (Clipboard ~= "!~?[A-z\d]{8}#")
Clipboard := decode(SubStr(Clipboard, -15, -8))
else
Clipboard := ""
Return
decode(str)
{
static code := " " "
( LTrim Join`s
00 0x 02 03 04 0z 06 01 08 09 0B 0b 0c 0d 0e 0H x0 xx x2 x3 x4 xz x6 x1 x8 x9 xB xb xc xd xe xH 20 2x
22 23 24 2z 26 21 28 29 2B 2b 2c 2d 2e 2H 30 3x 32 33 34 3z 36 31 38 39 3B 3b 3c 3d 3e 3H 40 4x 42 43
44 4z 46 41 48 49 4B 4b 4c 4d 4e 4H z0 zx z2 z3 z4 zz z6 z1 z8 z9 zB zb zc zd ze zH 60 6x 62 63 64 6z
66 61 68 69 6B 6b 6c 6d 6e 6H 10 1x 12 13 14 1z 16 11 18 19 1B 1b 1c 1d 1e 1H 80 8x 82 83 84 8z 86 81
88 89 8B 8b 8c 8d 8e 8H 90 9x 92 93 94 9z 96 91 98 99 9B 9b 9c 9d 9e 9H B0 Bx B2 B3 B4 Bz B6 B1 B8 B9
BB Bb Bc Bd Be BH b0 bx b2 b3 b4 bz b6 b1 b8 b9 bB bb bc bd be bH c0 cx c2 c3 c4 cz c6 c1 c8 c9 cB cb
cc cd ce cH d0 dx d2 d3 d4 dz d6 d1 d8 d9 dB db dc dd de dH e0 ex e2 e3 e4 ez e6 e1 e8 e9 eB eb ec ed
ee eH H0 Hx H2 H3 H4 Hz H6 H1 H8 H9 HB Hb Hc Hd He HH
)"
Loop, % StrLen(str) / 2
{
if (!InStr(code, block := " " SubStr(str, 2 * A_Index - 1, 2), true))
return ""
new .= "." Round((InStr(code, block, true) - 1) / 3)
}
return SubStr(new, 2)
}
~=(docs) is the RegExMatch()(docs) shorthand.

HMAC implementation failure

I hope this is the right forum; I was not sure if I should ask this in stackoverflow, cryptography or security.
So my problem is that php's hash_hmac function is only available with php >=5.1.2. Because some servers are not updated to this version I wrote my own HMAC-implementaion based on php's hash function. But the code doesn't produce the same output as hash_hmac...
So where is my mistake?
define("HASH_ALGO", "sha512");
define("HMAC_BLOCKSIZE", 64);
function computeHMAC($message, $key) {
$ikey;
$okey;
$zero = hex2bin("00");
$ipad = hex2bin("36");
$opad = hex2bin("5C");
/*
* HMAC construction scheme:
* $ikey = $key padded with zeroes to blocksize and then each byte xored with 0x36
* $okey = $key padded with zeroes to blocksize and then each byte xored with 0x5C
* hash($okey . hash($ikey . $message))
*/
//Hash key if it is larger than HMAC_BLOCKSIZE
if (strlen($key) > HMAC_BLOCKSIZE) {
$key = hash(HASH_ALGO, $key, true);
}
//Fill ikey with zeroes
for ($i = 0; $i < HMAC_BLOCKSIZE; $i++) {
$ikey[$i] = $zero;
}
//Fill ikey with the real key
for ($i = 0; $i < strlen($key); $i++) {
$ikey[$i] = $key[$i];
}
//Until they get xored both keys are equal
$okey = $ikey;
//Xor both keys
for ($i = 0; $i < HMAC_BLOCKSIZE; $i++) {
$ikey[$i] ^= $ipad;
$okey[$i] ^= $opad;
}
//Build inner hash
$innerHash = hash(HASH_ALGO, $ikey . $message, true);
//Build outer hash
$outerHash = hash(HASH_ALGO, $okey . $innerHash, true);
return $outerHash;
}
The function was tested with the following code:
echo hexDump(computeHMAC("Testolope", "Testkeyolope"));
echo hexDump(hash_hmac(HASH_ALGO, "Testolope", "Testkeyolope", true));
The output is the following:
HexDump (64 Bytes):
65 a8 81 af 49 f2 49 c5 64 7a 7a b7 a6 ac a0 4e 9e 9b 1a 3c 76 fc 48 19 13 33 e0 f8 82 be 48 52 1a 50 49 09 1e fe bf 94 63 5f 9d 36 82 3f 2f a1 43 b4 60 9f 9f e5 d1 64 c6 5b 32 22 45 07 c9 cb
HexDump (64 Bytes):
d2 e9 52 d2 ab f0 db a7 60 e0 52 b0 5c 23 5a 73 d9 8c 78 8e 9e fb 26 82 54 7e f9 c8 f1 65 df 7f 97 44 fe 2b 1e 2b 6d d5 cb a4 ba c6 73 35 06 9c 0f c8 2d 36 8c b3 9b c4 48 01 5c c2 9f ce b4 08
The problem is that you've mixed up the digest size and block size; SHA-512 has a digest size of 64, but a block size of 128.
Secondly, both $ikey and $okey are arrays and not strings, so you need to convert them both into a string first:
$innerHash = hash(HASH_ALGO, join($ikey) . $message, true);
$outerHash = hash(HASH_ALGO, join($okey) . $innerHash, true);
That said, both hash() and hash_hmac() are documented as being available since 5.1.2, so I'm not sure what this will achieve :)

Printing a binary buffer in readable Hex format instead of ASCII format

I have a binary buffer $data which has some hex data and if I print it, I'd get this :
$VAR1 = '☺, ?♥☻♦\' N v ►☻ ☻ ◄☻ ↕♥ ‼♥ ¶♥ §* ☺♥☺♥☺#☺ ☺☺☺♠☺♠☺ ☺♦ ☺♀ ☺☻ ☺3
which obviously makes no sense to me. But it would be very helpful if I can print it as :
8F 00 8F 13 D0 21 A5 25 A3 DA CA 00 01 82 00 80 03 02 04 27 00 4E 00 76
instead.
I tried using sprintf("%x", $data), but that doesn't help.
Can someone help me?
Thanks!
If you're not too fussy on the format,
sprintf("%v02X", $bytes)
will give you
8F.00.8F.13....
If you really want
8F 00 8F 13 ...
Then the following are some options:
sprintf("%v02X", $bytes) =~ s/\./ /rg
join ' ', map { sprintf("%02X", ord($_)) } split(//, $bytes)
join ' ', unpack '(H2)*', $bytes
Use the ord function:
$VAR1 = '☺, ?♥☻♦\' N v ►☻ ☻ ◄☻ ↕♥ ‼♥ ¶♥ §* ☺♥☺♥☺#☺ ☺☺☺♠☺♠☺ ☺♦ ☺♀ ☺☻ ☺3';
print join ' ', map {sprintf("%x", ord)} split //, $VAR1;
Outputs:
e2 98 ba 2c 20 3f e2 99 a5 e2 98 bb e2 99 a6 27 20 4e 20 76 20 e2 96 ba e2 98 bb 20 e2 98 bb 20 e2 97 84 e2 98 bb 20 20 20 e2 86 95 e2 99 a5 20 20 20 20 e2 80 bc e2 99 a5 20 20 20 20 c2 b6 e2 99 a5 20 20 20 20 c2 a7 2a 20 e2 98 ba e2 99 a5 e2 98 ba e2 99 a5 e2 98 ba 40 e2 98 ba 20 e2 98 ba e2 98 ba e2 98 ba e2 99 a0 e2 98 ba e2 99 a0 e2 98 ba 20 e2 98 ba e2 99 a6 20 e2 98 ba e2 99 80 20 20 20 20 20 20 20 20 20 e2 98 ba e2 98 bb 20 e2 98 ba 33
Note: if your data is utf8, then you'll need to specify it as such:
263a 2c 20 3f 2665 263b 2666 27 20 4e 20 76 20 25ba 263b 20 263b 20 25c4 263b 20 20 20 2195 2665 20 20 20 20 203c 2665 20 20 20 20 b6 2665 20 20 20 20 a7 2a 20 263a 2665 263a 2665 263a 40 263a 20 263a 263a 263a 2660 263a 2660 263a 20 263a 2666 20 263a 2640 20 20 20 20 20 20 20 20 20 263a 263b 20 263a 33

Merge specific rows from two files if number in row file 1 is between two numbers in row in file 2

I'm searching for a couple of hours (actually already two days) but I can't find an answer to my problem yet. I've tried Sed and Awk but I can't get the parameters right.
Essentially, this is what I'm looking for
FOR every line in file_1
IF [value in colum2 in file_1]
IS EQUAL TO [value in column 4 in some row in file_2]
OR IS EQUAL TO [value in column 5 in some row in file_2]
OR IS BETWEEN [value column 4 and value column 5 in some row in file_2]
THAN
ADD column 3, 6 and 7 of some row of file_2 to column 3, 4 and 5 of file_1
NB: Values that needs to be compared are INTs, values in col 3, 6 and 7 (that only needs to be copied) are STRINGs
And this is the context, but probably not necessary to read:
I've two files with genome data which I want to merge in a specific way (the columns are tab separated)
The first file contains variants (only SNPs for the ones interested) of which, efficiently, only the second column is relevant. This column is a list of numbers (position of that variant on the chromosome)
I have a structural annotation files that contains the following data:
In column 4 is a begin position of the specific structure and in column 5 is the end position.
Column 3, 7 and 9 contains information that describes the specific structure (name of a gene etc.)
I would like to annotate the variants in the first file with the data in the annotation file. Therefore, if the number in column 2 of the variants file is equal to column 4 or 5 OR between those values in a specific row, columns 3, 7 and 9 of that specific row in the annotation needs to be added.
Sample File 1
SOME_NON_RELEVANT_STRING 142
SOME_NON_RELEVANT_STRING 182
SOME_NON_RELEVANT_STRING 320
SOME_NON_RELEVANT_STRING 321
SOME_NON_RELEVANT_STRING 322
SOME_NON_RELEVANT_STRING 471
SOME_NON_RELEVANT_STRING 488
SOME_NON_RELEVANT_STRING 497
SOME_NON_RELEVANT_STRING 541
SOME_NON_RELEVANT_STRING 545
SOME_NON_RELEVANT_STRING 548
SOME_NON_RELEVANT_STRING 4105
SOME_NON_RELEVANT_STRING 15879
SOME_NON_RELEVANT_STRING 26534
SOME_NON_RELEVANT_STRING 30000
SOME_NON_RELEVANT_STRING 30001
SOME_NON_RELEVANT_STRING 40001
SOME_NON_RELEVANT_STRING 44752
SOME_NON_RELEVANT_STRING 50587
SOME_NON_RELEVANT_STRING 87512
SOME_NON_RELEVANT_STRING 96541
SOME_NON_RELEVANT_STRING 99541
SOME_NON_RELEVANT_STRING 99871
Sample File 2
SOME_NON_RELEVANT_STRING SOME_NON_RELEVANT_STRING A1 0 38 B1 C1
SOME_NON_RELEVANT_STRING SOME_NON_RELEVANT_STRING A2 40 2100 B2 C2
SOME_NON_RELEVANT_STRING SOME_NON_RELEVANT_STRING A3 2101 9999 B3 C3
SOME_NON_RELEVANT_STRING SOME_NON_RELEVANT_STRING A4 10000 15000 B4 C4
SOME_NON_RELEVANT_STRING SOME_NON_RELEVANT_STRING A5 15001 30000 B5 C5
SOME_NON_RELEVANT_STRING SOME_NON_RELEVANT_STRING A6 30001 40000 B6 C6
SOME_NON_RELEVANT_STRING SOME_NON_RELEVANT_STRING A7 40001 50001 B7 C7
SOME_NON_RELEVANT_STRING SOME_NON_RELEVANT_STRING A8 50001 50587 B8 C8
SOME_NON_RELEVANT_STRING SOME_NON_RELEVANT_STRING A9 50588 83054 B9 C9
SOME_NON_RELEVANT_STRING SOME_NON_RELEVANT_STRING A10 83055 98421 B10 C10
SOME_NON_RELEVANT_STRING SOME_NON_RELEVANT_STRING A11 98422 99999 B11 C11
Sample output file
142 A2 B2 C2
182 A2 B2 C2
320 A2 B2 C2
321 A2 B2 C2
322 A2 B2 C2
471 A2 B2 C2
488 A2 B2 C2
497 A2 B2 C2
541 A2 B2 C2
545 A2 B2 C2
548 A2 B2 C2
4105 A3 B3 C3
15879 A5 B5 C5
26534 A5 B5 C5
30000 A5 B5 C5
30001 A6 B6 C6
40001 A7 B7 C7
44752 A7 B7 C7
50587 A8 B8 C8
87512 A10 B10 C10
96541 A10 B10 C10
99541 A11 B11 C11
99871 A11 B11 C1
1
As a start, here's how to write the algorithm you posted in awk, assuming when you said "ADD" you meant "append" and assuming all lines in file1 have unique values of the 2nd field (ran against the sample input provided):
awk '
BEGIN{ FS=OFS="\t"; startIdx=1 }
NR==FNR {
if ($2 in seen) {
printf "%s on line %d, first seen on line %d\n", $2, NR, seen[$2] | "cat>&2"
}
else {
f2s[++endIdx] = $2
seen[$2] = NR
}
next
}
{
inBounds = 1
for (idx=startIdx; (idx<=endIdx) && inBounds; idx++) {
f2 = f2s[idx]
if (f2 >= $4) {
if (f2 <= $5) {
print f2, $3, $6, $7
}
else {
inBounds = 0
}
}
else {
startIdx = idx
}
}
}
' file1 file2
142 A2 B2 C2
182 A2 B2 C2
320 A2 B2 C2
321 A2 B2 C2
322 A2 B2 C2
471 A2 B2 C2
488 A2 B2 C2
497 A2 B2 C2
541 A2 B2 C2
545 A2 B2 C2
548 A2 B2 C2
4105 A3 B3 C3
15879 A5 B5 C5
26534 A5 B5 C5
30000 A5 B5 C5
30001 A6 B6 C6
40001 A7 B7 C7
44752 A7 B7 C7
50587 A8 B8 C8
87512 A10 B10 C10
96541 A10 B10 C10
99541 A11 B11 C11
99871 A11 B11 C11

S-Box used in AES 128 bit CFB mode of encryption

Can anybody give me the S-Box used for 128 bit AES CFB mode of encryption.
Will this S-Box be same for every 128 -bit AES CFB Implementation.
Thanks
Here it is:
| 0 1 2 3 4 5 6 7 8 9 a b c d e f
---|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|
00 |63 7c 77 7b f2 6b 6f c5 30 01 67 2b fe d7 ab 76
10 |ca 82 c9 7d fa 59 47 f0 ad d4 a2 af 9c a4 72 c0
20 |b7 fd 93 26 36 3f f7 cc 34 a5 e5 f1 71 d8 31 15
30 |04 c7 23 c3 18 96 05 9a 07 12 80 e2 eb 27 b2 75
40 |09 83 2c 1a 1b 6e 5a a0 52 3b d6 b3 29 e3 2f 84
50 |53 d1 00 ed 20 fc b1 5b 6a cb be 39 4a 4c 58 cf
60 |d0 ef aa fb 43 4d 33 85 45 f9 02 7f 50 3c 9f a8
70 |51 a3 40 8f 92 9d 38 f5 bc b6 da 21 10 ff f3 d2
80 |cd 0c 13 ec 5f 97 44 17 c4 a7 7e 3d 64 5d 19 73
90 |60 81 4f dc 22 2a 90 88 46 ee b8 14 de 5e 0b db
a0 |e0 32 3a 0a 49 06 24 5c c2 d3 ac 62 91 95 e4 79
b0 |e7 c8 37 6d 8d d5 4e a9 6c 56 f4 ea 65 7a ae 08
c0 |ba 78 25 2e 1c a6 b4 c6 e8 dd 74 1f 4b bd 8b 8a
d0 |70 3e b5 66 48 03 f6 0e 61 35 57 b9 86 c1 1d 9e
e0 |e1 f8 98 11 69 d9 8e 94 9b 1e 87 e9 ce 55 28 df
f0 |8c a1 89 0d bf e6 42 68 41 99 2d 0f b0 54 bb 16
It can also be found in FIPS Pub 197, the official standard.
And yes, it is exactly the same for every implementation of AES. Otherwise you wouldn't be able to encrypt something others could decrypt or vice-versa.