Upstream tarball 9324
[amule.git] / cocoa-mule / AppController.mm
blobab45f2013aa6459dc4b39b98696b2117b27eb910
1 #import "AppController.h"
3 #import "LoginDialogController.h"
4 #import "DownloadsViewController.h"
6 #include <unistd.h>
8 @implementation AppController
10 - (IBAction)show_Networks:(id)sender {
11         [m_main_tabview selectTabViewItemAtIndex: 2];
12         
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];
19 //      [stream open];
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)awakeFromNib {
58         NSUserDefaults *args = [NSUserDefaults standardUserDefaults];
59         NSString *mode = [args stringForKey:@"mode"];
60         NSLog(@"amule controller started, mode = [%@]\n", mode);
61         
62         NSString *targetaddr = 0;
63         int targetport = 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");
68                 return;
69         }       
71         if ( (mode != nil) && ([mode compare:@"remote"] == NSOrderedSame) ) {
72                 targetaddr = @"127.0.0.1";
73                 targetport = 4712;
74                 if (![self askCoreParams] ) {
75                         // "Cancel" selected on login dialog
76                         exit(0);
77                 }
78                 NSLog(@"Remote mode selected, target=%@:%d\n", targetaddr, targetport);
79         } else {
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)",
84                                                         @"OK", nil,nil);
85                         exit(-1);
86                 }
87         }
88         
89         m_connection = [ECRemoteConnection remoteConnection];
90         [m_connection retain];
91         m_data = [amuleData initWithConnection:m_connection];
92         [m_data retain];
94         //
95         // daemon (either local or remote) must be running by now
96         //
97         // try to connect 3 times, hopefully giving daemon enough
98         // time to start listening for EC
99         //
100         for(int i = 0; i < 3; i++) {
101                 sleep(1);
102                 [m_connection connectToAddress:targetaddr withPort:targetport];
103                 [m_connection sendLogin:@"123456"];
104                 if ( !m_connection.error ) {
105                         break;
106                 }
107         }
108         if ( m_connection.error ) {
109                 NSRunCriticalAlertPanel(@"Connection error", 
110                         @"Unable to start communication with daemon",
111                         @"OK", nil,nil);
112                 exit(-1);
113         }
117 - (int)startDaemon {
118         int pid = fork();
119         if ( pid == -1 ) {
120                 // fork failed
121                 return -1;
122         } else if ( pid > 0 ) {
123                 sleep(2);
124                 NSLog(@"Parent running, calling waitpid for pid %d\n", pid);
125                 // parent
126                 int status;
127                 switch ( waitpid(pid, &status, WNOHANG) ) {
128                         case -1:
129                                 NSLog(@"waitpid() call failed with code %d\n", errno);
130                                 break;
131                         case 0:
132                                 NSLog(@"Daemon running on pid %d status %d\n", pid, status);
133                                 m_daemon_pid = pid;
134                                 break;
135                         default:
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);
140                                         return -1;
141                                 } else if ( WIFSIGNALED(status) ) {
142                                         int sig_num = WTERMSIG(status);
143                                         NSLog(@"Child process terminated on signal %d", sig_num);
144                                         return -1;
145                                 } else if ( WIFSTOPPED(status) ) {
146                                         NSLog(@"Child process stopped. Not supposed to happen.");
147                                         return -1;
148                                 } else {
149                                         NSLog(@"Should not get here: child status unknown = %x", status);
150                                 }
151                                 break;
152                 }
153                 return 0;
154         } else {
155                 // child
156                 NSLog(@"Child running, calling execlp\n");
157                 //execlp("/usr/bin/touch", "/usr/bin/touch", "xxxx", 0);
158                 execlp("amuled", 0);
159                 NSLog(@"execlp() failed\n");
160                 exit(-1);
161         }
162         return 0;
165 @end