Browse Source

publish-subscribe

works_for_me
Volker Birk 2 years ago
parent
commit
c73fb9daa2
5 changed files with 59 additions and 20 deletions
  1. +22
    -3
      pEpMacOSAdapter/main.m
  2. +7
    -2
      pEpMacOSAdapter/pEpMacOSAdapter.m
  3. +16
    -2
      pEpMacOSAdapter/pEpMacOSAdapterProtocol.h
  4. +3
    -3
      pEpMacOSAdapter/pEpUpdater.h
  5. +11
    -10
      pEpMacOSAdapter/pEpUpdater.mm

+ 22
- 3
pEpMacOSAdapter/main.m View File

@ -7,6 +7,7 @@
// This file is under GNU General Public License 3.0
//
#include <signal.h>
#import <Foundation/Foundation.h>
#import "pEpMacOSAdapter.h"
@ -22,6 +23,7 @@
- (BOOL)listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection *)newConnection {
// This method is where the NSXPCListener configures, accepts, and resumes a new incoming NSXPCConnection.
NSLog(@"incoming connection");
// Configure the connection.
// First, set the interface that the exported object implements.
@ -30,24 +32,41 @@
// Next, set the object that the connection exports. All messages sent on the connection to this service will be sent to the exported object to handle. The connection retains the exported object.
pEpMacOSAdapter *exportedObject = [pEpMacOSAdapter new];
newConnection.exportedObject = exportedObject;
if (!newConnection.exportedInterface || !newConnection.exportedObject) {
NSLog(@"failed to allocate object and interface");
return NO;
}
// Resuming the connection allows the system to deliver more incoming messages.
[newConnection resume];
// Returning YES from this method tells the system that you have accepted this connection. If you want to reject the connection for some reason, call -invalidate on the connection and return NO.
NSLog(@"connection accepted");
return YES;
}
@end
void signal_TERM(int signal)
{
NSLog(@"stopping agent");
exit(0);
}
int main(int argc, const char *argv[])
{
signal(SIGTERM, signal_TERM);
// Create the delegate for the service.
ServiceDelegate *delegate = [ServiceDelegate new];
// Set up the one NSXPCListener for this service. It will handle all incoming connections.
NSXPCListener *listener = [NSXPCListener serviceListener];
NSXPCListener *listener = [[NSXPCListener alloc] initWithMachServiceName:@"foundation.pEp.adapter.macOS"];
NSLog(@"starting agent");
listener.delegate = delegate;
[listener resume];
[[NSRunLoop currentRunLoop] run];
return 0;
}

+ 7
- 2
pEpMacOSAdapter/pEpMacOSAdapter.m View File

@ -9,7 +9,6 @@
#import "pEpMacOSAdapter.h"
#import "pEpUpdater.h"
#include "Subscriber.h"
static pEpUpdater* _updater = nil;
@ -17,8 +16,10 @@ static pEpUpdater* _updater = nil;
// begin pEpMacOSAdapterProtocol
- (void)subscribeForUpdate:(DownloadNotification)downloadNotification
- (void)subscribeForUpdate:(pEpNotification*)downloadNotification
{
NSLog(@"subscribeForUpdate called");
if (downloadNotification) {
if (!_updater) {
_updater = [[pEpUpdater alloc] initWithSubscriber:downloadNotification];
@ -32,11 +33,15 @@ static pEpUpdater* _updater = nil;
- (void)unsubscribeForUpdate
{
NSLog(@"unsubscribeForUpdate called");
[_updater setNewSubscriber :nil];
}
- (void)updateNow
{
NSLog(@"updateNow called");
if (_updater) {
[_updater updateAll:nil];
}


+ 16
- 2
pEpMacOSAdapter/pEpMacOSAdapterProtocol.h View File

@ -8,7 +8,21 @@
//
#import <Foundation/Foundation.h>
#include "Subscriber.h"
/// Callback protocol to notify about download events
@protocol pEpNotificationProtocol
/// Notification types
typedef enum { downloading, downloadArrived, noDownloadAvailable } DNType;
/// notification about download events
- (void)notifyDownload:(DNType)type withName:(NSString*)name withFilename:(NSString*)filename;
@end
/// This is the object from the client where we deliver notifications to
@interface pEpNotification : NSObject <pEpNotificationProtocol>
@end
/// This protocol is providing the XPC interface to the User Interface program pEpNotifications
@protocol pEpMacOSAdapterProtocol
@ -19,7 +33,7 @@
- Parameter downloading: block to call back when a download is going to happen
- Parameter downloadArrived: block to call back when a download arrived
*/
- (void)subscribeForUpdate:(DownloadNotification)downloadNotification;
- (void)subscribeForUpdate:(pEpNotification*)downloadNotification;
/// unsubscribe from receiving notifications about downloads arriving
- (void)unsubscribeForUpdate;


+ 3
- 3
pEpMacOSAdapter/pEpUpdater.h View File

@ -8,7 +8,7 @@
//
#import <Foundation/Foundation.h>
#include "Subscriber.h"
#import "pEpMacOSAdapterProtocol.h"
/// object encapsulating the update client functionality of downloadclient
@interface pEpUpdater : NSObject
@ -22,10 +22,10 @@
/// initializes the pEpUpdater
///
/// - parameter subscriber: object fulfilling the `pEpMacOSAdapterSubscriberProtocol`.
- (id)initWithSubscriber:(DownloadNotification)subscriber;
- (id)initWithSubscriber:(pEpNotification*)subscriber;
/// switches the subscriber
- (void)setNewSubscriber:(DownloadNotification)subscriber;
- (void)setNewSubscriber:(pEpNotification*)subscriber;
/// schedules updates each 2 hours
- (void)scheduleUpdates;


+ 11
- 10
pEpMacOSAdapter/pEpUpdater.mm View File

@ -8,7 +8,6 @@
//
#import "pEpUpdater.h"
#import "Subscriber.h"
#include "../../downloadclient/downloadclient.hh"
@ -16,9 +15,9 @@ static const double CYCLE = 7200.0; // 7200 seconds = 2 hours
@implementation pEpUpdater
DownloadNotification _subscriber = nil;
pEpNotification *_subscriber = nil;
- (id)initWithSubscriber:(DownloadNotification)subscriber
- (id)initWithSubscriber:(pEpNotification*)subscriber
{
self = [super init];
self.configPath = @"/Library/Application Support/pEp/Updater";
@ -26,7 +25,7 @@ DownloadNotification _subscriber = nil;
return self;
}
- (void)setNewSubscriber:(DownloadNotification)subscriber
- (void)setNewSubscriber:(pEpNotification*)subscriber
{
_subscriber = subscriber;
}
@ -88,10 +87,12 @@ DownloadNotification _subscriber = nil;
[localFileManager changeCurrentDirectoryPath:tmp];
std::string _filename;
pEp::notifyRead_t notifyRead = nullptr;
if (_subscriber) {
notifyRead = [=]() { _subscriber(downloading, name, nil); };
}
pEp::notifyRead_t notifyRead = [=]()->void{
if (_subscriber) {
[_subscriber notifyDownload:downloading withName:name withFilename:nil];
}
};
_filename = pEp::UpdateClient::update(p, keyfile, notifyRead);
if (_filename.length()) {
NSString* filename = [NSString stringWithUTF8String:_filename.c_str()];
@ -101,12 +102,12 @@ DownloadNotification _subscriber = nil;
NSLog(@"pEpMacOSAdapter: download arrived %@", download);
if (_subscriber) {
_subscriber(downloadArrived, name, download);
[_subscriber notifyDownload:downloadArrived withName:name withFilename:download];
}
}
else {
if (_subscriber) {
_subscriber(noDownloadAvailable, nil, nil);
[_subscriber notifyDownload:noDownloadAvailable withName:nil withFilename:nil];
}
}
}


Loading…
Cancel
Save