<commit_before>#!/usr/bin/python
# TCP client example

import socket
import communication
import datetime
import cv2

def getTimeStamp():
    timestamp = datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d%H%M%S')
    return timestamp

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("localhost", 10006))

#receive SERVER_READY
msg = communication.recv_msg(client_socket)
print "received ", msg
#send CLIENT_READY
print "sending ", communication.CLIENT_READY
communication.send_msg(client_socket, communication.CLIENT_READY)
#communication.send_msg(client_socket, communication.DATA_READY_ACK)
#receive CAMERA_ID
camera_id = communication.recv_msg(client_socket)
print "received ", camera_id
#send CAMERA_ID_ACK
print "sending ", communication.CAMERA_ID_ACK
communication.send_msg(client_socket, communication.CAMERA_ID_ACK)
fname = getTimeStamp() + "-" + camera_id
img = communication.recv_img(client_socket)
print "received image data"
cv2.imwrite("%s.bmp"%fname, img)
print "closing connection"
client_socket.close()
<commit_msg>Parallel connection to the servers<commit_after>#!/usr/bin/python
# TCP client example

import socket
import communication
import datetime
import time
import cv2
import os
from multiprocessing import Process

#args is a list of tuple
def run_parallel(func, args):
  proc = []
  for arg in args:
    p = Process(target=func, args=arg)
    p.start()
    proc.append(p)
  for p in proc:
    p.join()

def get_time_stamp():
    timestamp = datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d%H%M%S')
    return timestamp

def get_ip_list():
    with open("active_cameras.txt", "r") as f:
        active_ip = [l.strip() for l in f]
    return active_ip

def run(ip, port, output_dir):
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect((ip, port))    
    #receive SERVER_READY
    msg = communication.recv_msg(client_socket)
    print "received ", msg
    #send CLIENT_READY
    print "sending ", communication.CLIENT_READY
    communication.send_msg(client_socket, communication.CLIENT_READY)
    #communication.send_msg(client_socket, communication.DATA_READY_ACK)
    #receive CAMERA_ID
    camera_id = communication.recv_msg(client_socket)
    camera_id = str(int(camera_id)).zfill(2) #add leading zeros
    print "received ", camera_id
    #send CAMERA_ID_ACK
    print "sending ", communication.CAMERA_ID_ACK
    communication.send_msg(client_socket, communication.CAMERA_ID_ACK)
    fname = output_dir + '/' + str(camera_id)
    img = communication.recv_img(client_socket)
    print "received image data"
    cv2.imwrite("%s.bmp"%fname, img)
    print "closing connection"
    client_socket.close()

def test_run(ip, port):
    print "Opening socket on ip %s port %s"%(ip, port)
    time.sleep(5)
    print "closing socket on ip %s port %s"%(ip, port)

def __init__():
    dir = "images/"+get_time_stamp()
    if not os.path.exists(dir):
        os.makedirs(dir)
    port = 10006
    ips = get_ip_list()
    args = [(ip, port, dir) for ip in ips]
    print "Running parallel connections"
    run_parallel(run, args)

__init__()
{'fraction_non_alphanumeric': 0.10872313527180784, 'fraction_numerical': 0.0050568900126422255, 'mean_word_length': 4.723327305605786, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6011100', 'n_tokens_mistral': 1081, 'n_tokens_neox': 1046, 'n_words': 247}