|
-
Jun 1st, 2011, 06:15 AM
#1
Thread Starter
Hyperactive Member
Advise, Queue ?
Hello,
I have make api that using winsock control split job's for clients and make some calculation's.
When 1st client from LAN complete calculation it send's back message to server that calculation is complete and server sends STOP info to all other client's to stop calculation for same job.
That is not problem and evrything works fine, but my problem is,
how to add more job's to queue and afther client sent message that job is done, how to send new job from server to clients ?
If I store all job's in txt file, evry line will be command, but I'm not sure how
to select new line each time when new calculation need to start?
example of command line in queue
Calc-1122121238533212-218731287381236278537951297351276231
Calc-2321312128913213-127839172836865213138287821738217381
Calc-2151512512535124-652896579361864367672564561526561253
and so on...
I will receive afther calculation that 1122121238533212 job is done, maybe I can load txt file, find that string in line and erase whole line, but
how to select first next line afther that ?
Or is there any other way, maybe better ?
B.R
Last edited by VB Client/Server; Jun 1st, 2011 at 06:19 AM.
-
Jun 1st, 2011, 06:57 AM
#2
Re: Advise, Queue ?
Maybe saving the jobs in an indexed table (let's say an Access table with autoincrement field as a key)
Advantages:
When the job is done you can delete it easy
The next job to select always will be the first record
Or You can keep the jobs adding a field to the table to store if job is done or not
etc...
JG
-
Jun 1st, 2011, 08:29 AM
#3
Thread Starter
Hyperactive Member
Re: Advise, Queue ?
 Originally Posted by jggtz
Maybe saving the jobs in an indexed table (let's say an Access table with autoincrement field as a key)
Advantages:
When the job is done you can delete it easy
The next job to select always will be the first record
Or You can keep the jobs adding a field to the table to store if job is done or not
etc...
JG
What do U mean as "indexed table" ?
-
Jun 1st, 2011, 09:41 AM
#4
Re: Advise, Queue ?
I'm sorry for my english
A table with an index so you can 'directly' access each record in an easy way (read, write, modify & delete)
JG
-
Jun 1st, 2011, 11:28 AM
#5
Thread Starter
Hyperactive Member
Re: Advise, Queue ?
 Originally Posted by jggtz
I'm sorry for my english
A table with an index so you can 'directly' access each record in an easy way (read, write, modify & delete)
JG
Still dont get it how..
-
Jun 1st, 2011, 11:36 AM
#6
Re: Advise, Queue ?
Another option is to use a class structure for your server. Each class is a job, i.e., clsJobs, and has properties as needed (i.e., time started, completed, commands, description, which client sent it, etc).
The classes can be added to a VB collection. 1st job class in the collection, 1st started.
So when job #1 is complete (collection's .Item(1)), you can remove that item and the next job becomes #1, etc, etc, etc.
If you don't want to remove the job from the collection, don't. Just us another variable to indicate which index/job is current
-
Jun 2nd, 2011, 12:09 AM
#7
Thread Starter
Hyperactive Member
Re: Advise, Queue ?
 Originally Posted by LaVolpe
Another option is to use a class structure for your server. Each class is a job, i.e., clsJobs, and has properties as needed (i.e., time started, completed, commands, description, which client sent it, etc).
The classes can be added to a VB collection. 1st job class in the collection, 1st started.
So when job #1 is complete (collection's .Item(1)), you can remove that item and the next job becomes #1, etc, etc, etc.
If you don't want to remove the job from the collection, don't. Just us another variable to indicate which index/job is current
Can you help with code, I realy dont know how to make this work and if I make something, I doubt that will work smothly.
-
Jun 2nd, 2011, 03:21 AM
#8
Re: Advise, Queue ?
Here's an example of a 'skeleton' Server which will 'hand out' work to clients as they connect. It's slightly different from what I understand you're currently doing as it will send each client a different unit of work. When the client completes a calculation, the server will send it the next unit of work until they've all been completed. This, if for example, you've got 3 Clients connected they will all be working on a different unit of work. (simulating parallel processing to some extent) When all the work has been completed it display the results of who's done what unit of work, when it started, when it finished and what the result was into a multi-line TextBox.
It is a similar approach to that proposed by LaVolpe but uses a UDT rather than Class.
Hopefully you can see the 'key' parts:
(a) Reading the data and creating a UDT array.
(b) Keeping track of the next unit of work to be performed.
(c) Receiving notice of completion (and results) from a Client and sending them the next unit of work, if there is one.
(d) Reporting the outcomes when all units of work have been completed.
You should be able to modify it quite easily to suit your needs (hopefully)
Code:
Option Explicit
'
' Assumes:
' Winsock control named wsServer with Index Property = 0
' Multi-Line TextBox named txtResults
'
Private Type Calc
Command As String
CalcID As String
CalcData As String
CalcResult As String
StartTime As Date
EndTime As Date
Client As String
End Type
Private udtCalc() As Calc
Private intCurCalc As Integer
Private strClientBuff() As String
Private Sub Form_Load()
Dim intI As Integer
Dim intUDT As Integer
Dim intFile As Integer
Dim strDataFile As String
Dim strData As String
Dim strRecords() As String
Dim strRecord() As String
ReDim strClientBuff(0)
txtResults.Text = ""
'
' Read all the data and create a UDT
' containing the information about the units of work
' Assumes format of file is
' Calc-calcid-calcdata
' eg
' Calc-1122121238533212-218731287381236278537951297351276231
'
intFile = FreeFile
strDataFile = "C:\myData.txt" ' Whatever the path and filename is
Open strDataFile For Input As intFile
strData = Input(LOF(intFile), intFile)
Close intFile
If strData <> vbNullString Then
strRecords = Split(strData, vbCrLf)
For intI = 0 To UBound(strRecords)
If strRecords(intI) <> vbNullString Then
ReDim Preserve udtCalc(intUDT)
strRecord = Split(strRecords(intI), "-")
udtCalc(intI).Command = strRecord(0)
udtCalc(intI).CalcID = strRecord(1)
udtCalc(intI).CalcData = strRecord(2)
intUDT = intUDT + 1
End If
Next intI
Else
MsgBox "Data File is Empty"
End If
'
' Start listening for connection requests
'
wsServer(0).LocalPort = 1234 ' Whatever Port you choose
wsServer(0).Listen
End Sub
Private Sub wsServer_Close(Index As Integer)
'
' A client has closed a connection
' close and unload the corresponding Server socket
'
wsServer(Index).Close
Unload wsServer(Index)
End Sub
Private Sub wsServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Dim strSend As String
'
' A client wants to connect
' Establish a new socket for this client
' Accept the request, allocate a receive buffer
' and send it the next unit of work
'
Load wsServer(wsServer.UBound + 1)
wsServer(wsServer.UBound).Accept requestID
If intCurCalc <= UBound(udtCalc) Then
ReDim Preserve strClientBuff(UBound(strClientBuff) + 1)
Call SendNextCalc(wsServer.UBound)
End If
End Sub
Private Sub wsServer_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim strReceived As String
Dim strResults() As String
Dim intPos As Integer
Dim intCalcDone As Integer
Dim boComplete As Boolean
'
' Something has been received from a Client
' Buffer the data until a vbCrLf is received
'
wsServer(Index).GetData strReceived
strClientBuff(Index) = strClientBuff(Index) & strReceived
intPos = InStr(strClientBuff(Index), vbCrLf)
If intPos > 0 Then
strReceived = Mid$(strClientBuff(Index), 1, intPos - 1)
strResults = Split(strReceived, ",")
'
' What's expected from the client is:
' FINISHED,Calc Number,Calc Results vbCrLf
'
Select Case strResults(0)
Case "FINISHED"
intCalcDone = CInt(strResults(1))
udtCalc(intCalcDone).EndTime = TimeValue(Now)
udtCalc(intCalcDone).Client = wsServer(Index).RemoteHostIP
udtCalc(intCalcDone).CalcResult = strResults(2)
'
' If there's any more work to be done
' send this client the next one
'
If intCurCalc <= UBound(udtCalc) Then
Call SendNextCalc(Index)
Else
'
' All work has been completed
' Output the results etc to a
' multi-line TextBox (txtResults)
'
Call ShowResults
End If
Case Else
MsgBox "Unrecognised Response From Client - " & wsServer(Index).RemoteHostIP & vbCrLf _
& strResults(0), , "Received From Client"
End Select
strClientBuff(Index) = ""
End If
End Sub
Private Sub SendNextCalc(intClient As Integer)
Dim strSend As String
'
' Send the next unit of work to the client
'
' Sending:
' Command-Calc Number-CalcID-CalcData vbcrlf
' eg
' Calc-2-2151512512535124-652896579361864367672564561526561253
'
strSend = udtCalc(intCurCalc).Command _
& "-" & CStr(intCurCalc) _
& "-" & udtCalc(intCurCalc).CalcID _
& "-" & udtCalc(intCurCalc).CalcData
wsServer(intClient).SendData strSend & vbCrLf
udtCalc(intCurCalc).StartTime = TimeValue(Now)
intCurCalc = intCurCalc + 1
End Sub
Private Sub ShowResults()
Dim intI As Integer
For intI = 0 To UBound(udtCalc)
txtResults.Text = txtResults.Text & "CalcID: " & udtCalc(intI).CalcID _
& " Completed by: " & udtCalc(intI).Client _
& " Start Time: " & Format(udtCalc(intI).StartTime, "hh:mm:ss") _
& " End Time: " & Format(udtCalc(intI).EndTime, "hh:mm:ss") _
& " Result: " & udtCalc(intI).CalcResult & vbCrLf
Next intI
'
' Close communications with the clients
'
For intI = 1 To wsServer.UBound
wsServer(intI).Close
Next intI
End Sub
-
Jun 2nd, 2011, 03:31 AM
#9
Re: Advise, Queue ?
Here's a 'skeleton' client that will 'talk' to the 'skeleton' Server
Code:
Option Explicit
Private Sub Form_Load()
Me.Caption = "Not Connected"
wsClient.RemoteHost = "localhost" 'Change to whatever the Server ip Address is
wsClient.RemotePort = 1234 'Change to whatever Port the Server is listening on
wsClient.Connect
End Sub
Private Sub wsClient_Close()
'
' We've been asked to close
'
wsClient.Close
Me.Caption = "Not Connected"
End Sub
Private Sub wsClient_Connect()
Me.Caption = "Connected to Server"
End Sub
Private Sub wsClient_DataArrival(ByVal bytesTotal As Long)
Dim strReceived As String
Dim strData() As String
Dim strCalcID As String
Dim strCalcData As String
Dim strResult As String
Static strBuffer As String
Dim intPos As Integer
wsClient.GetData strReceived
strBuffer = strBuffer & strReceived
intPos = InStr(strBuffer, vbCrLf)
If intPos > 0 Then
strReceived = Mid$(strBuffer, 1, intPos - 1)
strData = Split(strReceived, "-")
Select Case UCase(strData(0))
Case "CALC"
strCalcID = strData(2)
strCalcData = strData(3)
strResult = DoCalc(strCalcData)
wsClient.SendData "FINISHED," & strData(1) & "," & strResult & vbCrLf
Case Else
MsgBox "Unrecognised Request from Server - " & strData(0), , "Received from Server"
End Select
strBuffer = ""
End If
End Sub
Private Function DoCalc(strData As String) As String
Dim strResult As String
Dim lngResult As Long
Dim intI As Integer
'
' Perofrm whatever calculation / processing
' of the data and put the result into strResult
' As an example, this will return the sum of the numbers
' in strData
'
For intI = 1 To Len(strData)
lngResult = lngResult + CInt(Mid$(strData, intI, 1))
Next intI
strResult = CStr(lngResult)
DoCalc = strResult
End Function
This just simulates doing something with the data. Basically you just have to put your calculations in Function DoCalc and return a string as the result. (this example happens quickly so don't blink !)
Last edited by Doogle; Jun 2nd, 2011 at 03:46 AM.
-
Jun 2nd, 2011, 05:46 AM
#10
Thread Starter
Hyperactive Member
Re: Advise, Queue ?
Thank you for your time and your post, but I dont have problems with winsock and comunication betwean clients or server.
Maybe my question was not clear.
My problem is only queue, I have txt file like this
Calc-7622121238533212-218731287381236278537951297351276231
Calc-1122121235444532-364634812362785379512973512534535434
Calc-2222121238534212-235234447381236278537955345345437888
Calc-1522121235466412-623732287381236278537951297351423342
Calc-3422121238588212-342343287381236278537923423526236232
server sent's to all clients same command like this
Calc-7622121238533212-218731287381236278537951297351276231
Calc- is command for client for start calculate, "7622121238533212" will be
placed in client txt1 and "218731287381236278537951297351276231" i client txt2, that works fine
when client finish calc they send message
FINISHED: and results
when server find string "FINISHED" it send's command "STOP" to all clients to
stop calc.
So here is my problem, when server receive message "STOP", how to open
queue.txt file, erase 1st line which was finished and select new line for send to clients ?
I need a timer function, which will check for rtbCommand.Text
for string "FINISHED"
then only open queue.txt, erase 1st line, select 2nd line and input it to txt1 for example.
and continue loop
Last edited by VB Client/Server; Jun 2nd, 2011 at 07:08 AM.
-
Jun 2nd, 2011, 11:21 AM
#11
Thread Starter
Hyperactive Member
Re: Advise, Queue ?
OK, is here is my code, works but this is not what I want
Code:
Private Sub Timer1_Timer()
If RichTextBox1.Text = "FINISHED" Then
Text1.Text = "OK Found"
F = FreeFile
Open App.Path & "\queue.txt" For Input As #F 'Opens filename for loading
Text2.Text = Input$(LOF(F), F) ' Text2 = the input of Length of file(freefile), freefile
Close #F 'Closes the loading, so it isnt running
Text2.SelStart = 0 'select first position
Text2.SelLength = 61 'select last postion
Text2.SelText = "" ' erase selected
Text2.SelStart = 0 ' now there is empty space in 1st erased line so again select position for erase
Text2.SelLength = 1 ' ....
Text2.SelText = "" ' erased
Text2.SelStart = 0 ' again
Text2.SelLength = 1 ' again
Text2.SelText = "" ' done now is like I wish to be
RichTextBox1.Text = ""
Dim ff As Integer
ff = FreeFile
Open App.Path & "\queue.txt" For Output As #ff ' open file for save
Print #ff, Text2.Text ' save file
Close #ff ' close
Exit Sub
Else
Text1.Text = "Found and replaced"
End If
End Sub
Code:
Private Sub Command2_Click()
RichTextBox1.Text = "FINISHED"
End Sub
but even when it's empty queue.txt sw continue to remove I dont know what!?
in queue.txt file is several lines like this
Code:
Calc-356053034232198-65576b916096e7ca73ef9401a25302fad31f6031
-
Jun 2nd, 2011, 12:19 PM
#12
Re: Advise, Queue ?
I don't think a text file is a good idea for a queue implementation.
I think a Collection object would make the most sense. As far as I understand it is implemented internally as a linked list, this means that taking from the front and adding to the back are very simple operations.
-
Jun 2nd, 2011, 12:38 PM
#13
Thread Starter
Hyperactive Member
Re: Advise, Queue ?
 Originally Posted by Milk
I don't think a text file is a good idea for a queue implementation.
I think a Collection object would make the most sense. As far as I understand it is implemented internally as a linked list, this means that taking from the front and adding to the back are very simple operations.
I want that queue.txt can be modified while some calc is in progress, either delete line which will not be in current progress or add new lines!
-
Jun 2nd, 2011, 01:18 PM
#14
Re: Advise, Queue ?
That does not really explain why you can't use a Collection, it fact it gives several reasons why you should.
-
Jun 2nd, 2011, 01:23 PM
#15
Re: Advise, Queue ?
 Originally Posted by VB Client/Server
I want that queue.txt can be modified while some calc is in progress, either delete line which will not be in current progress or add new lines!
...that could be an easy task if you manage the queue in a table or a collection or ...
JG
-
Jun 2nd, 2011, 01:43 PM
#16
Re: Advise, Queue ?
Well, I'm totally lost !
You want to have several clients processing the same data at the same time. When one finishes you want to stop all the others from processing the same data. Why ? (is it some sort of 'race'?)
You then want to repeat that process for all 'work units' which seems to me like a waste of time and resource.
I posted a method of 'queuing' eariler using an array of User Defined Types which you could easily modify to meet your, somewhat strange, requirements.
What exactly are you trying to achieve ?
-
Jun 2nd, 2011, 02:06 PM
#17
Thread Starter
Hyperactive Member
Re: Advise, Queue ?
 Originally Posted by Doogle
Well, I'm totally lost !
You want to have several clients processing the same data at the same time. When one finishes you want to stop all the others from processing the same data. Why ? (is it some sort of 'race'?)
You then want to repeat that process for all 'work units' which seems to me like a waste of time and resource.
I posted a method of 'queuing' eariler using an array of User Defined Types which you could easily modify to meet your, somewhat strange, requirements.
What exactly are you trying to achieve ?
All client's receive same data, but each client have other start point of calculation, let's say, caluclation for only 1 pc will take 10 day's, so if I split calculation on 10 pc's and make each client start adress different, let's say server which is also 1st client will take calc from 0-10 % next client will start from 10-20 % and so on...
1st client which will find exact calc will return that info to server, and then start new calc, hope now is clear.
So, for example, 2nd client which calc address will start from 20-30% can find exact code at 21%, he will return
info to server that calc is finished and the rest client's will also stop calculate.
And that can be only 1 hour of calc insted of using 1 pc for 10 day's.
What I need is only queue and how to erase line and select another afther finished calc.
Last edited by VB Client/Server; Jun 2nd, 2011 at 02:44 PM.
-
Jun 2nd, 2011, 08:04 PM
#18
Re: Advise, Queue ?
Sounds suspiciously like a network of machines doing brute-force or dictionary password cracking to me.
-
Jun 3rd, 2011, 12:53 AM
#19
Thread Starter
Hyperactive Member
Re: Advise, Queue ?
 Originally Posted by dilettante
Sounds suspiciously like a network of machines doing brute-force or dictionary password cracking to me.
No I working for Skynet, we have build new X5000 T but he is on a lose, we
need to find algo to be able bring him back home safely
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|