What is a good way to run a time-consuming function from a C# webpage on the users CPU instead of running it on the web-server? Is it possible to get a C# function to run locally or do I have to write the function in JavaScript? The function itself is not secret in any way but I would prefer if the input and the output will be kept secret from the user. The solution should not require the user to download anything.
This functions uses data from the database + user input and when it's finished it writes the output to the database.
The functions best case is ~1s, average ~30s and worst ~10min (for every user) so it's not an option to run this function on the web-server.
You can get this to work, by either
coding the function in JavaScript
compiling the C# source to JavaScript (off the beaten track)
running the C# code in silverlight (done and done again, remember Java Applets? same science)
You won't really be able to keep the data totally secret, though.
Now, how to go about this... You will need a web service to provide the data to your silverlight component and another one to accept the computed results. You know, I don't really see why everyone else here thinks this is such a no-go...
As for data secrecy... The best you can do is obfuscate, though you should use a secure communication layer for aquiring the data and posting the results back, what ends up on the users computer will eventually be open for inspection by the user. If you use obfuscation techniques for your code, this will make reverse engineering an encryption/decryption scheme for the data payload harder, but you're playing essentially the same game as game devs / game crackers...
Personally, I would code the client side stuff in JavaScript. Chances are, what you want done is more of an algorithmic thing than a library thing, so porting to js should not be difficult to pull off.
You can't and shouldn't run arbitrary cs code on the user's computer. You also can't really run a long lived process in javascript on the client. User interaction with the page will be blocked and if they navigate to a different page it will stop.
What you should do is write a windows service to run these tasks in the background. Have a queue table where you save the input data from the web side, then have a service that polls the table for work and processes the input data.
You can't execute any C# code on the client computer when you they enter a webpage. You are correct in your assumption that you need to write it in javascript to execute it on the client.
This doesn't work. Web server does the processing. Unless you are doing distributed computing and stuff like that, it is not designed to work on a client. Client needs to download software to process stuff. Webpage is webpage. Text.
The only code you can run on the client is js.
That being said, you definitely don't want to run your function on the client if you have sensitive information involved.
Related
I'm trying to send data from a python script to my c# application using a standard input stream. I also need to eventually send data back from my c# application to the another python script. I first tried to do this with a UDP Connection which works fine for a couple lines, but I have a fair amount of data to send (a few thousand lines) and a requirement for data integrity which UDP cannot provide. I could also write to a file, but that seems very inefficient.
One last restriction is that while my two applications are related I cannot setup a direct connection between them using something like IronPython as they are both spawned separately by a 3rd party application.
This is what I am currently trying, but it is not working. Similar to this question: Passing data between Python and C# without writing a file
p = subprocess.Popen(C_SHARP_EXECUTABLE_FILE_PATH, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
p.communicate(blob)
On my C# side I'm not entirely sure how to read this data, but I've tried using things like a loop around this:
Console.ReadLine()
or getting the standard input and reading from it directly using:
Console.OpenStandardInput();
The current issue is that as soon as I call p.communicate my Python script gets locked and doesn't proceed. If it's waiting for the line to be read, what do I need to do to make it stop waiting? I tried only providing the stdin parameter, but that didn't help.
For last 2-3 weeks i am looking for a reason behind my problem. But unfortunately i could not found anything needed.
It is a Windows Phone Book reader application i am creating using c# and xaml .
For that i need to download book file from the web service and extract and save audio,image and html data from it. All data are stored in local folder.
The main functionality is that we can see highlighting of texts with the corresponding audio plays. And the high lighting functionality is done by java script function.
My problem is that the highlighting text is delaying some times and it doesn't meet with it's audio. And this is happening on the session where the book data downloaded from network or where download operation took place.
Is there anything related to network download operation can
prevent/delay the execution of java script function ?
Can network download operation makes any impact on Web browser control
?
If you use callback functions in JS the right way the function of eg. highlighting will start only after the loading is complete. This issue reminds me of a few things I have seen while toying with nodeJS that uses about 80% async functions and a loop. However, if you want to get things structured in JS you might want to have a look at RequireJS, but async func calls can be used anyways..
function highlightElement(element, function(){
// Load document here, once this operation is done, highlighting will start
}) {
element.addClass('active');
};
If you want a quick version scroll down to the "Edit|1|" part.
I HAVE done a bit of searching on this and can't seem to figure it out. I have a Webserver and a Minecraft server on the same machine (it never takes large loads so its ok) and I need the user to be able to put some input (in an html form), have that input saved in a file on the server, handled by a middleman app (which I've already got done and is in c#) and the middleman app interacts with the minecraft server.
Now everything I either have done before or know how to do. The only problem is saving the content of the form into a temporary text file so that the middleman app can do its magic. I thought about using SQL (since its on the server cause minecraft uses it for stats) but in my opinion its a bit overkill for something that will only be there for a few seconds. (not to mention then ill have to add SQL into the middleman app).
I don't really care where on the server the file ends up since I'll likely hard code the location into the middleman app and it will be deleted after the middleman app reads it. I can get saving to work in IDLE but not in this app on the server.
(I know this code won't take in anything from a form, this was just written as a test to save files)
import os
name = "none";
def editFile():
workfile = open("edit.x",'w')
workfile.write(name)
workfile.close()
def application(environ, start_response):
status = "200 OK"
output = "Testificate (Feature will be up shortly)"
response_headers = [('Content-type', 'text/plain'),('Content-Length',str(len(output)))]
start_response(status, response_headers)
return [output]
Extra Server info...
Hardware: Sufficiently powerful
WebServer: Wamp w/ Python Module installed on Apache
Also here is a link to what runs with that code Click HERE.
Edit|1|: I guess I didn't get much into the problem (It was late lastnight when I wrote this). Basically Any type of file will do. I want Ideally the simplest to implement. The above code has no result On the server. It never creates the file nor can it read from the file (if I create it manually). I've been working at it for about a day and a half now. I'm really just hoping Its a mistake on my part. Could the server config in wamp dis-allow the creation of files via Python?
Check out the tempfile module, for example, tempfile.mkstemp() with a known directory. Have your "middleman app" poll (or perhaps using inotify) the directory for new files, process the file, and then delete it when done.
So I realized what I did while at lunch today. (facepalming myself in the process). The reason it wasn't working was the fact that I used backslashes, not forward slashes. So here is the working version of the above (I used triple quotes just to be sure). I also changed it to be absolute path.
def editFile():
workfile = open("""C:/wamp/www/test/edit.txt""",'w')
workfile.write(name)
workfile.close()
After that it worked flawlessly. So if anyone else makes that small mistake here is a reminder. lol Thanks to "mhawke" for the reply on tempfiles Its good info to have in either case.
I have a unique set of processes that all need to be automated.
We receive very inconsistent data from our customer so this requires a lot of responses from not very computer literate users. I'de go with console if it wasn't for that.
That data needs to be transformed and then combined using a few different processes.
I need to create an application that can only be accessed from one person at a time (we don't want to have multiple people building the same data).
All processes can be run on one machine.
A basic outline is the following...
Get all of the zip files from our customers FTP
Unzip all of these files into the specified directory
Take this data and verify it's surface level integrity
Transform the data to a new format
Import to the database
Build documents based on the data
I know how to write each of these functions, my question is more: should I do this in MVC3 with AJAX updates, WPF, windows forms, or straight asp.net? I know all of them, I just can't think of which fits this linear processing scheme. The user also needs constant updating of progress on each file so any of the asp.net derivatives get tricky with ajax.
I'd recommend just making a console application. Do you need an interface?
Two options: a console application to be launched as a scheduled task, or a Windows Service.
If everything is automated I would create a windows service to do everything. By doing so you'll also prevent the application from being run by more than once simultaneously (unless you install it on several computers).
I am in the design stages of an app involving QR codes. It will be a contest where a user sees a QR code and scans it. The first user to scan the QR code is the winner.
Because the contest is on a first come first serve basis I want to make sure that the first user who scans the code is the winner. I have noticed that (at least from what I have read) there are Javascript and C# ways to read QR codes (Is there a JQuery way?).
Which way would be the fastest/fairest/most efficient? Assume that there will be many many people scanning a QR code at once. I want to makes sure that the first guy who scans it is the winner, and not get into issues because his request wasn't processed fast enough.
I guess the real question I would be asking would be this: When there are many users attempting one action on one app, would a client side or server side method work better? Would the differences have an effect like I am worried about? ect.
There are two stages to scanning a QR code.
Capturing the image
Converting the image to text
Step 1 has to be done on the client.
Then it becomes a question of "Client CPU power + time to send the text data" Vs. "Server CPU power (shared between clients) + time to send the image data".
Client CPU power is variable. Bandwidth is variable. Latency is variable. You'll need to provide the server with more CPU power and bandwidth per user if you want the decoding to happen there.
You can, of course, measure the time from when the image was uploaded instead of decoded if you go with the "send the image" approach.
So, in conclusion:
would a client side or server side method work better?
Both will some of the time.
Either way the process of decoding the image will take some time, and that varies depending on the client CPU or the network speed.
Once you have captured the image on the client, you could request a token/timestamp from the server. That will be a small request, so the network speed will not affect it much. Then you can proceed to either decode the image on the client or send i to the server to be decoded.
If two users come in close in time, you know which one captured the image first from their tokens.
On a modern smartphone, detecting and decoding a QR code is something that can happen in under 100ms. I doubt variability in the client-side would produce any meaningful unfairness. But, I certainly think the client-side variability is smaller than the network variability which would come into play when uploading images rather than decoding client side.