Skip to content

Instantly share code, notes, and snippets.

@gaxar77
Created August 22, 2010 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaxar77/543717 to your computer and use it in GitHub Desktop.
Save gaxar77/543717 to your computer and use it in GitHub Desktop.
Slightly, well, a bit more than slightly buggy implementation and demonstration of interprocess communication without using named pipes, files, or sockets, just pure window messages.
//AppTalk.cpp
//Author: Guido Arbia
#include <istream>
#include "AppTalk.h"
namespace AppTalking
{
AppSenator::AppSenator(string &AppName)
{
InitLocking();
m_hReceiveEvent = NULL;
m_AppName = AppName;
if (!Augric::GetMainAugric()) throw new AugraException("An AppSenator uses an Augerate, and Augerates necesitate an Augric! No Augric was thus far created.");
m_Augerate = new Augerate(-1);
m_Augerate->SetEraseBeforeProcess(true);
m_Augerate->AddMessageHandler(this);
}
AppSenator::~AppSenator()
{
vector<int> ports;
for (map<int, AppConnection>::iterator itr = connections.begin(); itr != connections.end(); itr++)
{
int nPort = (*itr).first;
ports.push_back(nPort);
}
for (vector<int>::iterator itr = ports.begin(); itr != ports.end(); itr++)
{
BreakBridge(*itr);
}
delete m_Augerate;
}
bool AppSenator::ProcessStringMessage(HWND hSource, string Message)
{
if (Message == string("name?", 5))
{
//while (!m_Augerate->CanWriteFresh(hSource));
m_Augerate->WriteString(hSource, string("name: ", 6) + m_AppName);
}
if (Message.length() > 6)
{
if (Message.substr(0, 6) == string("name: ", 6))
{
Lock();
m_RemoteAppName = Message.substr(6, Message.length() - 6);
if (m_hReceiveEvent) SetEvent(m_hReceiveEvent);
Unlock();
}
if (Message.substr(0, 6) == string("kill: ", 6))
{
string strPort = Message.substr(6, Message.length() - 6);
int nPort = atoi(strPort.c_str());
map<int, AppConnection>::iterator itr = connections.find(nPort);
if (itr != connections.end() && (*itr).second.GetRemoteAppSenator() == hSource)
{
connections.erase(itr);
}
}
if (Message.substr(0, 6) == string("init: ", 6))
{
string Params = Message.substr(6, Message.length() - 6);
int comma = Params.find(",");
string Port = Params.substr(0, comma);
string Window = Params.substr(comma+1, Params.length() - (comma+1));
HWND hWnd = (HWND) atoi(Window.c_str());
int nPort = atoi(Port.c_str());
Lock();
/* if (!connections[nPort].GetRemoteAppSenator())
{
return false;
} */
connections[nPort].SetOut(hWnd);
connections[nPort].SetRemoteAppSenator(hSource);
if (!connections[nPort].GetIn())
{
Augerate* na = new Augerate(-2);
connections[nPort].SetIn(na);
char num[10];
_itoa_s(nPort, num, 10);
Port = num;
_itoa_s((int) na->GetWindow(), num, 10);
Window = num;
string Send("init: " + Port + "," + Window);
m_Augerate->WriteString(hSource, Send);
}
else
{
if (m_hReceiveEvent) SetEvent(m_hReceiveEvent);
}
}
Unlock();
}
return false;
}
string AppSenator::GetRemoteAppName(HWND hAppSenatorWindow)
{
Lock();
m_hReceiveEvent = CreateEvent(NULL, false, false, NULL);
m_RemoteAppName.erase();
Unlock();
m_Augerate->WriteString(hAppSenatorWindow, string("name?", 5));
WaitForSingleObject(m_hReceiveEvent, 500);
CloseHandle(m_hReceiveEvent);
return m_RemoteAppName;
}
vector<HWND> AppSenator::GetAppSenatorWindows(bool bIncludeSelf)
{
Augric* augric = Augric::GetMainAugric();
vector<HWND> augerates = augric->GetGlobalAugerateWindows();
vector<HWND> senators;
for (vector<HWND>::iterator itr = augerates.begin(); itr != augerates.end(); itr++)
{
if (augric->GetAugerateProtocol(*itr) == -1)
if (bIncludeSelf)
senators.push_back(*itr);
else if (m_Augerate->GetWindow() != *itr)
senators.push_back(*itr);
}
return senators;
}
vector<HWND> AppSenator::GetAppSenatorWindowsByName(string AppName, bool bIncludeSelf)
{
vector<HWND> senators = GetAppSenatorWindows(bIncludeSelf);
vector<HWND> named;
for (vector<HWND>::iterator itr = senators.begin(); itr != senators.end(); itr++)
{
string RemoteName = GetRemoteAppName(*itr);
if (RemoteName == AppName) named.push_back(*itr);
}
return named;
}
AppConnection* AppSenator::Bridge(int port, HWND hAppSenatorWindow)
{
char num[10];
Augerate* aug = new Augerate(-2);
Lock();
connections[port].SetIn(aug);
Unlock();
_itoa_s(port, num, 10);
string strPort(num);
_itoa_s((int)aug->GetWindow(), num, 10);
string strWnd(num);
string Message = "init: " + strPort + "," + strWnd;
m_hReceiveEvent = CreateEvent(NULL, false, false, NULL);
m_Augerate->WriteString(hAppSenatorWindow, Message);
DWORD waitResult = WaitForSingleObject(m_hReceiveEvent, 500);
CloseHandle(m_hReceiveEvent);
if (waitResult == WAIT_TIMEOUT) return NULL;
return &connections[port];
}
void AppSenator::BreakBridge(int Port)
{
char num[10];
_itoa_s(Port, num, 10);
string strPort(num);
Lock();
map<int, AppConnection>::iterator itr = connections.find(Port);
if (itr == connections.end()) return;
HWND hRemote = (*itr).second.GetRemoteAppSenator();
if (hRemote != m_Augerate->GetWindow()) connections.erase(itr);
Unlock();
m_Augerate->WriteString(hRemote, "kill: " + strPort);
}
bool AppSenator::IsBridged(int Port)
{
Lock();
map<int, AppConnection>::iterator itr = connections.find(Port);
bool ret = itr != connections.end();
Unlock();
return ret;
}
AppConnection* AppSenator::GetConnectionFromPort(int Port)
{
Lock();
map<int, AppConnection>::iterator itr = connections.find(Port);
AppConnection* ret = itr != connections.end() ? &(*itr).second: NULL;
Unlock();
return ret;
}
vector<int> AppSenator::GetOpenPorts()
{
vector<int> ports;
Lock();
for (map<int, AppConnection>::iterator itr = connections.begin(); itr != connections.end(); itr++)
{
ports.push_back((*itr).first);
}
Unlock();
return ports;
}
void AppConnection::WriteString(string str)
{
m_In->WriteString(m_Out, str);
}
string AppConnection::ReadString()
{
return m_In->ReadString(m_Out);
}
}
//AppTalk.h
//Author: Guido Arbia
#ifndef _APPTALK
#define _APPTALK
#include <string>
#include "Augra.h"
using namespace Augra;
namespace AppTalking
{
class AppConnection
{
HWND m_Out;
Augerate *m_In;
HWND m_RemoteAppSenator;
public:
AppConnection() {m_Out = NULL; m_In = NULL; m_RemoteAppSenator = NULL;}
~AppConnection() {if (m_In) delete m_In;}
void SetOut(HWND hOut) {m_Out = hOut;}
void SetIn(Augerate* in) {m_In = in;}
HWND GetOut() {return m_Out;}
Augerate* GetIn() {return m_In;}
void SetRemoteAppSenator(HWND AppSenator) {m_RemoteAppSenator = AppSenator;}
HWND GetRemoteAppSenator() {return m_RemoteAppSenator;}
void WriteString(string str);
string ReadString();
};
class AppSenator : public AugerateMessageHandler, Lockable
{
string m_AppName;
Augerate *m_Augerate;
string m_RemoteAppName;
HANDLE m_hReceiveEvent;
map<int, AppConnection> connections;
public:
AppSenator(string &AppName);
~AppSenator();
bool ProcessStringMessage(HWND hSource, string Message);
string GetRemoteAppName(HWND hAppSenatorWindow);
HWND GetAugerateWindow() {return m_Augerate->GetWindow();}
vector<HWND> GetAppSenatorWindows(bool bIncludeSelf = false);
vector<HWND> GetAppSenatorWindowsByName(string AppName, bool bIncludeSelf);
AppConnection* Bridge(int port, HWND hAppSenatorWindow);
void BreakBridge(int port);
bool IsBridged(int port);
AppConnection* GetConnectionFromPort(int Port);
vector<int> GetOpenPorts();
};
}
#endif
//Augra.cpp
//Author: Guido Arbia
#include "Augra.h"
UINT TestMsg;
UINT AugricMsg_CreateAugerate;
UINT AugricMsg_DestroyAugerate;
UINT AugricMsg_Destruct;
UINT AugerateMsg_IsAugerate;
UINT AugerateMsg_ConfirmAugerate;
UINT AugerateMsg_GetProtocol;
UINT AugerateMsg_CanWriteFresh;
namespace Augra
{
Augric* Augric::AppAugric = 0;
bool Augric::bInitialized = false;
Augerate::Augerate(int protocol)
{
InitLocking();
bEraseBeforeProcess = false;
m_protocol = protocol;
Augric *aug = Augric::GetMainAugric();
if (!aug) throw new AugraException("No Augric created!");
hWindow = aug->CreateAugerateWindow();
if (!hWindow) throw new AugraException("Unable to create augerate window!");
//this->SetReceiverProc(NULL, NULL);
aug->RegisterAugerate(this);
}
Augerate::~Augerate()
{
Augric *aug = Augric::GetMainAugric();
if (!aug) throw new AugraException("No Augric Exists");
aug->DestroyAugerateWindow(hWindow);
aug->UnregisterAugerate(this);
}
LRESULT Augerate::ProcessWindowMessages(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
if (Msg == WM_COPYDATA)
{
HWND hSource = (HWND) wParam;
COPYDATASTRUCT *cds = (COPYDATASTRUCT*) lParam;
Lock();
data[hSource].append((char*) cds->lpData, cds->cbData);
if (data[hSource].length() && MessageHandlers.size())
{
//MessageBox(NULL, data[hSource].c_str(), "Hmm.", MB_OK);
map<HWND, string>::iterator itr = data.find(hSource);
if (ProcessStringMessages(hSource, (*itr).second))
{
itr = data.find(hSource);
if (itr != data.end())
data.erase(itr);
}
}
Unlock();
return TRUE;
}
if (Msg == AugerateMsg_IsAugerate)
{
SendMessage((HWND) wParam, AugerateMsg_ConfirmAugerate, (WPARAM) hWindow, NULL);
return 0;
}
if (Msg == AugerateMsg_GetProtocol)
{
Lock();
int pr = m_protocol;
Unlock();
return pr;
}
if (Msg == AugerateMsg_CanWriteFresh)
{
Lock();
map<HWND, string>::iterator itr = data.find((HWND) wParam);
Unlock();
if (itr == data.end()) return 1; else return 0;
}
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
bool Augerate::CanWriteFresh(HWND hAugerateWindow)
{
return SendMessage(hAugerateWindow, AugerateMsg_CanWriteFresh, (WPARAM) hWindow, NULL) ? true : false;
}
string Augerate::ReadString(HWND hAugerateWindow)
{
string retString;
retString.erase();
Lock();
map<HWND, string>::iterator itr = data.find(hAugerateWindow);
if (itr != data.end())
{
retString = (*itr).second;
data.erase(itr);
}
Unlock();
return retString;
}
void Augerate::WriteString(HWND hAugerateWindow, string& str)
{
COPYDATASTRUCT cds;
int size = str.length();
char *ch = new char[size];
str.copy(ch, size);
cds.cbData = size;
cds.lpData = (LPVOID) ch;
SendMessage(hAugerateWindow, WM_COPYDATA, (WPARAM) hWindow, (LPARAM) &cds);
delete ch;
}
vector<HWND> Augerate::GetReadableAugerateWindows()
{
vector<HWND> augs;
Lock();
for (map<HWND, string>::iterator itr = data.begin(); itr != data.end(); itr++)
{
augs.push_back((*itr).first);
}
Unlock();
return augs;
}
bool Augerate::ProcessStringMessages(HWND hSource, string Message)
{
bool bDelete = false;
if (bEraseBeforeProcess)
{
map<HWND, string>::iterator itr = data.find(hSource);
if (itr != data.end()) data.erase(itr);
}
for (vector<AugerateMessageHandler*>::iterator itr = MessageHandlers.begin(); itr != MessageHandlers.end(); itr++)
{
bDelete = (*itr)->ProcessStringMessage(hSource, Message) ? true : bDelete;
}
return bDelete;
}
void Augerate::AddMessageHandler(AugerateMessageHandler *handler)
{
for (vector<AugerateMessageHandler*>::iterator itr = MessageHandlers.begin(); itr != MessageHandlers.end(); itr++)
{
if (*itr == handler) return;
}
MessageHandlers.push_back(handler);
}
void Augerate::RemoveMessageHandler(AugerateMessageHandler *handler)
{
for (vector<AugerateMessageHandler*>::iterator itr = MessageHandlers.begin(); itr != MessageHandlers.end(); itr++)
{
if (*itr == handler)
{
MessageHandlers.erase(itr);
return;
}
}
throw new AugraException("No such handler registered!");
}
Augric::Augric()
{
InitLocking();
if (!bInitialized)
{
if (!Init())
{
throw new AugraException("Could not initialize Augra! Could not register window class!");
}
}
if (AppAugric)
{
delete AppAugric;
}
EventWinMade = CreateEvent(NULL, false, false, NULL);
hThread = CreateThread(NULL, 0, AugricThreadProc, this, 0, 0);
if (!hThread) throw new AugraException("Could not create Augrical Thread!");
WaitForSingleObject(EventWinMade, INFINITE);
CloseHandle(EventWinMade);
if (!hWindow) throw new AugraException("Could not create Augrical Window!");
bConfirmAugerate = false;
AppAugric = this;
}
int Augric::GetAugerateProtocol(HWND hAugerateWindow)
{
return SendMessage(hAugerateWindow, AugerateMsg_GetProtocol, (WPARAM) hWindow, NULL);
}
bool Augric::Init()
{
WNDCLASSEX wcx;
ZeroMemory(&wcx, sizeof(WNDCLASSEX));
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.hInstance = GetModuleHandle(NULL);
wcx.lpfnWndProc = &Augric::WinProc;
wcx.lpszClassName = "AugricalWindow";
if (!RegisterClassEx(&wcx)) return false;
TestMsg = RegisterWindowMessage("Augric_Test");
AugricMsg_CreateAugerate = RegisterWindowMessage("Augric_CreateAugerate");
AugricMsg_DestroyAugerate = RegisterWindowMessage("Augric_DestroyAugerate");
AugricMsg_Destruct = RegisterWindowMessage("Augric_Destruct");
AugerateMsg_IsAugerate = RegisterWindowMessage("Augerate_IsAugerate");
AugerateMsg_ConfirmAugerate = RegisterWindowMessage("Augerate_ConfirmAugerate");
AugerateMsg_GetProtocol = RegisterWindowMessage("Augerate_GetProtocol");
AugerateMsg_CanWriteFresh = RegisterWindowMessage("Augerate_CanWriteFresh");
bInitialized = true;
return true;
}
Augric::~Augric()
{
AppAugric = 0;
SendMessage(hWindow, AugricMsg_Destruct, NULL, (LPARAM) this);
}
DWORD WINAPI Augric::AugricThreadProc(LPVOID lpParam)
{
Augric* augric = (Augric*) lpParam;
augric->hWindow = CreateWindow("AugricalWindow", "", NULL, 0, 0, 10, 10, NULL, NULL, GetModuleHandle(NULL), NULL);
SetEvent(augric->EventWinMade);
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 1;
}
LRESULT CALLBACK Augric::WinProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
//if (!AppAugric) return DefWindowProc(hWnd, Msg, wParam, lParam);
if (!AppAugric) goto ret;
if (AppAugric->hWindow == hWnd || AppAugric->GetAugerateByWindow(hWnd))
return AppAugric->ProcessWindowMessages(hWnd, Msg, wParam, lParam);
ret:
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
LRESULT Augric::ProcessWindowMessages(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
Augerate* a = GetAugerateByWindow(hWnd);
if (a) return a->ProcessWindowMessages(hWnd, Msg, wParam, lParam);
if (Msg == TestMsg)
{
MessageBox(NULL, "Test functions. So, window must have been created and is capable of receiving messages.", "Yeah!", MB_OK);
return 0;
}
if (Msg == AugricMsg_CreateAugerate)
return (LRESULT) CreateWindow("AugricalWindow", "", NULL, 0, 0, 10, 10, NULL, NULL, GetModuleHandle(NULL), NULL);
if (Msg == AugricMsg_DestroyAugerate)
{
DestroyWindow((HWND) lParam);
return 0;
}
if (Msg == AugricMsg_Destruct)
{
if ((Augric*) lParam == Augric::GetMainAugric())
{
DestroyWindow(hWindow);
ExitThread(0);
}
return 0;
}
if (Msg == AugerateMsg_ConfirmAugerate)
bConfirmAugerate = true;
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
Augerate* Augric::GetAugerateByWindow(HWND hWnd)
{
map<HWND, Augerate*>::iterator itr = m_Augerates.find(hWnd);
return itr != m_Augerates.end() ? m_Augerates[hWnd] : NULL;
}
void Augric::Test()
{
SendMessage(hWindow, TestMsg, 0, 0);
}
HWND Augric::CreateAugerateWindow()
{
return (HWND) SendMessage(hWindow, AugricMsg_CreateAugerate, NULL, NULL);
}
void Augric::DestroyAugerateWindow(HWND hWnd)
{
SendMessage(hWindow, AugricMsg_DestroyAugerate, NULL, (LPARAM) hWnd);
}
void Augric::RegisterAugerate(Augerate* aug)
{
m_Augerates[aug->GetWindow()] = aug;
}
void Augric::UnregisterAugerate(Augerate* aug)
{
map<HWND, Augerate*>::iterator itr = m_Augerates.find(aug->GetWindow());
if (itr != m_Augerates.end())
{
m_Augerates.erase(itr);
}
}
bool Augric::IsWindowAugerate(HWND hWnd)
{
bConfirmAugerate = false;
SendMessageTimeout(hWnd, AugerateMsg_IsAugerate, (WPARAM) hWindow, NULL, SMTO_NORMAL, 100, NULL);
return bConfirmAugerate;
}
vector<HWND> Augric::GetGlobalAugerateWindows()
{
vector<HWND> augs;
EnumWindows(EnumAugWinProc, (LPARAM) &augs);
return augs;
}
BOOL CALLBACK Augric::EnumAugWinProc(HWND hWnd, LPARAM lParam)
{
vector<HWND> *augs = (vector<HWND>*) lParam;
Augric *a = Augric::GetMainAugric();
if (a->IsWindowAugerate(hWnd)) augs->push_back(hWnd);
return TRUE;
}
AugerateMessageHandler::AugerateMessageHandler(AugerateMessageReceiver *ReceivingObject, ProcessStringMessageProc Proc)
{
m_Receiver = ReceivingObject;
m_Proc = Proc;
}
bool AugerateMessageHandler::ProcessStringMessage(HWND hSource, string Message)
{
return (m_Receiver->*m_Proc)(hSource, Message);
}
}
//Augra.h
//Author: Guido Arbia
#ifndef _AUGRA
#define _AUGRA
#include <Windows.h>
#include <vector>
#include <map>
#include <string>
using namespace std;
namespace Augra
{
class AugerateMessageHandler;
class AugerateMessageReceiver;
class Receivable
{
};
class Lockable
{
private:
CRITICAL_SECTION m_cs;
public:
void InitLocking() {InitializeCriticalSection(&m_cs);}
void Lock() {EnterCriticalSection(&m_cs);}
void Unlock() {LeaveCriticalSection(&m_cs);}
~Lockable() {DeleteCriticalSection(&m_cs);}
};
class Augerate : Lockable
{
HWND hWindow;
map<HWND, string> data;
int m_protocol;
Receivable* m_Receiver;
vector<AugerateMessageHandler*> MessageHandlers;
bool bEraseBeforeProcess;
public:
Augerate(int protocol = 0);
~Augerate();
HWND GetWindow() {Lock(); HWND hWnd = hWindow; Unlock(); return hWnd;}
LRESULT ProcessWindowMessages(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
string ReadString(HWND hAugerateWindow);
void WriteString(HWND hAugerateWindow, string& str);
bool CanWriteFresh(HWND hAugerateWindow);
vector<HWND> GetReadableAugerateWindows();
bool ProcessStringMessages(HWND hSource, string Message);
void AddMessageHandler(AugerateMessageHandler *handler);
void RemoveMessageHandler(AugerateMessageHandler *handler);
void SetEraseBeforeProcess(bool EraseBeforeProcess) {bEraseBeforeProcess = EraseBeforeProcess;}
};
class AugraException
{
char* m_desc;
public:
AugraException(char* description) {m_desc = description;}
char* GetDescription() {return m_desc;}
};
class Augric : Lockable
{
map<HWND, Augerate*> m_Augerates;
HWND hWindow;
HANDLE hThread;
HANDLE EventWinMade;
bool bConfirmAugerate;
static Augric* AppAugric;
static bool bInitialized;
static bool Init();
public:
Augric();
LRESULT ProcessWindowMessages(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
Augerate* GetAugerateByWindow(HWND hWnd);
HWND CreateAugerateWindow();
void DestroyAugerateWindow(HWND hWnd);
bool IsWindowAugerate(HWND hWnd);
int GetAugerateProtocol(HWND hAugerateWindow);
vector<HWND> GetGlobalAugerateWindows();
void RegisterAugerate(Augerate *Aug);
void UnregisterAugerate(Augerate *Aug);
void Test();
static DWORD WINAPI AugricThreadProc(LPVOID lpParam);
static LRESULT CALLBACK WinProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
static Augric* GetMainAugric() {return AppAugric;}
static BOOL CALLBACK EnumAugWinProc(HWND hWnd, LPARAM lParam);
~Augric();
};
typedef bool (AugerateMessageReceiver::*ProcessStringMessageProc)(HWND hSource, string Message);
class AugerateMessageHandler
{
AugerateMessageReceiver *m_Receiver;
ProcessStringMessageProc m_Proc;
public:
AugerateMessageHandler() {}
AugerateMessageHandler(AugerateMessageReceiver *ReceivingObject, ProcessStringMessageProc Proc);
virtual bool ProcessStringMessage(HWND hSource, string Message);
};
class AugerateMessageReceiver
{
};
typedef bool (AugerateMessageReceiver::*AugerateMessageProc)(HWND hSource, string Message);
}
#endif
//main.cpp
//Author: Guido Arbia
#include <iostream>
#include <string>
#include "Augra.h"
#include "AppTalk.h"
using namespace Augra;
using namespace AppTalking;
using namespace std;
void CheckMessages(AppSenator &senator)
{
vector<int> ports = senator.GetOpenPorts();
for (vector<int>::iterator itr = ports.begin(); itr != ports.end(); itr++)
{
AppConnection *con = senator.GetConnectionFromPort(*itr);
string message = con->ReadString();
if (message.length())
{
cout << "Message from " << senator.GetRemoteAppName(con->GetRemoteAppSenator()) << " on port " << *itr << endl;
cout << "Content: " << message << endl;
}
}
if (!ports.size()) cout << "No messages to display." << endl;
cout << endl;
}
void ListConnections(AppSenator &senator)
{
vector<int> ports = senator.GetOpenPorts();
for (vector<int>::iterator itr = ports.begin(); itr != ports.end(); itr++)
{
AppConnection *con = senator.GetConnectionFromPort(*itr);
string RemoteAppName = senator.GetRemoteAppName(con->GetRemoteAppSenator());
int port = *itr;
cout << "Connected to " << RemoteAppName << " on port " << port << endl;
}
if (!ports.size()) cout << "No connections to display!" << endl;
cout << endl;
}
void CreateConnection(AppSenator &senator)
{
string AppName;
int Port;
cout << "Application to connect to: ";
cin >> AppName;
cout << "Port to connect on? ";
cin >> Port;
if (senator.IsBridged(Port))
{
cout << "Sorry, there is already a connection through that port." << endl << endl;
return;
}
vector<HWND> apps = senator.GetAppSenatorWindowsByName(AppName, true);
if (!apps.size())
{
cout << "Sorry, there is no such registered application on the system." << endl << endl;
return;
}
else if (apps.size() > 1)
{
cout << "Sorry, but there is more than one app with this name. It is a very ambigious matter." << endl << endl;
return;
}
AppConnection* con = senator.Bridge(Port, apps[0]);
if (!con) cout << "Sorry, cannot connect through that port to the specified application. Perhaps it a connection is occupying it." << endl;
cout << endl;
//for (vector<HWND>::iterator itr = apps.begin(); itr != apps.end(); itr++)
//{
//}
}
void _SendMessage(AppSenator &senator)
{
int Port;
cout << "Port? ";
cin >> Port;
AppConnection *con = senator.GetConnectionFromPort(Port);
if (!con)
{
cout << "Sorry, but that port is closed." << endl << endl;
return;
}
string Message;
cout << "Message? ";
cin >> Message;
con->WriteString(Message);
cout << endl;
}
void ListApplications(AppSenator &senator)
{
vector<HWND> apps = senator.GetAppSenatorWindows(true);
for (vector<HWND>::iterator itr = apps.begin(); itr != apps.end(); itr++)
{
string AppName = senator.GetRemoteAppName(*itr);
cout << AppName << endl;
}
cout << endl << endl;
}
void KillPort(AppSenator &senator)
{
int Port;
cout << "Port? ";
cin >> Port;
senator.BreakBridge(Port);
cout << endl;
}
int main(int argc, char* argv[])
{
string AppName;
cout << "Enter the application name of this instance: " << endl;
cin >> AppName;
Augric augric;
AppSenator senator(AppName);
while(true)
{
int choice;
cout << "?: ";
cin >> choice;
cout << endl;
switch(choice)
{
case 0:
cout << "1. Check for Messages" << endl;
cout << "2. List connections" << endl;
cout << "3. Create connection" << endl;
cout << "4. Send Message" << endl;
cout << "5. List Applications" << endl;
cout << "6. Close Port" << endl;
break;
case 1:
CheckMessages(senator);
break;
case 2:
ListConnections(senator);
break;
case 3:
CreateConnection(senator);
break;
case 4:
_SendMessage(senator);
break;
case 5:
ListApplications(senator);
break;
case 6:
KillPort(senator);
break;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment