Skip to content

Commit

Permalink
THE KING IS DEAD. LONG LIVE THE KING
Browse files Browse the repository at this point in the history
  • Loading branch information
zadjii-msft committed Oct 23, 2020
1 parent 53e4237 commit 6ffba71
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 26 deletions.
25 changes: 19 additions & 6 deletions src/tools/MonarchPeasantSample/Monarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace winrt::MonarchPeasantSample::implementation
Monarch::Monarch()
{
printf("Instantiated a Monarch\n");
_peasants = winrt::single_threaded_observable_vector<winrt::MonarchPeasantSample::Peasant>();
_peasants = winrt::single_threaded_observable_map<uint64_t, winrt::MonarchPeasantSample::IPeasant>();
}
Monarch::~Monarch()
{
Expand All @@ -26,11 +26,24 @@ namespace winrt::MonarchPeasantSample::implementation
dtored = true;
cv.notify_one();
}
uint64_t Monarch::AddPeasant(winrt::MonarchPeasantSample::Peasant peasant)
uint64_t Monarch::AddPeasant(winrt::MonarchPeasantSample::IPeasant peasant)
{
_peasants.Append(peasant);
peasant.AssignID(++_nextPeasantID);
printf("Added a new peasant, assigned them the ID=%d\n", _nextPeasantID);
return _nextPeasantID;
// TODO: This whole algo is terrible. There's gotta be a better way of
// finding the first opening in a non-consecutive map of int->object
auto providedID = peasant.GetID();

if (providedID == 0)
{
peasant.AssignID(_nextPeasantID++);
printf("Assigned the peasant the ID %lld\n", peasant.GetID());
}
else
{
printf("Peasant already had an ID, %lld\n", peasant.GetID());
_nextPeasantID = providedID >= _nextPeasantID ? providedID + 1 : _nextPeasantID;
}
_peasants.Insert(peasant.GetID(), peasant);
printf("(the next new peasant will get the ID %lld)\n", _nextPeasantID);
return peasant.GetID();
}
}
8 changes: 4 additions & 4 deletions src/tools/MonarchPeasantSample/Monarch.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ namespace winrt::MonarchPeasantSample::implementation
Monarch();
~Monarch();

uint64_t AddPeasant(winrt::MonarchPeasantSample::Peasant peasant);
uint64_t AddPeasant(winrt::MonarchPeasantSample::IPeasant peasant);
bool IsInSingleInstanceMode() { return false; }
winrt::MonarchPeasantSample::Peasant GetPeasant(uint64_t peasantID)
winrt::MonarchPeasantSample::IPeasant GetPeasant(uint64_t peasantID)
{
peasantID;
return nullptr;
}
winrt::MonarchPeasantSample::Peasant GetMostRecentPeasant() { return nullptr; }
winrt::MonarchPeasantSample::IPeasant GetMostRecentPeasant() { return nullptr; }

private:
uint64_t _nextPeasantID{ 1 };
Windows::Foundation::Collections::IObservableVector<winrt::MonarchPeasantSample::Peasant> _peasants{ nullptr };
Windows::Foundation::Collections::IObservableMap<uint64_t, winrt::MonarchPeasantSample::IPeasant> _peasants{ nullptr };
};
}

Expand Down
6 changes: 3 additions & 3 deletions src/tools/MonarchPeasantSample/Monarch.idl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace MonarchPeasantSample
{
Monarch();

UInt64 AddPeasant(Peasant peasant);
UInt64 AddPeasant(IPeasant peasant);
Boolean IsInSingleInstanceMode();
Peasant GetPeasant(UInt64 peasantID);
Peasant GetMostRecentPeasant();
IPeasant GetPeasant(UInt64 peasantID);
IPeasant GetMostRecentPeasant();
};
}
55 changes: 42 additions & 13 deletions src/tools/MonarchPeasantSample/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ bool areWeTheKing(const winrt::MonarchPeasantSample::Monarch& monarch, const boo
}
else
{
printf("we're a lowly peasant - the king is %d\n", kingPID);
printf("We're a lowly peasant - the king is %d\n", kingPID);
}
}
return (ourPID == kingPID);
Expand All @@ -112,7 +112,7 @@ void printPeasants(const winrt::MonarchPeasantSample::Monarch& monarch)
printf("This is unimplemented\n");
}

void monarchAppLoop(const winrt::MonarchPeasantSample::Monarch& monarch,
bool monarchAppLoop(const winrt::MonarchPeasantSample::Monarch& monarch,
const winrt::MonarchPeasantSample::IPeasant& peasant)
{
bool exitRequested = false;
Expand All @@ -130,7 +130,9 @@ void monarchAppLoop(const winrt::MonarchPeasantSample::Monarch& monarch,
exitRequested = true;
}
}
return true;
}

bool peasantReadInput(const winrt::MonarchPeasantSample::Monarch& monarch,
const winrt::MonarchPeasantSample::IPeasant& peasant)
{
Expand Down Expand Up @@ -181,14 +183,14 @@ bool peasantReadInput(const winrt::MonarchPeasantSample::Monarch& monarch,

return false;
}
void peasantAppLoop(const winrt::MonarchPeasantSample::Monarch& monarch,
bool peasantAppLoop(const winrt::MonarchPeasantSample::Monarch& monarch,
const winrt::MonarchPeasantSample::IPeasant& peasant)
{
wil::unique_handle hMonarch{ OpenProcess(PROCESS_ALL_ACCESS, FALSE, monarch.GetPID()) };
printf("handle for the monarch process is %d\n", hMonarch.get());
// printf("handle for the monarch process is %d\n", hMonarch.get());

g_hInput = GetStdHandle(STD_INPUT_HANDLE);
printf("handle for the console input is %d\n", g_hInput);
// printf("handle for the console input is %d\n", g_hInput);

HANDLE handlesToWaitOn[2]{ hMonarch.get(), g_hInput };

Expand All @@ -206,7 +208,8 @@ void peasantAppLoop(const winrt::MonarchPeasantSample::Monarch& monarch,
case WAIT_OBJECT_0 + 0:
printf("First event was signaled.\n");
printf("THE KING IS DEAD\n");
exitRequested = true;
// Return false here - this will trigger us to find the new monarch
return false;
break;

// handlesToWaitOn[1] was signaled
Expand All @@ -231,22 +234,48 @@ void peasantAppLoop(const winrt::MonarchPeasantSample::Monarch& monarch,
}

printf("Bottom of peasantAppLoop\n");
return true;
}

void app()
{
registerAsMonarch();
bool exitRequested = false;

auto monarch = instantiateAMonarch();
const bool isMonarch = areWeTheKing(monarch, true);
bool isMonarch = areWeTheKing(monarch, true);
auto peasant = getOurPeasant(monarch);

if (isMonarch)
{
monarchAppLoop(monarch, peasant);
}
else
while (!exitRequested)
{
peasantAppLoop(monarch, peasant);
if (isMonarch)
{
exitRequested = monarchAppLoop(monarch, peasant);
}
else
{
exitRequested = peasantAppLoop(monarch, peasant);
if (!exitRequested)
{
monarch = instantiateAMonarch();
isMonarch = areWeTheKing(monarch, true);
printf("LONG LIVE THE KING\n");
if (isMonarch)
{
// TODO: If we've been elected monarch, then we need to make
// sure that we're one of the listed peasants (right?) We
// probably don't _only_ need to do this during the
// election, we probably _always need to do this.
//
// we'll loop back into the monarchAppLoop
}
else
{
// Add us to the new monarch
monarch.AddPeasant(peasant);
}
}
}
}
}

Expand Down

1 comment on commit 6ffba71

@github-actions

This comment was marked as outdated.

Please sign in to comment.