in ironfleet/src/IronSHTClient/Client.cs [435:576]
public void Experiment()
{
ulong requestKey;
int serverIdx = 0;
scheduler = IoScheduler.CreateClient(serviceIdentity.Servers, ps.Verbose);
byte[] myPublicKey = IoScheduler.GetCertificatePublicKey(scheduler.MyCert);
ulong seqNum = 0;
// Test the functionality of the Sharding
if (ps.Workload == 'f')
{
// A delegation can delegate at most 61 keys, so make sure
// there can't be that many keys in the range by having the
// range be smaller than 61.
ulong k_lo = 125;
ulong k_hi = 175;
requestKey = 150;
var recipient = serviceIdentity.Servers[(serverIdx + 1) % serviceIdentity.Servers.Count()];
seqNum++;
var msg = new GetRequestMessage(seqNum, myPublicKey, requestKey);
this.Send(msg, serviceIdentity.Servers[serverIdx].PublicKey);
ReceiveReply(serverIdx, myPublicKey, requestKey, false);
seqNum++;
Console.WriteLine("Sending a Shard request with a sequence number {0}", seqNum);
var shardMessage = new ShardRequestMessage(seqNum, myPublicKey, k_lo, k_hi, recipient.PublicKey);
this.Send(shardMessage, serviceIdentity.Servers[serverIdx].PublicKey);
ReceiveReply(serverIdx, myPublicKey, requestKey, true);
Thread.Sleep(5000);
Console.WriteLine("Sending a GetRequest after a Shard, expect a redirect");
seqNum++;
msg = new GetRequestMessage(seqNum, myPublicKey, requestKey);
this.Send(msg, serviceIdentity.Servers[(serverIdx + 0) % serviceIdentity.Servers.Count()].PublicKey);
ReceiveReply(serverIdx, myPublicKey, requestKey, false, expectRedirect: true);
Thread.Sleep(5000);
Console.WriteLine("Sending a GetRequest after a Shard to the second host, expect a reply");
// Must use sequence number 1 since this is the first message
// to this server.
msg = new GetRequestMessage(1, myPublicKey, requestKey);
this.Send(msg, serviceIdentity.Servers[(serverIdx + 1) % serviceIdentity.Servers.Count()].PublicKey);
ReceiveReply((serverIdx + 1) % serviceIdentity.Servers.Count(), myPublicKey, requestKey, false);
Console.WriteLine("Successfully received reply");
return;
}
// Run an actual workload
while (true)
{
seqNum++;
var receivedReply = false;
requestKey = seqNum % (ulong)ps.NumKeys;
MessageBase msg;
if (ps.Workload == 'g')
{
msg = new GetRequestMessage(seqNum, myPublicKey, requestKey);
}
else
{
msg = new SetRequestMessage(seqNum, myPublicKey, requestKey, (ulong)ps.ValueSize);
}
var startTime = HiResTimer.Ticks;
this.Send(msg, serviceIdentity.Servers[serverIdx].PublicKey);
// Wait for the reply
while (!receivedReply)
{
byte[] bytes = Receive();
if (bytes == null) {
//serverIdx = (serverIdx + 1) % serviceIdentity.Servers.Count();
//Console.WriteLine("#timeout; rotating to server {0}", serverIdx);
Console.WriteLine("#timeout; retrying {0}", serverIdx);
this.Send(msg, serviceIdentity.Servers[serverIdx].PublicKey);
continue;
}
var endTime = HiResTimer.Ticks;
if (bytes.Length == 16)
{
//Ignore acks
}
else if (bytes.Length >= 56)
{
var replySeqNum = ExtractBE64(bytes, offset: 8);
if (ps.Verbose)
{
Console.WriteLine("Reply sequence number : {0}", replySeqNum);
Console.WriteLine("Client {0}: Sending an ack with sequence number {1} to {2}",
id, replySeqNum, serviceIdentity.Servers[serverIdx]);
}
if (seqNum % 100 == 0)
{
var ack_msg = new AckMessage(replySeqNum);
this.Send(ack_msg, serviceIdentity.Servers[serverIdx].PublicKey);
}
int publicKeyLength = Convert.ToInt32(ExtractBE64(bytes, offset: 16));
if (bytes.Length < publicKeyLength + 40)
{
Console.WriteLine("ERROR - Received too-short message (size {0} not long enough for public key of length {1})",
bytes.Length, publicKeyLength);
}
else
{
var replyKey = ExtractBE64(bytes, offset: 32 + publicKeyLength);
// Need to send an ack
if (ps.Verbose)
{
Console.WriteLine("Request key : {0}", requestKey);
Console.WriteLine("Reply key : {0}", replyKey);
Console.WriteLine("Got packet length: {0}", bytes.Length);
}
// key is the same as the sequence number
if (replyKey == requestKey)
{
receivedReply = true;
Console.WriteLine("#req {0} {1} {2}",
id,
seqNum,
HiResTimer.TicksToMilliseconds(endTime - startTime));
}
}
}
else {
Console.WriteLine("Received packet of unexpected length {0}", bytes.Length);
}
}
}
}