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];
58 NSUserDefaults *args = [NSUserDefaults standardUserDefaults];
59 NSString *mode = [args stringForKey:@"mode"];
60 NSLog(@"amule controller started, mode = [%@]\n", mode);
62 NSString *targetaddr = 0;
65 if ( (mode != nil) && ([mode compare:@"guitest"] == NSOrderedSame) ) {
66 [m_dload_tableview reloadData];
67 NSLog(@"Started in GUI test mode - will not connect to core");
71 if ( (mode != nil) && ([mode compare:@"remote"] == NSOrderedSame) ) {
72 targetaddr = @"127.0.0.1";
74 if (![self askCoreParams] ) {
75 // "Cancel" selected on login dialog
78 NSLog(@"Remote mode selected, target=%@:%d\n", targetaddr, targetport);
80 NSLog(@"Local mode selected - starting daemon\n");
81 if ( [self startDaemon] == -1 ) {
82 NSRunCriticalAlertPanel(@"Daemon startup error",
83 @"Unable to start core daemon (amuled)",
89 m_connection = [ECRemoteConnection remoteConnection];
90 [m_connection retain];
91 m_data = [amuleData initWithConnection:m_connection];
95 // daemon (either local or remote) must be running by now
97 // try to connect 3 times, hopefully giving daemon enough
98 // time to start listening for EC
100 for(int i = 0; i < 3; i++) {
102 [m_connection connectToAddress:targetaddr withPort:targetport];
103 [m_connection sendLogin:@"123456"];
104 if ( !m_connection.error ) {
108 if ( m_connection.error ) {
109 NSRunCriticalAlertPanel(@"Connection error",
110 @"Unable to start communication with daemon",
122 } else if ( pid > 0 ) {
124 NSLog(@"Parent running, calling waitpid for pid %d\n", pid);
127 switch ( waitpid(pid, &status, WNOHANG) ) {
129 NSLog(@"waitpid() call failed with code %d\n", errno);
132 NSLog(@"Daemon running on pid %d status %d\n", pid, status);
136 //NSLog(@"waitpid returned pid=%d status=%x\n", pid, status);
137 if ( WIFEXITED(status) ) {
138 int exit_code = WEXITSTATUS(status);
139 NSLog(@"Daemon exec failed - child process exited with status %d", exit_code);
141 } else if ( WIFSIGNALED(status) ) {
142 int sig_num = WTERMSIG(status);
143 NSLog(@"Child process terminated on signal %d", sig_num);
145 } else if ( WIFSTOPPED(status) ) {
146 NSLog(@"Child process stopped. Not supposed to happen.");
149 NSLog(@"Should not get here: child status unknown = %x", status);
156 NSLog(@"Child running, calling execlp\n");
157 //execlp("/usr/bin/touch", "/usr/bin/touch", "xxxx", 0);
159 NSLog(@"execlp() failed\n");