1 #import "AppController.h"
3 #import "LoginDialogController.h"
4 #import "DownloadsViewController.h"
8 @implementation AppController
10 - (IBAction)show_Networks:(id)sender {
11 [m_main_tabview selectTabViewItemAtIndex: 2];
14 // ECLoginPacket *p = [ECLoginPacket loginPacket:@"123456" withVersion:@"0.1"];
15 // NSOutputStream *stream = [NSOutputStream outputStreamToMemory];
16 // uint8_t buffer[1024];
17 // memset(buffer, 0, 1024);
18 // NSOutputStream *stream = [NSOutputStream outputStreamToBuffer:buffer capacity:1024];
20 // [p writeToSocket:stream];
21 // id data = [stream propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
22 // int off = [data length];
23 // NSLog(@"off=%d", off);
24 // [m_connection sendPacket:p];
27 - (IBAction)show_Search:(id)sender {
28 [m_main_tabview selectTabViewItemAtIndex: 1];
31 - (IBAction)show_Sharing:(id)sender {
32 [m_main_tabview selectTabViewItemAtIndex: 3];
35 - (IBAction)show_Stats:(id)sender {
36 [m_main_tabview selectTabViewItemAtIndex: 4];
39 - (IBAction)show_Xfers:(id)sender {
40 [m_main_tabview selectTabViewItemAtIndex: 0];
42 ECPacket *p = [ECPacket packetWithOpcode:EC_OP_GET_DLOAD_QUEUE];
43 [m_connection sendPacket:p];
46 -(IBAction)show_Preferences:(id)sender {
49 -(IBAction)show_About:(id)sender {
52 - (bool)askCoreParams {
53 LoginDialogController *dlg = [[LoginDialogController alloc] init];
54 return [dlg showDlg:nil];
57 - (void)applicationWillTerminate:(NSNotification *)aNotification {
59 // Save gui state in all views
61 if ([m_dload_controller respondsToSelector:@selector(saveGui)]) {
62 [m_dload_controller performSelector:@selector(saveGui)];
65 NSLog(@"Exiting ...\n");
69 NSUserDefaults *args = [NSUserDefaults standardUserDefaults];
70 NSString *mode = [args stringForKey:@"mode"];
71 NSLog(@"amule controller started, mode = [%@]\n", mode);
74 [[NSApplication sharedApplication] setDelegate:self];
76 NSString *targetaddr = 0;
79 if ( (mode != nil) && ([mode compare:@"guitest"] == NSOrderedSame) ) {
80 [m_dload_tableview reloadData];
81 NSLog(@"Started in GUI test mode - will not connect to core");
85 if ( (mode != nil) && ([mode compare:@"remote"] == NSOrderedSame) ) {
86 targetaddr = @"127.0.0.1";
88 if (![self askCoreParams] ) {
89 // "Cancel" selected on login dialog
92 [[NSUserDefaults standardUserDefaults] setObject:targetaddr forKey:@"LastTargetHost"];
93 [[NSUserDefaults standardUserDefaults] setInteger:targetport forKey:@"LastTargetPort"];
94 [[NSUserDefaults standardUserDefaults] synchronize];
95 NSLog(@"Remote mode selected, target=%@:%d\n", targetaddr, targetport);
97 NSLog(@"Local mode selected - starting daemon\n");
98 targetaddr = @"127.0.0.1";
100 if ( [self startDaemon] == -1 ) {
101 NSRunCriticalAlertPanel(@"Daemon startup error",
102 @"Unable to start core daemon (amuled)",
108 m_connection = [ECRemoteConnection remoteConnection];
109 [m_connection retain];
110 m_data = [amuleData initWithConnection:m_connection];
114 // bind datastructure to GUI controllers
116 [m_dload_controller setFileSet:m_data.downloads];
117 [m_data.downloads setGuiController:m_dload_controller];
120 // daemon (either local or remote) must be running by now
122 // try to connect 3 times, hopefully giving daemon enough
123 // time to start listening for EC
125 for(int i = 0; i < 3; i++) {
127 [m_connection connectToAddress:targetaddr withPort:targetport];
128 [m_connection sendLogin:@"123456"];
129 if ( !m_connection.error ) {
133 if ( m_connection.error ) {
134 NSRunCriticalAlertPanel(@"Connection error",
135 @"Unable to start communication with daemon",
148 } else if ( pid > 0 ) {
150 NSLog(@"Parent running, calling waitpid for pid %d\n", pid);
153 switch ( waitpid(pid, &status, WNOHANG) ) {
155 NSLog(@"waitpid() call failed with code %d\n", errno);
158 NSLog(@"Daemon running on pid %d status %d\n", pid, status);
162 //NSLog(@"waitpid returned pid=%d status=%x\n", pid, status);
163 if ( WIFEXITED(status) ) {
164 int exit_code = WEXITSTATUS(status);
165 NSLog(@"Daemon exec failed - child process exited with status %d", exit_code);
167 } else if ( WIFSIGNALED(status) ) {
168 int sig_num = WTERMSIG(status);
169 NSLog(@"Child process terminated on signal %d", sig_num);
171 } else if ( WIFSTOPPED(status) ) {
172 NSLog(@"Child process stopped. Not supposed to happen.");
175 NSLog(@"Should not get here: child status unknown = %x", status);
182 NSLog(@"Child running, calling execlp\n");
183 //execlp("/usr/bin/touch", "/usr/bin/touch", "xxxx", 0);
184 execlp("/Users/lfroen/prog/amule/src/amuled", "/Users/lfroen/prog/amule/src/amuled", 0);
185 NSLog(@"execlp() failed\n");