Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SyncClientProxy.m « FinderSyncExt « OwnCloudFinderSync « MacOSX « shell_integration - github.com/owncloud/client.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b7b64571f747d2f005f551d76acd3f64506d97fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * Copyright (C) by Jocelyn Turcotte <jturcotte@woboq.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 */

#import "SyncClientProxy.h"

@protocol ServerProtocol <NSObject>
- (void)registerClient:(id)client;
@end

@interface SyncClientProxy ()
- (void)registerTransmitter:(id)tx;
@end

@implementation SyncClientProxy

- (instancetype)initWithDelegate:(id)arg1 serverName:(NSString*)serverName
{
	self = [super init];

	self.delegate = arg1;
	_serverName = serverName;
	_remoteEnd = nil;

	return self;
}

#pragma mark - Connection setup

- (void)start
{
	if (_remoteEnd)
		return;
	
	// Lookup the server connection
	NSConnection *conn = [NSConnection connectionWithRegisteredName:_serverName host:nil];
	
	if (!conn) {
		// Could not connect to the sync client
		[self scheduleRetry];
		return;
	}
	
	[[NSNotificationCenter defaultCenter] addObserver:self
		selector:@selector(connectionDidDie:)
		name:NSConnectionDidDieNotification
		object:conn];
	
	NSDistantObject <ServerProtocol> *server = (NSDistantObject <ServerProtocol> *)[conn rootProxy];
	assert(server);
	
	// This saves a few Mach messages, enable "Distributed Objects" in the scheme's Run diagnostics to watch
	[server setProtocolForProxy:@protocol(ServerProtocol)];
	
	// Send an object to the server to act as the channel rx, we'll receive the tx through registerTransmitter
	[server registerClient:self];
}

- (void)registerTransmitter:(id)tx;
{
	// The server replied with the distant object that we will use for tx
	_remoteEnd = (NSDistantObject <ChannelProtocol> *)tx;
	[_remoteEnd setProtocolForProxy:@protocol(ChannelProtocol)];

	// Everything is set up, start querying
	[self askOnSocket:@"" query:@"GET_STRINGS"];
}

- (void)scheduleRetry
{
	[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(start) userInfo:nil repeats:NO];
}

- (void)connectionDidDie:(NSNotification*)notification
{
#pragma unused(notification)
	_remoteEnd = nil;
	[_delegate connectionDidDie];
	
	[self scheduleRetry];
}

#pragma mark - Communication logic

- (void)sendMessage:(NSData*)msg
{
	NSString *answer = [[NSString alloc] initWithData:msg encoding:NSUTF8StringEncoding];

	// Cut the trailing newline
	answer = [answer substringToIndex:[answer length] - 1];
	NSArray *chunks = [answer componentsSeparatedByString: @":"];

	if( [[chunks objectAtIndex:0] isEqualToString:@"STATUS"] ) {
		NSString *result = [chunks objectAtIndex:1];
		NSString *path = [chunks objectAtIndex:2];
		if( [chunks count] > 3 ) {
			for( int i = 2; i < [chunks count]-1; i++ ) {
				path = [NSString stringWithFormat:@"%@:%@",
						path, [chunks objectAtIndex:i+1] ];
			}
		}
		[_delegate setResultForPath:path result:result];
	} else if( [[chunks objectAtIndex:0] isEqualToString:@"UPDATE_VIEW"] ) {
		NSString *path = [chunks objectAtIndex:1];
		[_delegate reFetchFileNameCacheForPath:path];
	} else if( [[chunks objectAtIndex:0 ] isEqualToString:@"REGISTER_PATH"] ) {
		NSString *path = [chunks objectAtIndex:1];
		[_delegate registerPath:path];
	} else if( [[chunks objectAtIndex:0 ] isEqualToString:@"UNREGISTER_PATH"] ) {
		NSString *path = [chunks objectAtIndex:1];
		[_delegate unregisterPath:path];
	} else if( [[chunks objectAtIndex:0 ] isEqualToString:@"GET_STRINGS"] ) {
		// BEGIN and END messages, do nothing.
	} else if( [[chunks objectAtIndex:0 ] isEqualToString:@"STRING"] ) {
		[_delegate setString:[chunks objectAtIndex:1] value:[chunks objectAtIndex:2]];
	} else {
		NSLog(@"SyncState: Unknown command %@", [chunks objectAtIndex:0]);
	}
}

- (void)askOnSocket:(NSString*)path query:(NSString*)verb
{
	NSString *query = [NSString stringWithFormat:@"%@:%@\n", verb,path];

	@try {
		[_remoteEnd sendMessage:[query dataUsingEncoding:NSUTF8StringEncoding]];
	} @catch(NSException* e) {
		// Do nothing and wait for connectionDidDie
	}
}

- (void)askForIcon:(NSString*)path isDirectory:(BOOL)isDir
{
	NSString *verb = isDir ? @"RETRIEVE_FOLDER_STATUS" : @"RETRIEVE_FILE_STATUS";
	[self askOnSocket:path query:verb];
}

@end