-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMPWSiteServer.m
153 lines (126 loc) · 4.3 KB
/
MPWSiteServer.m
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
150
151
152
153
//
// MPWSiteServer.m
// ObjectiveHTTPD
//
// Created by Marcel Weiher on 2/11/12.
// Copyright (c) 2012 Marcel Weiher. All rights reserved.
//
#import "MPWSiteServer.h"
//#import <MethodServer/MethodServer.h>
#import <ObjectiveSmalltalk/STCompiler.h>
#import <ObjectiveSmalltalk/MPWMethodStore.h>
#import "MPWHTMLRenderScheme.h"
#import "MPWHTTPServer.h"
@implementation MPWSiteServer
objectAccessor(MPWHTTPServer*, server, setServer)
objectAccessor(MPWSiteMap*, sitemap, setSitemap)
objectAccessor(MPWTemplater*, templater, setTemplater )
objectAccessor(MPWWriteThroughCache*, cache , setCache )
objectAccessor(STCompiler*, interpreter , setInterpreter )
objectAccessor(MethodServer*, methodServer , setMethodServer)
objectAccessor(MPWHTMLRenderScheme*, renderer , setRenderer)
-(void)initializeAndClearCache
{
// [[self cache] setReadWrite:[NSClassFromString(@"MPWSiteMap") scheme]];
}
-(void)enableCaching
{
[[self server] setDelegate:[self cache]];
}
-(void)disableCaching
{
NSLog(@"cached disabled");
[[self server] setDelegate:[self renderer]];
}
-createServer:aTemplater
{
if ( ![self server] ) {
[self setServer:[[[MPWHTTPServer alloc] init] autorelease]];
}
[[self server] setDefaultMimeType:@"text/html"];
[self setRenderer:[MPWHTMLRenderScheme scheme]];
[[self renderer] setSourceScheme:aTemplater];
[[self sitemap] setRenderer:[self renderer]];
[self setCache:[MPWWriteThroughCache storeWithSource:[self renderer]]];
[self initializeAndClearCache];
// [cache setCacheReads:YES];
[self enableCaching];
// [self disableCaching];
// NSLog(@"site-def: %@",[cache graphViz]);
return [self server];
}
-(void)createMethodServer
{
[self setMethodServer:[[[NSClassFromString(@"MethodServer") alloc] initWithMethodDictName:@"website"] autorelease]];
[[self methodServer] setupWithInterpreter:[self interpreter]];
[[self methodServer] setupWebServerInBackground];
[[self methodServer] setDelegate:self];
}
-(void)setupSite
{
if ( [[self sitemap] respondsToSelector:@selector(setupSite)]) {
[[self sitemap] setupSite];
[self setTemplater:[[self sitemap] createTemplater]];
[self createServer:[self templater]];
}
}
-(void)loadMethods
{
[[self interpreter] defineMethodsInExternalDict:self.methodDict];
}
-(NSDictionary*)siteDictForSiteMap:(MPWSiteMap*)sitemap
{
#if TARGET_OS_IPHONE
NSDictionary* dict=[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"website" ofType:@"classdict"]];
#else
NSString* methodString = [[sitemap frameworkResource:@"website" category:@"classdict"] stringValue];
NSDictionary *dict=[methodString propertyList];
#endif
return dict;
}
-(NSDictionary*)storedSiteDict
{
return [self siteDictForSiteMap:[self sitemap]];
}
-(instancetype)initWithSite:(MPWSiteMap*) aSite siteDict:(NSDictionary*)dict interpreter:(STCompiler*)interpreter
{
self = [super init];
[self setSitemap:aSite];
[self setInterpreter:interpreter];
self.methodDict = [[[dict objectForKey:@"methodDict"] mutableCopy] autorelease];
[self loadMethods];
[[self interpreter] bindValue:aSite toVariableNamed:@"site"];
[[self interpreter] bindValue:self toVariableNamed:@"siteServer"];
[[self interpreter] bindValue:[self server] toVariableNamed:@"server"];
// [self createMethodServer];
//
// NSString *uid=[dict objectForKey:@"uniqueID"];
// if ( uid ) {
// [[self methodServer] setUniqueID:uid];
// }
if ( [[self sitemap] respondsToSelector:@selector(sitemap)]) {
[[[self interpreter] schemes] setSchemeHandler:[[self sitemap] sitemap] forSchemeName:@"dynamic"];
}
// [self setupInterpreter];
return self;
}
-(instancetype)initWithSite:(MPWSiteMap*) aSite
{
self = [self initWithSite: aSite siteDict:[self siteDictForSiteMap:aSite] interpreter:[STCompiler compiler]];
[self setupSite];
return self;
}
-(NSDictionary*)siteDict
{
NSDictionary *methods = [[[self interpreter] methodStore] externalScriptDict];
NSMutableDictionary *totalDict = [[[self storedSiteDict] mutableCopy] autorelease];
totalDict[@"methodDict"]=methods;
return totalDict;
}
-(void)didDefineMethods:server
{
// NSLog(@"======= didDefineMethods, now setup site again");
[self setupSite];
[self initializeAndClearCache];
}
@end