Upstream tarball 9356
[amule.git] / cocoa-mule / AppController.mm
blob89ba1467c7c42d16b43d183327f64c2b6edc63e5
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)applicationWillTerminate:(NSNotification *)aNotification {
58         //
59         // Save gui state in all views
60         //
61         if ([m_dload_controller respondsToSelector:@selector(saveGui)]) {
62                 [m_dload_controller performSelector:@selector(saveGui)];
63         }
64         
65         NSLog(@"Exiting ...\n");
68 -(void)awakeFromNib {
69         NSUserDefaults *args = [NSUserDefaults standardUserDefaults];
70         NSString *mode = [args stringForKey:@"mode"];
71         NSLog(@"amule controller started, mode = [%@]\n", mode);
72         
73         
74         [[NSApplication sharedApplication] setDelegate:self];
75         
76         NSString *targetaddr = 0;
77         int targetport = 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");
82                 return;
83         }       
85         if ( (mode != nil) && ([mode compare:@"remote"] == NSOrderedSame) ) {
86                 targetaddr = @"127.0.0.1";
87                 targetport = 4712;
88                 if (![self askCoreParams] ) {
89                         // "Cancel" selected on login dialog
90                         exit(0);
91                 }
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);
96         } else {
97                 NSLog(@"Local mode selected - starting daemon\n");
98                 targetaddr = @"127.0.0.1";
99                 targetport = 4712;
100                 if ( [self startDaemon] == -1 ) {
101                         NSRunCriticalAlertPanel(@"Daemon startup error", 
102                                                         @"Unable to start core daemon (amuled)",
103                                                         @"OK", nil,nil);
104                         exit(-1);
105                 }
106         }
107         
108         m_connection = [ECRemoteConnection remoteConnection];
109         [m_connection retain];
110         m_data = [amuleData initWithConnection:m_connection];
111         [m_data retain];
113         //
114         // bind datastructure to GUI controllers
115         //
116         [m_dload_controller setFileSet:m_data.downloads];
117         [m_data.downloads setGuiController:m_dload_controller];
118         
119         //
120         // daemon (either local or remote) must be running by now
121         //
122         // try to connect 3 times, hopefully giving daemon enough
123         // time to start listening for EC
124         //
125         for(int i = 0; i < 3; i++) {
126                 sleep(1);
127                 [m_connection connectToAddress:targetaddr withPort:targetport];
128                 [m_connection sendLogin:@"123456"];
129                 if ( !m_connection.error ) {
130                         break;
131                 }
132         }
133         if ( m_connection.error ) {
134                 NSRunCriticalAlertPanel(@"Connection error", 
135                         @"Unable to start communication with daemon",
136                         @"OK", nil,nil);
137                 exit(-1);
138         }
143 - (int)startDaemon {
144         int pid = fork();
145         if ( pid == -1 ) {
146                 // fork failed
147                 return -1;
148         } else if ( pid > 0 ) {
149                 sleep(2);
150                 NSLog(@"Parent running, calling waitpid for pid %d\n", pid);
151                 // parent
152                 int status;
153                 switch ( waitpid(pid, &status, WNOHANG) ) {
154                         case -1:
155                                 NSLog(@"waitpid() call failed with code %d\n", errno);
156                                 break;
157                         case 0:
158                                 NSLog(@"Daemon running on pid %d status %d\n", pid, status);
159                                 m_daemon_pid = pid;
160                                 break;
161                         default:
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);
166                                         return -1;
167                                 } else if ( WIFSIGNALED(status) ) {
168                                         int sig_num = WTERMSIG(status);
169                                         NSLog(@"Child process terminated on signal %d", sig_num);
170                                         return -1;
171                                 } else if ( WIFSTOPPED(status) ) {
172                                         NSLog(@"Child process stopped. Not supposed to happen.");
173                                         return -1;
174                                 } else {
175                                         NSLog(@"Should not get here: child status unknown = %x", status);
176                                 }
177                                 break;
178                 }
179                 return 0;
180         } else {
181                 // child
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");
186                 exit(-1);
187         }
188         return 0;
191 @end