public static void WriteToFile() { var store = IsolatedStorageFile.GetUserStoreForApplication(); if (logfile == null) { logfile = store.OpenFile("wazelog.txt", FileMode.Create); } byte[] bytes = Syscalls.StringToAscii(logs); logfile.Write(bytes, 0, bytes.Length); logfile.Close(); logs = ""; }
/* * * Pushes the file Connection path to be used for Waze ( for config files, icons, logs, etc. ). * * Prefers SD-card over device memory * * Returns : * * The length of the path. * */ public static int fileConnectionPath(int addr) { /*todomt * string root = null; * bool sdcard_exists = false; * bool internal_exists = false; * bool path_found = false; * byte[] str_bytes; * string valid_path = new string(); * string FILE_SYSTEM_ROOT = "file:///store/home/user"; * string FILE_SYSTEM_WAZE = "file:///store/home/user/waze"; * string SDCARD_WAZE = "file:///SDCard/BlackBerry/waze"; * try{ * Enumeration e = FileSystemRegistry.listRoots(); * while (e.hasMoreElements()) { * root = (string) e.nextElement(); * if( root.equalsIgnoreCase("sdcard/") ) { * sdcard_exists = true; * } * else if( root.equalsIgnoreCase("store/") ) { * internal_exists =true; * } * } * if(sdcard_exists){ * if(createPath(SDCARD_WAZE) == 1){ * valid_path = SDCARD_WAZE; * path_found = true; * } * } * if(!path_found){ * if(internal_exists){ * if(createPath(FILE_SYSTEM_WAZE) == 1){ * valid_path = FILE_SYSTEM_WAZE; * }else{ * valid_path = FILE_SYSTEM_ROOT; * } * }else{ * Logger.log("WAZE ERROR: No sd-card or internal memory"); * } * } * str_bytes = valid_path.getBytes(); * CRunTime.memcpy(addr,str_bytes,0,str_bytes.length); * return str_bytes.length; * }catch(Exception e){ * Logger.log("WAZE ERROR: Exception in fileConnectionPath : " + e); * return 0; * }*/ byte[] str_bytes = Syscalls.StringToAscii("Userstore://"); CRunTime.memcpy(addr, str_bytes, 0, str_bytes.Length); return(str_bytes.Length); }
public static void writeMsgToBuffer(String msg) { byte[] str_bytes = Syscalls.StringToAscii(msg); int length; if (str_bytes.Length > msgAddrSize) // do not overflow size of buffer in roadmap_main { length = msgAddrSize; } else { length = str_bytes.Length; } CRunTime.memcpy(msgAddr, str_bytes, 0, length); CRunTime.memoryWriteByte(msgAddr + length, 0); }
/* * * Adds the needed suffix to the url string. The connection Timeout parameter * * determines how long before a timeout is thrown. */ /*todomt * public static string str2Add2Url(bool printInfo){ * string st = ""; * st += ";ConnectionTimeout=25000"; * //The Device is a simultaor --> TCP * if (DeviceInfo.isSimulator()) * { * st += ";deviceside=true"; * } * else if ( ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_BIS_B ) == CoverageInfo.COVERAGE_BIS_B )|| * ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT ) == CoverageInfo.COVERAGE_DIRECT ) ) * { //A carrier is providing us with the data service * st += getConnectionSuffix(printInfo); * }else if (WIFI_ENABLED && _wifiAvailable){ * st += ";interface=wifi"; * }else{ * UIWorker.addUIEventLog("FreemapApp - getConnectionString - No network Coverage"); * return ""; * } * return st; * } */ /* * If called with updateAddr 0, don't update the static connection string address */ public static int getConnectionString(int updateAddr, int addr, int size) { int strSize; if (updateAddr != 0) { connStringAddr = addr; connStringSize = size; } strSize = connStringSize; string st = ""; st += ""; //todomt str2Add2Url(false); byte[] bytes = Syscalls.StringToAscii(st); strSize--; if (strSize > bytes.Length) { strSize = bytes.Length; } CRunTime.memcpy(connStringAddr, bytes, 0, strSize); CRunTime.memoryWriteByte(connStringAddr + strSize, 0); return(0); }
public void runNetLoop() { http_response_sync = new ManualResetEvent(false); resp = null; conn = null; //bool wait = true; //while (wait) //{ // lock (concurrent_conns_lock) // { // if (concurrent_conns <= 6) // { // wait = false; // concurrent_conns++; // } // } // if (wait) // Thread.Sleep(1000); //} int registeredHandle = 0; try { lock_object = new object(); conn = (HttpWebRequest)WebRequest.Create(url);//todomt (HttpConnection)Connector.open(url); conn.AllowReadStreamBuffering = false; conn.AllowAutoRedirect = true; //System.Net.ServicePointManager.Expect100Continue = false; if (method == 0) { conn.Method = "GET"; } else { conn.Method = "POST"; } if (updateTime != null && updateTime.Trim().Length > 0) { conn.Headers["IfModifiedSince"] = updateTime; } registeredHandle = CRunTime.registerObject(conn); } catch (Exception e) { quit = true; Logger.log(e.ToString()); UIWorker.addUIEventLog("Async Net : Exception opening URL " + e.ToString()); } UIWorker.addUIEventValid(c_do_async_connect_cb, registeredHandle, cb_addr, context, 0, false, this); if (quit) { return; } while (!quit) { lock (lock_object) { if (!do_read) { try { Monitor.Wait(lock_object); } catch (SynchronizationLockException e) { Logger.log(e.ToString()); } if (quit) { return; } if (!do_read) { continue; } } } Dictionary <string, string> conn_props; if (Syscalls.connection_properties.TryGetValue(registeredHandle, out conn_props)) { foreach (string key in conn_props.Keys) { string value = conn_props[key]; if (key.Equals("Content-type")) { conn.ContentType = value; } else if (key.Equals("User-Agent")) { ((HttpWebRequest)(conn)).UserAgent = value; } } Syscalls.connection_properties.Remove(registeredHandle); } try { if (Stream == null) { resp = null; Exception exp = null; try { if (conn.Method == "POST") { http_response_sync.Reset(); byte[] buffer; if (Syscalls.buffered_requests.TryGetValue(registeredHandle, out buffer)) { conn.BeginGetRequestStream(delegate(IAsyncResult result) { var request = (WebRequest)result.AsyncState; using (var str = request.EndGetRequestStream(result)) { Syscalls.buffered_requests.Remove(registeredHandle); str.Write(buffer, 0, (int)buffer.Length); #if DEBUG Logger.log("http put: " + System.Text.Encoding.UTF8.GetString(buffer, 0, buffer.Length)); #endif } http_response_sync.Set(); }, conn); } else { http_response_sync.Set(); } http_response_sync.WaitOne(); } http_response_sync.Reset(); Logger.log("Start downloading " + method + " " + conn.RequestUri); conn.BeginGetResponse(delegate(IAsyncResult result) { try { var request = (HttpWebRequest)result.AsyncState; Logger.log("Downloading " + request.RequestUri); resp = (HttpWebResponse)request.EndGetResponse(result); Logger.log("Finish getting response " + request.RequestUri); http_response_sync.Set(); } catch (Exception we) { exp = we; int status_code = -1; if (resp != null) { status_code = (int)resp.StatusCode; } Logger.log("status code " + status_code); if (we is WebException) { WebException wwe = (WebException)we; Logger.log("status - " + wwe.Status); if (wwe.Response != null) { Logger.log(" url:" + wwe.Response.ResponseUri); } Logger.log(wwe.ToString()); if (wwe.Data != null) { Logger.log(wwe.Data.ToString()); } } else { Logger.log(we.ToString()); } if (we.InnerException != null) { Logger.log("Inner exception " + we.InnerException.ToString()); } if (resp != null) { resp.Dispose(); } resp = null; http_response_sync.Set(); } }, conn); } catch (Exception ioe) { int status_code = -1; if (resp != null) { resp.Dispose(); status_code = (int)resp.StatusCode; } exp = ioe; Logger.log("status code2 " + status_code + " " + ioe.ToString()); if (ioe.InnerException != null) { Logger.log("and inner exception " + ioe.InnerException.ToString()); } resp = null; http_response_sync.Set(); } http_response_sync.WaitOne(); if (resp != null) { Stream = resp.GetResponseStream(); int status = (int)resp.StatusCode; long data_size = resp.ContentLength; string lastModifiedStr = resp.Headers["Last-Modified"]; Logger.log("Finish getting response stream for " + url); //Logger.log("Java header, s is " + lastModifiedStr); /* * We need to send c a complete header string, so we fake it by creating the * res string. More header fields can be added later on besides the content length and last-modified * */ string res = "HTTP/1.1 " + status + " OK\r\nContent-Length: " + data_size + "\r\n"; if (lastModifiedStr != null) { res += "Last-Modified:" + lastModifiedStr + "\r\n\r\n"; } else { res += "\r\n"; } buffer = new byte[4096]; byte[] res_bytes = Syscalls.StringToAscii(res); res_bytes.CopyTo(buffer, 0); buffer_len = res_bytes.Length; buffer_cur_ptr = 0; } else { UIWorker.addUIEventLog("Exception in async net read: " + exp); eof = true; quit = true; //lock (concurrent_conns_lock) //{ // concurrent_conns--; //} ////buffer = new byte[4096]; //string res = "HTTP/1.1 404 Not Found\r\n"; ///*byte[] res_bytes*/buffer = Syscalls.StringToAscii(res); ////res_bytes.CopyTo(buffer, 0); //buffer_len = /*res_bytes.Length;*/buffer.Length; //buffer_cur_ptr = 0; //do_read = false; } } else { if (buffer_cur_ptr == buffer_len) { buffer_len = Stream.Read(buffer, 0, buffer.Length); if (buffer_len == 0) { eof = true; Stream.Close(); //lock (concurrent_conns_lock) //{ // concurrent_conns--; //} } buffer_cur_ptr = 0; } } } catch (Exception e) { UIWorker.addUIEventLog("Exception in async net read: " + e.ToString()); eof = true; quit = true; } lock (lock_object) { do_read = false; } // Call read CB if (is_valid) { UIWorker.addUIEventValid(c_input_ready_cb, input_id, 0, 0, 0, false, this); } } }
private void runEventQueue(bool forever) { Item o; while (!should_quit) { o = null; lock (lockQueues) { if (priorityQueue.Count > 0) { o = priorityQueue.Dequeue() as Item; } else if (queue.Count > 0) { o = queue.Dequeue() as Item; } else { // Making sure no elements // adds to the queues will querying the queues if (forever) { try { isWaiting = true; // Wait until another thread insert new elements to the queue Monitor.Wait(lockQueues); isWaiting = false; } catch (Exception e) { Logger.log("Exception: " + e.ToString()); } } } //cur_item = o; if ((o != null) && o.user_draw) { userDrawCount--; } } if (!forever && (o == null)) { return; } if (o != null) { try { if (o.logString != null) { byte[] str_bytes; int length; str_bytes = Syscalls.StringToAscii(o.logString); if (str_bytes.Length > msgAddrSize) { // do not overflow size of buffer in roadmap_main length = msgAddrSize; } else { length = str_bytes.Length; } //lock (this) //{ CRunTime.memcpy(msgAddr, str_bytes, 0, length); CRunTime.memoryWriteByte(msgAddr + length, 0); try { CibylCallTable.fcall(o.addr, c_sp, o.p1, o.p2, o.p3, o.p4); } catch (Exception e) { Logger.log("UIWORKER - print to log file : Could not print out to log, message :" + o.logString + " Exception : " + e.ToString()); } //} } else if ((o.validity_check == null) || (o.validity_check.isValid())) { //lock (this) //{ //long start = DateTime.Now.Ticks;// System.currentTimeMillis(); //Logger.log("calling addres "+ Integer.toHexString(o.addr)); CibylCallTable.fcall(o.addr, c_sp, o.p1, o.p2, o.p3, o.p4); /* todomt * long end = DateTime.Now.Ticks;// System.currentTimeMillis(); * if ((end - start) > 750) * { * if ((end - start) > 3000) * { * Logger.log("UIWorker Callback took too long!!! " + (end - start) + " UIWorker Callback addr:" + o.addr); * addUIEventLog("UIWorker Callback took too long!!! " + (end - start) + " UIWorker Callback addr:" + o.addr); * } * else * { * Logger.log("UIWorker Callback took too long!!! " + (end - start)); * Logger.log("UIWorker Callback addr:" + o.addr); * } * } */ //} } } catch (Exception t) { //Console.WriteLine("Exception in UI action: " + t); //t.printStackTrace(); String res = "EXCEPTION in UiWorker, cb addr: " + o.addr + ", toString() : " + t.ToString(); Logger.log(res); addUIEventLog(res); } } } }