Skip to content

Commit

Permalink
Some code cleanup, commenting
Browse files Browse the repository at this point in the history
  • Loading branch information
zadjii-msft committed Nov 4, 2020
1 parent 891d0bd commit b8aea85
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
12 changes: 2 additions & 10 deletions src/tools/MonarchPeasantSample/Monarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,16 @@ using namespace winrt;
using namespace winrt::Windows::Foundation;
using namespace ::Microsoft::Console;

extern std::mutex m;
extern std::condition_variable cv;
extern bool dtored;

namespace winrt::MonarchPeasantSample::implementation
{
Monarch::Monarch()
{
printf("Instantiated a Monarch\n");
// _peasants = winrt::single_threaded_observable_map<uint64_t, winrt::MonarchPeasantSample::IPeasant>();
}

Monarch::~Monarch()
{
printf("~Monarch()\n");
std::unique_lock<std::mutex> lk(m);
dtored = true;
cv.notify_one();
}

uint64_t Monarch::GetPID()
Expand All @@ -35,8 +27,8 @@ namespace winrt::MonarchPeasantSample::implementation

uint64_t Monarch::AddPeasant(winrt::MonarchPeasantSample::IPeasant peasant)
{
// 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
// TODO: This whole algorithm 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)
Expand Down
33 changes: 25 additions & 8 deletions src/tools/MonarchPeasantSample/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ using namespace winrt::Windows::Foundation;
using namespace ::Microsoft::Console;

////////////////////////////////////////////////////////////////////////////////
std::mutex m;
std::condition_variable cv;
bool dtored = false;
// This seems liks a hack, but it works.
//
// This class factory works so that there's only ever one instance of a Monarch
// per-process. Once the first monarch is created, we'll stash it in g_weak.
// Future callers who try to instantiate a Monarch will get the one that's
// already been made.
//
winrt::weak_ref<MonarchPeasantSample::implementation::Monarch> g_weak{ nullptr };

struct MonarchFactory : implements<MonarchFactory, IClassFactory>
Expand Down Expand Up @@ -52,6 +56,9 @@ struct MonarchFactory : implements<MonarchFactory, IClassFactory>
};
////////////////////////////////////////////////////////////////////////////////

// Function Description:
// - Register the Monarch object with COM. This allows other processes to create
// Monarch's in our process space with CoCreateInstance and the Monarch_clsid.
DWORD registerAsMonarch()
{
DWORD registrationHostClass{};
Expand All @@ -63,6 +70,11 @@ DWORD registerAsMonarch()
return registrationHostClass;
}

// Function Description:
// - Called when the old monarch dies. Create a new connection to the new
// monarch. This might be us! If we're the new monarch, then update the
// Monarch to know which Peasant it came from. Otherwise, tell the new monarch
// that we exist.
void electNewMonarch(AppState& state)
{
state._monarch = AppState::instantiateAMonarch();
Expand All @@ -83,9 +95,6 @@ void electNewMonarch(AppState& state)

void appLoop(AppState& state)
{
// Tricky - first, we have to ask the monarch to handle the commandline.
// They will tell us if we need to create a peasant.

auto dwRegistration = registerAsMonarch();
// IMPORTANT! Tear down the registration as soon as we exit. If we're not a
// real peasant window (the monarch passed our commandline to someone else),
Expand All @@ -95,15 +104,22 @@ void appLoop(AppState& state)
check_hresult(CoRevokeClassObject(dwRegistration));
});

// state.createMonarchAndPeasant();
// Tricky - first, we have to ask the monarch to handle the commandline.
// They will tell us if we need to create a peasant.
state.createMonarch();

// processCommandline will return true if we should exit early.
if (state.processCommandline())
{
return;
}

bool isMonarch = state.areWeTheKing(true);
bool exitRequested = false;

// (monarch|peasant)AppLoop will return when they've run to completion. If
// they return true, then just exit the application (the user might have
// pressed 'q' to exit). If the peasant returns false, then it detected the
// monarch died. Attempt to elect a new one.
while (!exitRequested)
{
if (isMonarch)
Expand All @@ -127,6 +143,7 @@ int main(int argc, char** argv)
AppState state;
state.initializeState();

// Collect up all the commandline arguments
printf("args:[");
for (auto& elem : wil::make_range(argv, argc))
{
Expand Down

1 comment on commit b8aea85

@github-actions

This comment was marked as outdated.

Please sign in to comment.