platform/iphone/Classes/Server/ServerHost.m in rhodes-1.2.2 vs platform/iphone/Classes/Server/ServerHost.m in rhodes-1.4.0
- old
+ new
@@ -9,33 +9,34 @@
#pragma mark Includes
#include <assert.h>
#include <unistd.h>
-#include "ruby/ext/rho/rhoruby.h"
-
#include "defs.h"
#include "Server.h"
#include "HttpContext.h"
#include "ServerHost.h"
-#include "Dispatcher.h"
+//#include "Dispatcher.h"
#include "AppManagerI.h"
#include "common/RhoConf.h"
#include "logging/RhoLogConf.h"
#include "sync/syncthread.h"
+#include "common/RhodesApp.h"
#include "JSString.h"
#import "WebViewUrl.h"
#import "ParamsWrapper.h"
#import "DateTime.h"
#import "NativeBar.h"
#import "MapViewController.h"
+#include "ruby/ext/rho/rhoruby.h"
#import "logging/RhoLog.h"
#undef DEFAULT_LOGCATEGORY
#define DEFAULT_LOGCATEGORY "ServerHost"
-extern char* get_current_location();
+//extern char* get_current_location();
+extern void geo_init();
#pragma mark -
#pragma mark Constant Definitions
#define kServiceType CFSTR("_http._tcp.")
@@ -72,31 +73,32 @@
static ServerHost* sharedSH = nil;
@implementation ServerHost
-@synthesize actionTarget, onStartFailure, onStartSuccess, onRefreshView, onNavigateTo, onExecuteJs;
-@synthesize onSetViewHomeUrl, onSetViewOptionsUrl, onTakePicture, onChoosePicture, onChooseDateTime, onCreateNativeBar;
+@synthesize actionTarget, /*onStartFailure,*/ onStartSuccess, onRefreshView, onNavigateTo, onExecuteJs;
+@synthesize /*onSetViewHomeUrl, onSetViewOptionsUrl,*/ onTakePicture, onChoosePicture, onChooseDateTime;
+@synthesize onCreateNativeBar, onRemoveNativeBar, onSwitchTab;
@synthesize onShowPopup, onVibrate, onPlayFile, onSysCall, onMapLocation, onCreateMap, onActiveTab;
- (void)serverStarted:(NSString*)data {
if(actionTarget && [actionTarget respondsToSelector:onStartSuccess]) {
[actionTarget performSelector:onStartSuccess withObject:data];
}
// Do sync w/ remote DB
//wake_up_sync_engine();
}
-
+/*
- (void)serverFailed:(void*)data {
if(actionTarget && [actionTarget respondsToSelector:onStartFailure]) {
[actionTarget performSelector:onStartFailure];
}
-}
+}*/
-- (void)refreshView {
+- (void)refreshView:(int)index {
if(actionTarget && [actionTarget respondsToSelector:onRefreshView]) {
- [actionTarget performSelectorOnMainThread:onRefreshView withObject:NULL waitUntilDone:NO];
+ [actionTarget performSelectorOnMainThread:onRefreshView withObject:(void*)index waitUntilDone:NO];
}
}
- (void)navigateTo:(WebViewUrl*) wvUrl {
if(actionTarget && [actionTarget respondsToSelector:onNavigateTo]) {
@@ -112,16 +114,16 @@
- (void)performRefreshView {
[self performSelectorOnMainThread:@selector(refreshView)
withObject:NULL waitUntilDone:NO];
}
-
+/*
- (void)setViewHomeUrl:(NSString*)url {
if(actionTarget && [actionTarget respondsToSelector:onSetViewHomeUrl]) {
[actionTarget performSelector:onSetViewHomeUrl withObject:url];
}
-}
+}*/
- (void)takePicture:(NSString*) url {
if(actionTarget && [actionTarget respondsToSelector:onTakePicture]) {
[actionTarget performSelectorOnMainThread:onTakePicture withObject:url waitUntilDone:NO];
}
@@ -154,15 +156,30 @@
[actionTarget performSelectorOnMainThread:onCreateNativeBar withObject:nativeBar waitUntilDone:YES];
[nativeBar release];
}
}
+- (void)removeNativeBar {
+ if (actionTarget && [actionTarget respondsToSelector:onRemoveNativeBar]) {
+ [actionTarget performSelectorOnMainThread:onRemoveNativeBar withObject:nil waitUntilDone:YES];
+ }
+}
+
+- (void)switchTab:(int)index {
+ if (actionTarget && [actionTarget respondsToSelector:onSwitchTab]) {
+ NSValue* value = [NSValue valueWithPointer:&index];
+ [actionTarget performSelectorOnMainThread:onSwitchTab withObject:value waitUntilDone:YES];
+ [value release];
+ }
+}
+
+/*
- (void)setViewOptionsUrl:(NSString*)url {
if(actionTarget && [actionTarget respondsToSelector:onSetViewOptionsUrl]) {
[actionTarget performSelector:onSetViewOptionsUrl withObject:url];
}
-}
+}*/
- (void)showPopup:(NSString*) message {
if(actionTarget && [actionTarget respondsToSelector:onShowPopup]) {
[actionTarget performSelectorOnMainThread:onShowPopup withObject:message waitUntilDone:NO];
}
@@ -210,43 +227,38 @@
if(actionTarget && [actionTarget respondsToSelector:onSysCall]) {
[actionTarget performSelectorOnMainThread:onSysCall withObject:pw waitUntilDone:NO];
}
}
+#if defined(RHO_USE_OWN_HTTPD) && !defined(RHO_HTTPD_COMMON_IMPL)
- (void)ServerHostThreadRoutine:(id)anObject {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-
+
+ runLoop = CFRunLoopGetCurrent();
+ m_geoThread = [NSThread currentThread];
+ geo_init();
+
RAWLOG_INFO("Initializing ruby");
RhoRubyStart();
-
- char* _url = rho_conf_getString("start_path");
- homeUrl = [NSString stringWithCString:_url encoding:NSUTF8StringEncoding];
- rho_conf_freeString(_url);
- _url = rho_conf_getString("options_path");
- optionsUrl = [NSString stringWithCString:_url encoding:NSUTF8StringEncoding];
- rho_conf_freeString(_url);
- RAWLOG_INFO1("Start page: %s", [homeUrl UTF8String]);
- RAWLOG_INFO1("Options page: %s", [optionsUrl UTF8String]);
- [[ServerHost sharedInstance] setViewHomeUrl:homeUrl];
- [[ServerHost sharedInstance] setViewOptionsUrl:optionsUrl];
-
- runLoop = CFRunLoopGetCurrent();
ServerContext c = {NULL, NULL, NULL, NULL};
ServerRef server = ServerCreate(NULL, AcceptConnection, &c);
if (server != NULL && ServerConnect(server, NULL, kServiceType, 8080)) {
RAWLOG_INFO("HTTP Server started and ready");
RAWLOG_INFO("Create Sync");
rho_sync_create();
RhoRubyInitApp();
+ rho_ruby_activateApp();
[self performSelectorOnMainThread:@selector(serverStarted:)
- withObject:homeUrl waitUntilDone:NO];
+ withObject:NULL waitUntilDone:NO];
[[NSRunLoop currentRunLoop] run];
- RAWLOG_INFO("Invalidating local server");
+
+
+ RAWLOG_INFO("Invalidating local server");
ServerInvalidate(server);
} else {
RAWLOG_INFO("Failed to start HTTP Server");
[self performSelectorOnMainThread:@selector(serverFailed:)
withObject:NULL waitUntilDone:NO];
@@ -257,26 +269,39 @@
RAWLOG_INFO("Stopping ruby");
RhoRubyStop();
RAWLOG_INFO("Server host thread routine is completed");
- [pool release];
+ [pool release];
}
+#else // defined(RHO_USE_OWN_HTTPD) && !defined(RHO_HTTPD_COMMON_IMPL)
+- (void)ServerHostThreadRoutine:(id)anObject {
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+
+ runLoop = CFRunLoopGetCurrent();
+ m_geoThread = [NSThread currentThread];
+ geo_init();
+ [[NSRunLoop currentRunLoop] run];
+
+ RAWLOG_INFO("Server host thread routine is completed");
+ [pool release];
+}
+#endif // defined(RHO_USE_OWN_HTTPD) && !defined(RHO_HTTPD_COMMON_IMPL)
+
/*
- (int)initializeDatabaseConn {
NSString *appRoot = [AppManager getApplicationsRootPath];
NSString *path = [appRoot stringByAppendingPathComponent:@"../db/syncdb.sqlite"];
return sqlite3_open([path UTF8String], &database);
}*/
-extern const char* RhoGetRootPath();
+//extern const char* RhoGetRootPath();
-(void) start {
//Create
appManager = [AppManager instance];
//Configure AppManager
- rho_logconf_Init(RhoGetRootPath());
[appManager configure];
//Init log and settings
//Start Sync engine
//[self initializeDatabaseConn];
@@ -284,33 +309,49 @@
//start_sync_engine(database);
// Start server thread
[NSThread detachNewThreadSelector:@selector(ServerHostThreadRoutine:)
toTarget:self withObject:nil];
+ rho_rhodesapp_create(rho_native_rhopath());
+#if !defined(RHO_USE_OWN_HTTPD) || defined(RHO_HTTPD_COMMON_IMPL)
+ rho_rhodesapp_start();
+#endif
}
+void* rho_nativethread_start()
+{
+ return [[NSAutoreleasePool alloc] init];
+}
+
+void rho_nativethread_end(void* pData)
+{
+ NSAutoreleasePool *pool = (NSAutoreleasePool *)pData;
+ [pool release];
+}
+
-(void) stop {
- CFRunLoopStop(runLoop);
+ rho_rhodesapp_destroy();
+ CFRunLoopStop(runLoop);
// Stop the sync engine
//stop_sync_engine();
//shutdown_database();
}
-
+/*
//Sync all sources
- (void) doSync {
rho_sync_doSyncAllSources(TRUE);
}
- (void) doSyncFor:(NSString*)url {
rho_sync_doSyncSourceByUrl([url cStringUsingEncoding:[NSString defaultCStringEncoding]]);
-}
+}*/
- (void)dealloc
{
[appManager release];
- [homeUrl release];
- [optionsUrl release];
+ //[homeUrl release];
+ //[optionsUrl release];
[super dealloc];
}
+ (ServerHost *)sharedInstance {
@synchronized(self) {
@@ -353,43 +394,55 @@
}
@end
//ruby extension hooks
-void webview_refresh() {
- [[ServerHost sharedInstance] refreshView];
+void webview_refresh(int index) {
+ [[ServerHost sharedInstance] refreshView:index];
}
void webview_navigate(char* url, int index) {
WebViewUrl *webViewUrl = [[[WebViewUrl alloc] init] autorelease];
+ //char* szNormUrl = rho_http_normalizeurl(url);
webViewUrl.url = [NSString stringWithUTF8String:url];
+ //rho_http_free(szNormUrl);
webViewUrl.webViewIndex = index;
[[ServerHost sharedInstance] navigateTo:webViewUrl];
}
-char* webview_execute_js(char* js) {
+char* webview_execute_js(char* js, int index) {
char * retval;
JSString *javascript = [[[JSString alloc] init] autorelease];
javascript.inputJs = [NSString stringWithUTF8String:js];
+ javascript->index = index;
[[ServerHost sharedInstance] executeJs:javascript];
// TBD: Does ruby GC pick this up?
retval = strdup([[javascript outputJs] cStringUsingEncoding:[NSString defaultCStringEncoding]]);
return retval;
}
void perform_webview_refresh() {
[[ServerHost sharedInstance] performRefreshView];
}
-char* webview_current_location() {
- return get_current_location();
+void rho_conf_show_log() {
}
-void webview_set_menu_items(VALUE argv) {
+char* webview_current_location(int index) {
+ return (char*)rho_rhodesapp_getcurrenturl(index);
+}
+
+void webview_set_menu_items(VALUE valMenu) {
//TODO: webview_set_menu_items
+ rho_rhodesapp_setViewMenu(valMenu);
}
+void Init_RingtoneManager()
+{
+ //TODO: implement Ringtone
+}
+
void alert_show_popup(char* message) {
if (message==NULL) {
RAWLOG_ERROR("Alert.show_popup - wrong arguments");
} else {
[[ServerHost sharedInstance] showPopup:[NSString stringWithCString:message]];
@@ -423,11 +476,11 @@
initialTime:initial_time
format:format
data:[NSString stringWithCString:data]];
}
-void _rho_map_location(char* query) {
+void rho_map_location(char* query) {
[[ServerHost sharedInstance] mapLocation:[NSString stringWithCString:query]];
}
void mapview_create(int nparams, char** params, int nannotations, char** annotation) {
#ifdef __IPHONE_3_0
@@ -449,14 +502,22 @@
for(int i = 0; i < nparams; i++) {
if (params[i]) {
printf("param: %s\n", params[i]);
[items addObject:[NSString stringWithCString:params[i]]];
} else {
- printf("param: nil");
+ printf("param: nil\n");
[items addObject:@""];
}
}
[[ServerHost sharedInstance] createNativeBar:bar_type dataArray:items];
+}
+
+void remove_nativebar() {
+ [[ServerHost sharedInstance] removeNativeBar];
+}
+
+void nativebar_switch_tab(int index) {
+ [[ServerHost sharedInstance] switchTab:index];
}
int webview_active_tab() {
return [[ServerHost sharedInstance] activeTab];
}
\ No newline at end of file